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

It worked !....not sure I understand why

 
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
mgm_03
Smarty Rookie


Joined: 07 Dec 2004
Posts: 23

PostPosted: Tue Dec 14, 2004 10:45 pm    Post subject: It worked !....not sure I understand why Reply with quote

Beginner smarty user here...I wanted to display a set of links (grouped by category) from mysql. I found this thread below and used it as a reference for my needs.

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=215&highlight=grouping+database+mysql+results

Code:

$result = mysql_query("SELECT cat, name, url, cats.catID as cid FROM cats LEFT JOIN links ON links.catID=cats.catID ORDER BY cat");
      $var = " ";
      while ($row = mysql_fetch_array($result)) {
         if ($var != $row['cid']) {
            $var = $row['cid'];
            $cats[$row['cid']] = $row['cat'];
         }
         $links[$var][] = array('name'=>$row['name'], 'url'=>$row['url']);
                
      }
       $smarty->assign('cats', $cats);
       $smarty->assign('links', $links);


$smarty->display('index.tpl');


Code:
{foreach from=$cats key=cid item=cat}
   {$cat}<br />
   {foreach from=$links[$cid] item=url}
      <a href="{$url.url}">{$url.name}</a><br />
   {/foreach}
{/foreach}


What I don't understand is why can't {section} be used instead of {foreach}?

Suggestion: Can this code or something similar be put in the doc? It seems like a very practical example that people can learn from...especially about how to create Smarty-friendly arrays. I would have never figured out how to do this on my own.

It appears that 'item' in a {foreach} loop is similar to 'name' in a {section} loop because both can be arbitrary words (or "strings" to be more precise) that have no inherent value.
Back to top
View user's profile Send private message
boots
Administrator


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

PostPosted: Wed Dec 15, 2004 2:18 am    Post subject: Reply with quote

Why? Because {section} requires indexes that are 0-based and sequential where this array ($cats) have indexes that are derived from a database table (cats.catID as cid) and therefore will almost certainly not meet the requirements of {section}. Foreach also has a somewhat simpler syntax since you get the record as a variable in each iteration instead of having to write an expression that drills down into your recordset via the name variable.

And yes, you can choose any string value for 'item' -- it simply determines the template variable name for the current recordset. You are permitted to choose it so that it does not interfere with other template variables or other foreach loop items if you are nesting them.

HTH
Back to top
View user's profile Send private message
mgm_03
Smarty Rookie


Joined: 07 Dec 2004
Posts: 23

PostPosted: Wed Dec 15, 2004 3:47 am    Post subject: Reply with quote

Yes, it does. ... thanks very much.
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 -> 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