Php 谜题:如何组织动态分层类别菜单
我正在尝试在我的电子商务网站上开发一个层次菜单,其中动态显示我的类别,以便添加更多类别,而无需触摸代码 所以我就这样组织了我的数据库,我的分类有3个不同的等级: 我有3个不同的等级:Php 谜题:如何组织动态分层类别菜单,php,model-view-controller,menu,categories,Php,Model View Controller,Menu,Categories,我正在尝试在我的电子商务网站上开发一个层次菜单,其中动态显示我的类别,以便添加更多类别,而无需触摸代码 所以我就这样组织了我的数据库,我的分类有3个不同的等级: 我有3个不同的等级: 第一名:信息学[id=1]配件[2]设备[3]高保真 第二级:硬件[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]W
- 第一名:信息学[id=1]配件[2]设备[3]高保真
- 第二级:硬件[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]
<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循环,但我自己无法解决
如果有人想帮助解决这个问题,他是非常受欢迎的:)关于将数据存储到数据库中的方法(不是MVC部分,因为我不倾向于使用框架,我经常构建自己的框架),我推荐这篇非常好的文章:
它讨论了当您拥有分层数据时将数据存储到数据库中的方法。根据输出,我更喜欢的方法是邻接列表或遍历方法。由您选择您认为最有效的方法:)好的,谢谢您的帖子,但我找到了解决问题的方法,这里是我的个人解决方案: 因此,我首先检索数组中的所有类别:“allCategories” 然后,当我构建菜单时,通过while循环和if条件,我得到了我想要的: 所以我们有,对于每个类别
- 类别['cat_id'],它是类别的id
- 类别['cat_title']猫的名称
- 类别['cat_order']类别的等级
- 类别['cat_parentkey']我的类别的父级id(母板的父级:硬件)
$token1=TRUE; $2=真; foreach($allCategories作为$Categories1){//我们将检查所有的猫::on fait defiler toutes les catégories if($Categories1['cat_order']==1){//if的秩1::si le rang de la categories est 1 $key=$Categories1['cat_id'];//我们保存它的id echo“
- ”;//这是打印列表的loyaout,而/li则更进一步::在fait la mise的页面上,您可以看到一个列表,即le/li venant plus bas foreach($categories as$categories 2test){//我们将检查是否至少有一个类别的排名较低,否则我们不会打印菜单旁边有一个难看的条带的
- :在va测试仪上,您可以在您的菜单中找到您的姓名和地址
如果($Categories2test['cat\u order']==2和$Categories2test['cat\u parentkey']==key和$token1==TRUE){//我们用一个标记做一个测试,一旦我们在里面做了一个循环,它会告诉我们至少有一只猫的等级较低::事实上,它没有测试,它没有标记,它有一个标记,一个标记,一个标记,一个标记
echo“
- ”;
foreach($Categories作为$Categories3test的所有类别){
如果($Categories3test['cat\u order']==3和$Categories3test['cat\u parentkey']===$key2和$token2==TRUE){
echo“
- ”foreach($categories作为$Categories3的所有类别){
如果($Categories3['cat_order']==3和$Categories3['cat_parentkey']==$key2){
$key3=$Categories3['cat_id'];
回音“
- ”; 回声“ ”; } } 回声“
”;
}
$2=真;
}
回声“
- ”;//只有在有地狱猫的情况下,才会打印我们菜单的布局::在菜单的形式上,没有地狱分类的标签。
foreach($allCategories as$Categories2){//我们再次检查所有的猫::on fait défiler les catégories
如果($Categories2['cat_order']==2和$Categories2['cat_parentkey']==$key){//如果至少有一个秩2,那么….::是一个1范围2的值。。。
$key2=$Categories2['cat_id'];
回音“
$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 }
- ”;//这是打印列表的loyaout,而/li则更进一步::在fait la mise的页面上,您可以看到一个列表,即le/li venant plus bas foreach($categories as$categories 2test){//我们将检查是否至少有一个类别的排名较低,否则我们不会打印菜单旁边有一个难看的条带的