|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
iusefedora Smarty Rookie
Joined: 04 Jan 2005 Posts: 6
|
Posted: Tue Jan 04, 2005 1:31 am Post subject: Pulling info from a database and looping through it |
|
|
Hello,
I hope I don't get flamed for this, I searched around and saw a few things relating to my question, but I do not fully understand it yet.
Lets say that I have a MySQL database of users and I want to print a table of them...
I would usually do (without using smarty)
Code: |
$db_query = "SELECT * FROM USERS";
$db_result = mysql_query($db_query);
while($db_row = mysql_fetch_object($db_result)) {
echo "$db_row->username<br>";
}
|
Now how do I loop through results in a template file? Again, sorry if this has been clearly posted elsewhere, I couldn't find anything that I could fully understand in my searches - I'm new at this If anybody knows of a link/tutorial that explains this clearly, please post it.
Thanks! |
|
Back to top |
|
iusefedora Smarty Rookie
Joined: 04 Jan 2005 Posts: 6
|
Posted: Tue Jan 04, 2005 1:45 am Post subject: |
|
|
I've got...
Code: |
$db_query = "SELECT * FROM users";
$db_result = mysql_query($db_query);
while($db_row = mysql_fetch_object($db_result)) {
$username = $db_row->user;
}
$smarty->assign('user', $username);
$smarty->display('index.tpl');
|
with this as the template file:
Code: |
{foreach from=$user item=user}
{$user}<br>
{/foreach}
|
and it only displays the first user |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Jan 04, 2005 8:24 am Post subject: |
|
|
you have to put all usernames in an array and assign that:
Code: |
$db_query = "SELECT * FROM users";
$db_result = mysql_query($db_query);
$users = array();
while($db_row = mysql_fetch_object($db_result)) {
$users[] = $db_row->user;
}
$smarty->assign('users', $users);
$smarty->display('index.tpl');
|
Code: |
{foreach from=$users item=user}
{$user}<br>
{/foreach}
|
|
|
Back to top |
|
iusefedora Smarty Rookie
Joined: 04 Jan 2005 Posts: 6
|
Posted: Tue Jan 04, 2005 2:36 pm Post subject: |
|
|
Perfect, that works great! |
|
Back to top |
|
carsten888 Smarty Rookie
Joined: 26 Feb 2005 Posts: 13 Location: Tilburg, Holland
|
Posted: Sat Feb 26, 2005 2:42 pm Post subject: what if you want to read more then one colum |
|
|
what if you want to read more then one colum. say not just "user" but all the other colums as well. this is what I would do in PHP:
Code: | while ($row = mysql_fetch_array ($result)){
print ("$row[id] $row[name] $row[message]<br>");
}
|
this is how i would like to put it in a template:
Code: | {foreach from=$row item=thisrow}
{$thisrow.id} {$thisrow.name} {$thisrow.message}<br>
{/foreach} |
why is this not in the documentation? It seems such basic stuff... |
|
Back to top |
|
carsten888 Smarty Rookie
Joined: 26 Feb 2005 Posts: 13 Location: Tilburg, Holland
|
Posted: Thu Mar 31, 2005 10:51 am Post subject: the answer |
|
|
in the php file:
Code: | $all = array();
while($db_row = mysql_fetch_array($resultaat)) {
$all[] = $db_row;
}
$smarty->assign('all', $all); |
in the template file:
Code: | {foreach from=$all item=row}
{$row.id} {$row.naam} {$row.volgorde} {$row.bericht}<br>
{/foreach} |
the best thing about this is that you can reuse the same query. You can have more then 1 foreach-loop on the page, which is not quite that easy with a while-loop in php. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|