We help IT Professionals succeed at work.

Help! smarty template issue

tarakwar101
tarakwar101 asked
on
Medium Priority
361 Views
Last Modified: 2007-12-19
Help! I have been working with a smarty template for awhile now and so far I get it..but I am trying to render the results of a query, a (loop result) and I am stuck. I have it giving the results but they are an echo result and it shows up above the include header I can't get it to render within the template, because of the echo results. In the past all I needed to do was do something like this
EXAMPLE:
 $new_row = mysql_fetch_array($new_result);
 $user_name = stripslashes($new_row["Name"]);
 $user_name .= " ";
 $user_name .= stripslashes($new_row["Surename"]);
 $smarty->assign("user_name",$user_name);
}
and on the .tpl put in {if (isset($cur_student))} {$username} {/if} and I could put it any where I want it on the page, but I am not sure how to change the code for a loop to to show from the below code I have.
change it from an echo result to defined result and still do the loop?

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {     <!--- from here is where I am stuck!-->

        } else {
        echo  "<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Course ID:&nbsp;$CourseID&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Subject:&nbsp;$Subj&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Course Number:&nbsp;$Number&nbsp;</font></td>
<td><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-2\">Purchase Date:&nbsp;$Date&nbsp;</font></td><br>
</td> </tr>";
    }
$i++;
endwhile;
echo  "</table>";
}
Comment
Watch Question

Brian BushSolutions Architect

Commented:
Ok, I think I understand what you're wanting.
This is how you do that:

1. Create an array in PHP and assign it to a SMARTY variable.
2. Loop through the results in SMARTY to display.

for example:

$_test = array(
    array(
        "CourseID" => "101",
        "Subject" => "Physics",
        "Date" => "1/1/2006"
    ),
    array(
        "CourseID" => "103",
        "Subject" => "Physics",
        "Date" => "12/1/2006"
    ),
    array(
        "CourseID" => "101",
        "Subject" => "Calculus",
        "Date" => "1/1/2006"
    )
);

$smarty->assign("courses", $_test);

Then, in your template, something like this:

{foreach key=key item=item from=$courses}
    Row Number {$key}: {$item.Subject} {$item.CourseID} - {$item.Date}<br />
{/foreach}

Make sense?
--brian

Author

Commented:
Well I got it to show in the right place !! Now the only issue is getting it to loop? It shows the first info just great but then it shows the same info on next 2 lines as well.
Here is what I did.

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$form_value["CourseID"] = "value=\"".($row["CourseID"])."\"";
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
            
$showmy = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    ),
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    ),
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date
    )
)
}
$i++;
endwhile;
}
$smarty->assign("courses", $showmy);
Brian BushSolutions Architect

Commented:
Sorry I wasn't clear about that.

$showmy[] = array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
          "Date" => $Date
);

Try it like that.
--brian

Author

Commented:
Another question what if they have more then 3 do I have to put in as many arrays as I think they would have? Meaning if they have taken 20 courses do I have to add more arrays?
Is there a wat to code the array to just loop craeting another row?
Brian BushSolutions Architect

Commented:
The "[]" on the $showmy[] tell PHP to just add another one to the end of the array.
You can have as many as you want.

I was showing the three at the top as a manual loading of an array to test.
Sorry it was misleading.
--brian

Author

Commented:
When I added the $showmy[], then nothing showed up?

Author

Commented:
ahhh ok I will retest

Author

Commented:
Hmm a Little lost?

Author

Commented:
Do I need to add something on the template page? []
     <td>{foreach key=key item=item from=$courses}
    Course: {$item.Subject} Course ID:{$item.CourseID} Date: {$item.Date}<br />
{/foreach}</td>

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
   
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
            
      $showmy[] = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
            "Date" => $Date)
);
}
$i++;
endwhile;
}
$smarty->assign("courses", $showmy);

Author

Commented:
this it what shows up on template using above code:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:
Course: Course ID: Date:

Which is the correct number of courses for the test student but nothing shows up.
before here is what showed up:

Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
Course: State Regulatory Study Guide-Washington Course ID:116 Date: 02-14-2006
Brian BushSolutions Architect

Commented:
$result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=" . $session_data["CurStudentID"],  $link);

$showmy = array();
while ($row = mysql_fetch_assoc($result)) {
    $showmy[] = array(
       "CourseID" => $row["CourseID"],
       "Subject" => $row["Subj"],
       "Number" => $row["Number"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

That is the code to run a query and dump the result to an array called $showmy.

I am concerned about this line in your code:
if ($i%2 == 0) {

        }

That tells it to skip every other result from the query. where $i (the row number) modulus (%) 2 is zero.
That is rownumber/2 has a remainder of zero.

--brian

Author

Commented:
What should it be, I am still learning to use php. I was wondwering how that modulus (%) came into play?
The above code you posted though has no loop?
Solutions Architect
Commented:
The while($row = mysql_fetch_assoc($result)) is a loop.
It says loop through the $result until it is done.

You should take out these lines:

if ($result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=".$session_data["CurStudentID"], $link))
   while ($row = mysql_fetch_row($result))  

       $number1 = mysql_num_rows($result);
  if (!$number1)
   printf("<center><font face=\"Verdana, Arial, Helvetica, sans-serif\">No Course History found.</center></font><br> \n");
   
 $i = 0;
while ($i < $number1):

$CourseID = mysql_result($result, $i,"CourseID");
$Subj = mysql_result($result, $i,"Subj");
$Number = mysql_result($result, $i,"Number");
$Date = mysql_result($result, $i,"Date");
$Date = date('m-d-Y', $Date);

if ($i%2 == 0) {

        } else {
         
      $showmy[] = array(
    array(
        "CourseID" => $CourseID,
        "Subject" => $Subj,
        "Number" => $Number,
          "Date" => $Date)
);
}
$i++;
endwhile;
}

and then substitute these:
$result = mysql_query("SELECT * FROM ORDERS WHERE StudentID=" . $session_data["CurStudentID"],  $link);

$showmy = array();
while ($row = mysql_fetch_assoc($result)) {
    $showmy[] = array(
       "CourseID" => $row["CourseID"],
       "Subject" => $row["Subj"],
       "Number" => $row["Number"],
       "Date" => date('m-d-Y', $row["Date"]),
    );
}
mysql_free_result($result);

--brian


--brian

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Nothing happens now , nothing shows up?

Author

Commented:
AHHH perfect I forgot to add the $smarty->assign("courses", $showmy); Now it works great!!!

Author

Commented:
I knew I was over thinking this! You would crack up if you had seen what I had started with and what I tried after it didn't work! I actually had similiar code  to begin with. Grrrr
Thank you so much Brian! You have been an awsome teacher.
This gives me so much info for all the rest of the pages I have to do.
The next challenge is to do the same sort of thing. I have a page that when a cooordinator clicks link and page opens I have to show all other students that have the same companyid as he does?
But I will open that in another question WHEN I run into issues lol
thanks for all the help! A+
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.