Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Pulling info from a database and looping through it

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
iusefedora
Smarty Rookie


Joined: 04 Jan 2005
Posts: 6

PostPosted: Tue Jan 04, 2005 1:31 am    Post subject: Pulling info from a database and looping through it Reply with quote

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 Smile If anybody knows of a link/tutorial that explains this clearly, please post it.

Thanks!
Back to top
View user's profile Send private message
iusefedora
Smarty Rookie


Joined: 04 Jan 2005
Posts: 6

PostPosted: Tue Jan 04, 2005 1:45 am    Post subject: Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Jan 04, 2005 8:24 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
iusefedora
Smarty Rookie


Joined: 04 Jan 2005
Posts: 6

PostPosted: Tue Jan 04, 2005 2:36 pm    Post subject: Reply with quote

Perfect, that works great!
Back to top
View user's profile Send private message
carsten888
Smarty Rookie


Joined: 26 Feb 2005
Posts: 13
Location: Tilburg, Holland

PostPosted: Sat Feb 26, 2005 2:42 pm    Post subject: what if you want to read more then one colum Reply with quote

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
View user's profile Send private message Visit poster's website
carsten888
Smarty Rookie


Joined: 26 Feb 2005
Posts: 13
Location: Tilburg, Holland

PostPosted: Thu Mar 31, 2005 10:51 am    Post subject: the answer Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP