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

looping problems

 
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 -> General
View previous topic :: View next topic  
Author Message
rms
Smarty n00b


Joined: 05 Sep 2003
Posts: 2

PostPosted: Fri Sep 05, 2003 8:55 pm    Post subject: looping problems Reply with quote

[php:1:e4f0ed8a3a]<?php
$r_matches = mysql_query("SELECT field FROM table1");

while ($matches = mysql_fetch_assoc($r_matches))
{
$r_players = mysql_query("SELECT field FROM table2 WHERE id='{$matches['id']}'");

while ($players = mysql_fetch_assoc($r_players))
{
print $players['name'].'<br />';
}
}
?>[/php:1:e4f0ed8a3a]

how should the smarty-template look to function similar to the php example above?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Fri Sep 05, 2003 9:33 pm    Post subject: Re: looping problems Reply with quote

Hi rms,

there are many things you can do to Smarty-ize this, but the simplest is probably the following. Here, you still use PHP to do the heavy lifting of your application mechanics--in this case, your db access:[php:1:b8305f3e0e]<?php
$r_matches = mysql_query("SELECT field FROM table1");

while ($matches = mysql_fetch_assoc($r_matches))
{
$r_players = mysql_query("SELECT field FROM table2 WHERE id='{$matches['id']}'");

while ($players = mysql_fetch_assoc($r_players))
{
$out[] = $players;
}
}

$smarty = new Smarty;
$smarty->assign('players', $out);
?>[/php:1:b8305f3e0e]

Your template is thus very basic:

Code:
{foreach from=$players item=player}
    {$player.name}<br/>
{/foreach}

You could also use a {section} block instead of {foreach}:
Code:
{section name=i loop=$players}
    {$players[i].name}<br/>
{/section}


Alternatives include building a plugin template function or using a custom resource to provide the data.

The main point with the example above is that the template does not in any way participate in the production of the data--it is only repsonsible for displaying whatever prepared data is passed to it and that is all. That separation is one of the better reasons to use Smarty, IMHO.

HTH
Back to top
View user's profile Send private message
rms
Smarty n00b


Joined: 05 Sep 2003
Posts: 2

PostPosted: Fri Sep 05, 2003 9:59 pm    Post subject: Wow! Reply with quote

Wow! Big thanks for replying so fast boots!
I feel pretty stupid now though. Pretty simple solution. I guess I didn't put much thought into it before I asked. Embarassed

Anyway, case closed. Thanks again! Wink
Back to top
View user's profile Send private message
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 -> General 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