[英]Dynamic Drop Down Menu and submenu php mysql
I have a menu that needs to be created dynamically from the database. 我有一个菜单,需要从数据库中动态创建。 need to have menu and submenu 需要有菜单和子菜单
for example (wht i want): 例如(我想要):
<ul class="dropdown">
<li><a href="#">Link 1</a> </li>
<li><a href="#">Link 2</a> </li>
<li><a href="#">Link 3</a> </li>
<li><a href="#">Link 4</a>
<ul class="sub_menu">
<li><a href="#">Link 4 - 1</a></li>
<li><a href="#">Link 4 - 2</a></li>
<li><a href="#">Link 4 - 3</a></li>
</ul>
</li>
<li><a href="#">Link 5</a></li>
<li><a href="#">Link 6</a> </li>
</ul>
below is the code i used for menu using function i want submenu as well 以下是我用于菜单的代码,该菜单也使用我想要子菜单的功能
function listMenu(){
$ans = $this->select("cat_id,cat_name","s_category","1");
if(is_array($ans)){
foreach($ans as $val){
echo
"<li><a href=\"post-summary.php?cid=$val[0]\" >$val[1]</a></li>";
}
}
else{
echo "";
}
}
database is as follows:- 数据库如下:
CREATE TABLE IF NOT EXISTS `s_category` (
`cat_id` int(11) NOT NULL AUTO_INCREMENT,
`cat_name` varchar(15) NOT NULL,
`cat_uid` int(2) NOT NULL,
`cat_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`cat_parent` int(11) DEFAULT '0',
`cat_sort` int(11) DEFAULT NULL,
`cat_delete` int(1) DEFAULT '0',
PRIMARY KEY (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
as far as menus are concerned storing the submenus as a comma seperated values in the database would come handy. 就菜单而言,将子菜单存储为数据库中的逗号分隔值会很方便。
ie consider a table with id,menus,submenus(several submenus seperated by a comma). 例如,考虑一个具有id,menus,submenus(用逗号分隔的子菜单)的表。
function listMenu(){
$ans = $this->select("menus,submenus");
if(is_array($ans)){
foreach($ans as $val){
echo "<li><a href=\"post-summary.php?cid=$val[0]\" >$val[0]</a>"; //menus
$submenu = explode(",",$val[1]);
foreach($submenu as $values){
echo "<li><a href=\"post-summary.php?cid=$val[0]&proname=$values\" >$values</a></li>"; // submenus
}
echo "</li>";
}
}
}
try this 尝试这个
$con = db_connect();
$str = "select menus,submenus from your_tabel ";
$result = mysql_query($str,$con);
while($arr = mysql_fetch_array($result))
{
echo "<li><a href=\"post-summary.php?cid=$arr[0]\" >$arr [0]</a>"; //menus
$submenu = explode(",",$arr [1]);
//make sure you have several sub-menus else use a if condition here to avoid foreach error
foreach($submenu as $values){
echo "<li><a href=\"post-summary.php?cid=$arr[0]&proname=$values\" >$values</a></li>"; // submenus
}
echo "</li>";
}
this works fine for me just tested now 这对我来说很好,刚刚测试
comment if working. 如果可以发表评论。 comment if not-working. 如果无效,请发表评论。
You need to have a column, let we name it parentId
in the s_category
table. 您需要有一列,让我们在s_category
表中将其命名为parentId
。
Example values in there: 其中的示例值:
catId,catName,parentId
1,Vehicles,null
2,Planes,null
3,Cars,1
4,Bicycles,1
So your structure will be like: 因此,您的结构将如下所示:
- Vehicles
- Cars
- Bicycles
- Planes
You'll need to get the data like this ( to be considered as pseudocode ): 您需要获取如下数据( 被视为伪代码 ):
$topLevelItems = (select catId, catName from s_category where parentId is null)
foreach($topLevelItems as $item) {
echo '<a href="...">$item["catName"]</a>';
$subItems = (select catId, catName from s_category where parentId=$item['catId'])
foreach($subItems as $subItem) {
echo '<a href="...">$subItem["catName"]</a>';
}
}
You need to re-write the SQL part to match your case. 您需要重新编写SQL部分以匹配您的情况。 This will work if you have just one inner level, eg no sub-sub-menus. 如果您只有一个内部级别,例如没有子子菜单,这将起作用。 If you plan to use sub-sub-menus you'll need recursive function. 如果您打算使用子菜单,则需要递归函数。
created your database and write function with code and change parameters 用代码和更改参数创建数据库并编写函数
function getAllFrom($field, $table,$where = NULL, $and = NULL , $orderfield, $ordering = "DESC") {
global $con;
$getAll = $con->prepare("SELECT $field FROM $table where $where $and ORDER BY $orderfield $ordering");
$getAll->execute();
$all = $getAll->fetchAll();
return $all;}
<ul>
<?php
$allCats = getAllFrom("*", "categories", "parent = 0", "" , "cat_id", "ASC");
foreach ($allCats as $cat) { ?>
<li>
<?php
echo '<a href="categoreis.php?pagename=' . str_replace(' ', '-', $cat['name_cat']) . '&pageid=' . $cat["cat_id"] . '">
' . $cat['name_cat'] . '
</a>';
?>
<ul class="sub-menu[enter image description here][1]">
<?php
$catsub = $cat['cat_id'];
$SubCats = getAllFrom("*", "categories", "parent = {$catsub}", "" , "cat_id", "ASC");
foreach ($SubCats as $sub) { ?>
<li>
<?php
echo '<a href="categoreis.php?pagename=' . $sub['name_cat'] . '&pageid=' . $sub["cat_id"] . '">
' . $sub['name_cat'] . '
</a>';
?>
</li>
<?php }
?>
</ul>
</li>
<?php }
?>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.