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

foreach in PHP & section in Smarty TPL does not have the

 
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
gomezo
Smarty n00b


Joined: 30 Oct 2003
Posts: 1

PostPosted: Thu Oct 30, 2003 11:07 am    Post subject: foreach in PHP & section in Smarty TPL does not have the Reply with quote

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
View user's profile Send private message
metheorn
Smarty Rookie


Joined: 22 Aug 2003
Posts: 14

PostPosted: Thu Oct 30, 2003 4:50 pm    Post subject: Reply with quote

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
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri Oct 31, 2003 8:45 am    Post subject: Reply with quote

Using {foreach} instead of {section} should help too.
http://smarty.php.net/manual/en/language.function.foreach.php
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