Hello, normaly I would do this without problems in PHP, the thing is that now I use Smarty and I have to do the {foreach} in the template not in the PHP. This is the stuff: I have a query and I extract everything from a table. $row = mysql_fetch_assoc($result) This is how I place the info into the $row variabile. Normaly I ould do a for and simply multiply the information and make it look as I want. Now I have to do the for in the template. <table border="1"> <tr class="table_header"> <td>#</td> <td>User</td> <td>Nume</td> <td>Prenume</td> <td>Email</td> <td>Mobil</td> <td>Opt.</td> </tr> {foreach} <tr> <td> </td> <td>{$user} </td> <td>{$nume} </td> <td>{$prenume} </td> <td>{$email} </td> <td>{$mobil} </td> <td> </td> </tr> {/foreach} <tr class="table_header"> <td>#</td> <td>User</td> <td>Nume</td> <td>Prenume</td> <td>Email</td> <td>Mobil</td> <td>Opt.</td> </tr> </table> HTML: This is the table, and as you see, the seccond row it will be mutyplied for each row I have in the database. Now should I build the foreach from smarty? P.S. In the table I have about 30 fields, but I want to show only 5 of them (user, nume, prenume, email, mobil). In PHP I would simply place $row['nume'] under the Nume coumn and so on...
Something like this: {foreach from=$row item=r name=row} <tr> <td>{$r.user} </td> <td>{$r.nume} </td> ... </tr> {/foreach} Code (markup):
This is the template: {foreach from=$row item=r name=row} <tr> <td> </td> <td>{$r.user} </td> <td>{$r.nume} </td> <td>{$r.prenume} </td> <td>{$r.email} </td> <td>{$r.mobil} </td> <td> </td> </tr> {/foreach} HTML: This is the PHP $query = "SELECT * FROM `users`"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); $smarty->assign('row',$row); $smarty->assign('title','Rheal - Admin CP - Show Users'); // PAGE TITLE $smarty->display('showusers.tpl'); PHP: I'm getting something like in the screenshot. Any sugestions? I'm a little tired to think this part. THanks P.S. I have about 7 entryes in the table.
{php} $query = "SELECT * FROM `users`"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); echo "<tr>"; echo "<td>" . $row['nume'] . "</td>"; //etc... echo "</tr>"; {/php}
Yes, look at the code the {php} and {/php} can be used in smarty. Those are smarty tags, and everything in between is php code.
You have only one single row after mysql_fetch_assoc execution. You need to iterate through all results (code not tested, just an example): $rows = array(); while ($row = mysql_fetch_assoc($result)) $rows[] = $row; $smarty->assign_by_ref('row',$rows); PHP: Note that assign_by_ref is more suitable in that case, it consumes less memory.