[英]php, jquery three level dropdown menu
I have a menu that uses PHP generated HTML to create jQuery dropdown menu. 我有一个菜单,该菜单使用PHP生成的HTML创建jQuery下拉菜单。 The information used to build the menu is coming from a database table of categories:
用于构建菜单的信息来自以下类别的数据库表:
cats (db table) 猫(db表)
cat_id | cat_name | cat_parent | cat_short __________________________________________ 1 | Home | 0 | home __________________________________________ 2 | Games | 1 | games __________________________________________ 3 | Checkers | 2 | checkers
The code that I have built so far only allows for 2 levels in the menu, and won't add another dropdown for a 3rd level (ie Checkers): 到目前为止,我已构建的代码仅允许菜单中包含2个级别,而不会为3级添加另一个下拉列表(即Checkers):
<ul class="dropdown">
<?php
$sql ='SELECT * FROM cats WHERE cat_parent = 0';
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result)){
$cats[] = array(
'id' => $row['cat_id'],
'name' => $row['cat_name'],
'parent' => $row['cat_parent'],
'short' => $row['cat_short'],
'subs' => array()
);
}
foreach($cats as $cat){
$sql = 'SELECT * FROM cats WHERE cat_parent = '.$cat['id'];
$result = $db->sql_query($sql);
while($row = $db->sql_fetchrow($result)){
$cat['subs'][] = array(
'id' => $row['cat_id'],
'name' => $row['cat_name'],
'parent' => $row['cat_parent'],
'short' => $row['cat_short']
);
}
$menu[] = $cat;
}
foreach($menu as $cat){ ?>
<li class="ui-state-default"><a class="transition" href="index.php?mode=<?php echo $cat['short']; ?>"><?php echo $cat['name']; ?></a>
<?php if($cat['subs'] != '0'){ ?>
<ul class="sub_menu">
<?php foreach($cat['subs'] as $sub){ ?>
<li class="ui-state-default"><a class="transition" href="index.php?mode=<?php echo $sub['short']; ?>"><?php echo $sub['name']; ?></a></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
How can I rewrite this (or write a function) to be able to utilize 3 layers? 如何重写此(或编写一个函数)以能够利用3层? I only need the PHP loop necessary, as I can easily manipulate the list item elements with jquery to perform the actual drop down.
我只需要必要的PHP循环,因为我可以轻松地使用jquery操作列表项元素以执行实际的下拉列表。
The following code (untested, sorry) may do the trick for any number of levels: 以下代码(未经测试,很抱歉)可以在任何数量的级别上实现成功:
<?php
# a key-value list of all cats by their IDs:
$all_cats = array();
# the final list of cats we want:
$cats = array();
$sql ='SELECT * FROM cats';
$result = $db->sql_query($sql);
# fetch all cats
while($row = $db->sql_fetchrow($result)){
$all_cats[$row['cat_id']] = array(
'id' => $row['cat_id'],
'name' => $row['cat_name'],
'parent' => $row['cat_parent'],
'short' => $row['cat_short'],
'subs' => array()
);
}
# group cats by parent (note reference & to $cat) - we are dealing with the same object
foreach ($all_cats as $id => &$cat) {
$parent = $cat['cat_parent'];
if ($parent == 0) {
$cats[] = $cat;
} else {
$all_cats[$parent]['subs'][] = $cat;
}
}
At the end, $cats
will contain an array of first-level cats, with its subs
filled properly. 最后,
$cats
将包含一级猫阵列,其subs
正确填写。
Note that if your table gets too large, this will take a lot of memory, but at least it will hit the database only once. 请注意,如果您的表太大,这将占用大量内存,但是至少它将仅一次访问数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.