|
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 |
gomezo Smarty n00b
Joined: 30 Oct 2003 Posts: 1
|
Posted: Thu Oct 30, 2003 11:07 am Post subject: foreach in PHP & section in Smarty TPL does not have the |
|
|
Hi,
i'm new in Smarty template and perhaps it's not a bug...
I have a Url class with a fromLinks array, i populate this array like this:
Code: | function addFromLink($id,$url)
{
$this->fromLinks[$id] = $url;
} |
with these datas (ex 5 rows) { 4=>"http://...",5=>"http://..."}
where the $id is an int coming from an sql database and the url is an url...
when i do this in php for debug:
Code: | foreach($url->fromLinks as $key => $value)
{
echo "array: ".$key.":".$value."<br>";
} |
it works well i have 5 rows but when pass $url->fromLinks as fromLins to Smarty template with this code:
Code: | {section name=link loop=$fromLinks}
<tr><td>{$link}:{$fromLinks[link]}</td></tr>
{/section} |
the result is 4 + 5 rows , how can i do to avoid smarty not to loop on the 'empty' rows ? Smaty seems to loop on 0,1,2,3,4 keys but they does not exist...
Thanks for your help,
Julien |
|
Back to top |
|
metheorn Smarty Rookie
Joined: 22 Aug 2003 Posts: 14
|
Posted: Thu Oct 30, 2003 4:50 pm Post subject: |
|
|
Hi,
The smarty section begin at id 0 of the table and loop x times (number of items in the table). So for your example, for 5 lines starting at id 4, it'll will go throw 0,1,2,3,4 and you'll not see the end ...
You say that your datas come from an SQL Database.
Perhaps it's simplier to do like this :
PHP :
Code: | $SqlQuery="select Id, Url from ...";
$fromLinks = $db->getall($SqlQuery,DB_FETCHMODE_ASSOC);
$smarty->assign('fromLinks ', $fromLinks );
|
Template :
Code: | {section name=link loop=$fromLinks}
<tr><td>{$fromLinks[link].Id} : {$fromLinks[link].Url}</td></tr>
{/section}
|
Hope this will help you |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
|
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
|