0

我的问题我有这个代码没完没了的菜单

        $result=db::query_tpv("SELECT * FROM  tpv_link");
while($row = db::fetch_array($result,'assoc'))
 {

$menu[$row['parent_id']][$row['id_link']] = $row['name_link'];

}
function make_menu($parent_id)
{global $menu;if (!isset($menu[$parent_id])) {return;}
$nodes = $menu[$parent_id];
echo '<ul>';
foreach($nodes as $menu_id => $menu_name)
{
echo '<li>'.$menu_name ;
make_menu($menu_id) ;
echo '</li>';
}
echo '</ul>';
}

这个 {0|make_menu} 也可以在 samrty 中工作,但我想取消这个函数并在 smarty 中更改为这个(定义 $menu)

{function name=menu level=0}
        <ul>
            {foreach $data as $items}
                <li>
                    <a href="{$items.url}">
                        {$items.title}
                    </a>
                    {if is_array($items.submenu)}
                        {menu data=$items.submenu level=$level+1}
                    {/if}
                </li>
            {/foreach}
        </ul>
    {/function}
{menu data=$menuItems}

在此如何在 Smarty 中 foreach 使用递归函数创建的数组变量, 但它不起作用.....

4

1 回答 1

0

Hello I Find Solution after 3 days

Here

------------First You make Query You table---------- Ex:

    $result=db::query_tpv("SELECT * FROM  tpv_link");
  while($row = db::fetch_array($result,'assoc'))
 {

 $menu[]=$row ;
 }

------------Secound Uses This Function (For make Your pyrimd array)---------- ------------ Note change id,parentid----------

   function buildTree(array $elements, $parentId = 0) {
$branch = array();

foreach ($elements as $element) {
    if ($element['parent_id'] == $parentId) {
        $children = buildTree($elements, $element['id_link']);
        if ($children) {
            $element['children'] = $children;
        }
        $branch[] = $element;
    }
}

return $branch;
}

-------------------Before Smarty Template--------------------

$tree = buildTree($menu);//$menu array all data From Query
$smarty->assign('menu',$tree);

------------------In smarty Template-------------------------

   {function name=menu level=0}
        <ul>
            {foreach $data as $items}
                <li>
                    <a href="{$items.url}">
                        {$items.name_link}
                    </a>
                    {if $items.children}
                        {menu data=$items.children level=$level+1}
                    {/if}
                </li>
            {/foreach}
        </ul>
    {/function}


    {menu data=$menu}
于 2013-02-08T04:57:08.727 回答