难题:如何组织一个动态的分层分类菜单



我试图在我的电子商务网站开发一个分层菜单,其中我的类别是动态显示的,以便添加更多的类别,而无需触摸代码。

我这样组织我的数据库,用3个不同的等级来划分我的类别:

我有三个不同的等级:

  • 第一名: Informatics [id=1] Accessories[2] vêtements[3] Hifi ..

  • 第二等级:硬件[parent_key = 1] [id = 10]软件[parent_key = 1] [id = 11]男人[parent_key = 3] [id = 30] . .

  • 第三名:主板[parent_key=10][id=100]处理器[parent_key=10][id=101] Windows7[parent_key=11][id=110]鞋子[parent_key=30][id=300] .

所以你已经明白了"parent_key"指的是我的类别abd的父id对于每个排名1的类别我有几个排名2的类别等等。

现在,我已经硬编码了我的菜单,像这样:

<div id="main_menu">
  <ul id="nav">
    <li class="current"><a href="<?php echo base_url();?>">Home</a></li>
    <li><a href="#">High Tech</a>
      <ul>
        <li><a href="#">Informatique</a>
          <ul>
            <li><a href="#">Hardware</a></li>
            <li><a href="#">Ecrans</a></li>
            <li><a href="#">Clavier</a></li>
            <li><a href="#">Souris</a></li>
            <li><a href="#">Imprimantes</a></li>
          </ul>
        </li>
        <li><a href="#">TV</a>
          <ul>
            <li><a href="#">LCD</a></li>
            <li><a href="#">Plasma</a></li>
            <li><a href="#">3D</a></li>
          </ul>
        </li>
        <li><a href="#">Appareils Photos</a></li>
        <li><a href="#">GPS</a></li>
        <li><a href="#">Smartphones</a></li>
        <li><a href="#">Lecteur MP3</a></li>
        <li><a href="#">Hi-Fi</a>
          <ul>
            <li><a href="#">Amplificateurs</a></li>
            <li><a href="#">Enceintes</a></li>
            <li><a href="#">Cables</a></li>
            <li><a href="#">Autres</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
  <br class="clear" />
</div>

我在MVC编码,我不知道确切地如何建立我的模型,我的控制器,和我的视图。我想我必须做一些if/else和foreach循环,但我不能自己弄清楚。

如果有人想帮忙解决这个问题,欢迎他来。

好的,谢谢你的帖子,但我找到了一个方法来处理我的问题,这里是我个人的解决方案:

所以我首先从检索数组中的所有类别开始:"allCategories"

然后,当我用while循环和if条件构建菜单时,我得到了我想要的:

对于每个类别,我们有

  • Category['cat_id']类别的id

  • Category['cat_title']猫的名字

  • Category['cat_order']类别的排名

  • Category['cat_parentkey']类别父类的id(主板父类:Hardware)

    $token1=TRUE;
    $token2=TRUE;
    foreach($allCategories as $Categories1){ //we are going to check all the cats :: on fait defiler toutes les catégories
        if($Categories1['cat_order']==1){ // if its rank 1 :: si le rang de la categorie est 1
            $key = $Categories1['cat_id']; // we save its ID
            echo '<li><a href="#">';
            echo $Categories1['cat_title'];
            echo '</a>'; // this is the loyaout to print the list, the /li comes further :: on fait la mise en page pour afficher la liste, le /li venant plus bas
            foreach($allCategories as $Categories2test){ // We gonna check if there is AT LEAST ONE categorie with an inferior rank, otherwise we do not print the <ul> which produce an ugly bar next to the menu :: on va tester si il existe AU MOINS UNE catégorie de rang inférieur, sinon on n'affiche pas de ul afin d'éviter une barre moche dans le menu
                if($Categories2test['cat_order']==2 AND $Categories2test['cat_parentkey']==$key AND $token1==TRUE){ // We do a test with a token which, once we do 1 loop inside, tell us there is at least one cat with an inferior rank :: on fait donc un test avec un token qui, une fois qu'on passe dedans 1 fois, nous dis qu'i'il y a donc au moins un rang inféireur
                    echo '<ul>'; // layout of our menu, is printed only if there is inferioir cats :: mise en forme du sous menu, ne s'affiche donc qu si il ya une categorie de ranf inferieur.
                        foreach ($allCategories as $Categories2){ // One again, we check all the cats :: on fait défiler les catégories
                            if($Categories2['cat_order']==2 AND $Categories2['cat_parentkey']==$key){ // If there is at least one of rank 2 so .... :: si il y en a 1 de rang 2 alors ...
                                $key2 = $Categories2['cat_id'];
                                echo '<li><a href="#">';
                                echo $Categories2['cat_title'];
                                echo '</a>';
                                foreach($allCategories as $Categories3test){
                                    if($Categories3test['cat_order']==3 AND $Categories3test['cat_parentkey']==$key2 AND $token2==TRUE){
                                        echo "<ul>";                                            foreach ($allCategories as $Categories3){
                                            if($Categories3['cat_order']==3 AND $Categories3['cat_parentkey']==$key2){
                                                $key3 = $Categories3['cat_id'];
                                                echo '<li><a href="#">';
                                                echo $Categories3['cat_title'];
                                                echo '</a>';
                                                echo "</li>";
                                            }
                                        }
                                        echo "</ul>";
                                        $token2=FALSE; 
                                    }
                                }
                            echo"</li>";
                            }
                        $token2=TRUE;
                        }
                    echo'</ul>';
                    $token1=FALSE; // We put our token to FALSE in order to avoid that loop for that particular rank1 category :: on met notre token à FALSE afin de ne plus refaire cette boucle pour cette catégorie de rang1
                }
            }
            echo "</li>"; 
        }
        $token1=TRUE; // We put the token to TRUE in order to do that loop again for the other rank 1 categorie :: on remet le token à 0 afin de repasser dans la boucle pour la catégorie de rang 1 suivante
    }
    

    ?>

关于将数据存储到数据库中的方式(不是MVC部分,因为我不倾向于使用框架,我经常构建自己的框架),我推荐这篇非常好的文章:

http://www.sitepoint.com/hierarchical-data-database/

它讨论了当你有分层数据时如何将数据存储到数据库中。我喜欢的方法,取决于输出是邻接表或遍历方法。你可以选择一个你认为最好的

最新更新