父级和子类别下拉菜单

时间:2015-04-27 22:35:08

标签: php mysql smarty

我正在使用Smarty,PHP和MySQL。

对于了解如何执行此操作的人来说,这应该很容易。我是个菜鸟,不知道。

尝试在下拉列表中获取包含父级和子类别的水平导航栏。

当前代码仅显示父代,我需要在子代下显示子类别。

我不认为我有任何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}

1 个答案:

答案 0 :(得分: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>';

您需要修改项目