2015-04-27 126 views
1

我正在使用Smarty,PHP和MySQL。父类和子类别下拉菜单

对于懂得如何操作的人来说,这应该是非常容易的。我是一个noob,不知道。

尝试在下拉菜单中获取父级和子类别的水平导航栏。

当前代码仅显示父级,我需要子类别才能显示。

我不认为我有任何SQL来获取子邮件,并不真正知道如何编写它,所以也许有人可以提供帮助。

TABLE >>> CATEGORY 

    category_id name  parent_id 
    1   Parent 0 
    2   Parent 0 
    2   Parent 0 
    3   Child  1 
    4   Child  2 
    5   Child  2 
    6   Child  3 
    7   Child  3 

当前函数来获取分类

// list of all categories 
function getCategoriesList($include_subcats = false) { 

$where = ''; 

if ($include_subcats != false) { 
    $where = ' where parent_id = 0 '; 
} 
$list = 
getSqlResult(
    "select * from category $where ORDER BY parent_id ASC", 
    SQL_RESULT_ALL); 

return $list; 
} 

模板代码显示菜单

{foreach name=CategoriesList from=$CategoriesList item=i} 
<li class="dropdown "><a href="/{$i.category_filename}" class="dropdown-toggle" data-toggle="dropdown">{$i.category_name}<b class="caret"></b></a 
</li>{/foreach} 
+0

有做这几个方式。我回答了这个问题希望你能明白的方式 –

+0

你似乎有重复的行 – Strawberry

回答

0

你可以做到这一点,返回功能

首先我们将编写的父母,因为他们的catid是0

   function categori($upcatid=0, $satir=0, $bosluk=""){ 
      // $upcatid is 0 for began that means is we will see the all parent 
       $data["result"]= $categori->kategori($upcatid);// i got my all cats from db 
       foreach ($data["result"] as $key => $kategorim) { 
     // lets look at the cats parent categories parent_id are 0 if categori parent is not 0 it main the categori have parent! so we are going to return the function and call the parent firstly 

       if(!isset($disabled)) 
        $disabled=''; 
       echo '<option value="'.$kategorim["id"].'" '.$disabled.' >'.str_repeat("->>", $satir).">$bosluk ".$kategorim["catname"].'</option>'; 
    categori($kategorim["id"], $satir+1); // now get the categori id and retun the function 
    //call the all childs of the cat. 

       } 
      } 

我写的所有类别中选择选项

if($upcatid==0) 
echo ' <li class="dropdown"> 
       <a data-toggle="dropdown" class="dropdown-toggle" href="#">'.$kategorim["catname"].' <b class="caret"></b></a> 

'; 
elseif($upcatid!==0){ 
echo ' <li class="dropdown-submenu"> 
        <a href="#">'.$kategorim["catname"].'</a> 
        <ul class="dropdown-menu"> 
        <li><a href="#">'.categori($kategorim["id"], $satir+1).'</a></li> 

        </ul> 
       </li>'; 


echo '</li>'; 

所有你需要修改你的项目

+0

嗨艾哈迈德。感谢您的回答。不知道如何在我的Smarty模板中完成这项工作。我粘贴它,但它不起作用。 –

+0

它不起作用,因为您需要为您的项目修改此代码。如果你没有理解树猫的话,那么只需看看bootstrap multi下拉的例子。你会修改你的项目 –

+0

不知道如何修改。我需要一个完整的解决方案来理解如何使用Smarty模板进行这项工作。不应该在Smarty模板中使用这样的PHP。 –