RE: [SMARTY] Recursion, include, and {section}

From: Date: Wed, 31 Jul 2002 08:42:37 +0000
Subject: RE: [SMARTY] Recursion, include, and {section}
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5531@lists.php.net to get a copy of this message
> >>KY> Problem is, the template contains a named {section}, and > >>KY> the recursive include is inside that {section}. This results in > >>KY> nested sections with the same name. This is futzing up the > >>KY> loops, as the Smarty manual predicts. > >>I managed to fix this using foreach and a local variable in > >>my{include}. Something like: > >>{foreach from=$TREE_ARRAY item=TREE} > >>{include file=$SELF TREE_ARRAY=$TREE_ARRAY.tree} > >>{/foreach} > >>($TREE_ARRAY.tree contains each time exactly the same structure, > >>of course) > > I'll try it, but it seems on its face to be no different. The manual > > says that nested {foreach} loops need distinct names, too, and you > > haven't done that here. I was facing the same problem about a week ago. There what I came up with: Database structure ------------------ CREATE TABLE menu ( id int(11) NOT NULL auto_increment, parent_id int(11) NOT NULL default '0', caption text NOT NULL, KEY id (id) ) TYPE=MyISAM; index.php --------- $tpl->new Smarty; $tpl->display('menu.tpl'); menu.tpl: --------- {php} /* I use a special query function which check if the query was ok and if not then echoes the query's SQL statement and also why is it not OK. $return_type can be set to "array". In that case, instead of returning mysql resource identifier, the function returns array, which contains all the query rows. Makes my life easier. ;-) SYNTAX: sql_query($sql, $return_type); */ {php} if (!$_GET[menuitem_id]) # if no menuitem has been clicked: # Get root's children: $tree = up_one_level(1, false); else { $resParentId = sql_query ("SELECT parent_id FROM menu WHERE id='$_GET[menuitem_id]'"); if (!mysql_num_rows($resParentId) > 0) die ("<br>->Couldn't get initial parent_id!"); list($parent_id) = mysql_fetch_array($resParentId); # If menuitem was selected then we need to get selected menuitem's brothers and sisters. $selected_menubranch = sql_query ("SELECT * FROM menu WHERE parent_id='$id'", "array"); # This little 'innocent' row starts a myriad of processes :) $tree = up_one_level($id, $selected_menubranch); } /* You give it selected menuitem's ID and it's brothers * ...and it will dig up all it's parents and uncles and aunts */ function up_one_level($this_id, $this_elements) { // Get parent_id $parent_id = get_parent_id($this_id); if ($parent_id === false) return; // Get parent's children $children_elements = get_children($parent_id); $arr = $children_elements; // Make an element called children and // assign an array we got from argument to it. $arr['node'.$this_id][children] = $this_elements; // Include upper folder if parent was not top level element. // This involves calling this function again. if ($parent_id > 0) $arr = up_one_level($parent_id, $arr); return $arr; } function get_parent_id ($id) { $resParentId = sql_query ("SELECT parent_id FROM menu WHERE id='$id'"); if (mysql_num_rows($resParentId) == 0) return false; list($parent_id) = mysql_fetch_array($resParentId); return $parent_id; } function get_name ($id) { if ($id == 0) return "{root}"; $resName = sql_query ("SELECT caption FROM menu WHERE id='$id'"); if (mysql_num_rows($resName) == 0) return "no name for id=$id!"; list($caption) = mysql_fetch_array($resName); return $caption; } function get_children ($parent_id) { $resChildren = sql_query ("SELECT * FROM menu WHERE parent_id='$parent_id'"); if (!mysql_num_rows($resChildren) > 0) die ("!"); while ($field = mysql_fetch_assoc ($resChildren)) { $arr['node'.$field[id]] = $field; } return $arr; } $this->assign('tree', $tree); {/php} {strip} <table width=100%> <tr> <td> {include file="menu_node.tpl" nodes=$tree} </td> </tr> </table> {/strip} ------end of menu.tpl------ menu_node.tpl ------------- <table border="0" cellspacing="0" cellpadding="0" width=100%> {foreach item=node from=$nodes} <tr> <td valign=center> {if $node.children} {/if}&nbsp; </td> {if $node.id == $smarty.get.menuitem_id} <td colspan=3> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td class="hilite" align="center" valign=center> <a href="?menuitem_id={$node.id}">{$node.caption}</a> </td> <td class="lingid hilite" align="right"> {if $admin} [administrative buttons] {/if} </td> </tr> </table> </td> {else} <td class=lingid colspan=3 width=100%> <table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td align="left"> <a href=?menuitem_id={$node.id}>{$node.caption}</a></td> <td align="right"> {if $admin} [administrative buttons] {/if} </td> </tr> </table> </td> {/if} </tr> {if $node.children} <tr> <td width=10></td> <td>{include file="menu_node.tpl" nodes=$node.children}</td> </tr> {/if} {/foreach} </table>

« previous php.smarty.general (#5531) next »