|
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 |
rms Smarty n00b
Joined: 05 Sep 2003 Posts: 2
|
Posted: Fri Sep 05, 2003 8:55 pm Post subject: looping problems |
|
|
[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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Fri Sep 05, 2003 9:33 pm Post subject: Re: looping problems |
|
|
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 |
|
rms Smarty n00b
Joined: 05 Sep 2003 Posts: 2
|
Posted: Fri Sep 05, 2003 9:59 pm Post subject: Wow! |
|
|
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.
Anyway, case closed. Thanks again! |
|
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
|