gpt4 book ai didi

PHP Mysql子菜单数组

转载 作者:行者123 更新时间:2023-11-28 06:33:40 25 4
gpt4 key购买 nike

你能帮忙制作吗使用 Mycode Cat.php 的下拉菜单(子菜单)文件包含在标题中 菜单

/////// ( Home )//////////
include_once("./includes/config.php") ;

$mysqlQuery=new mysqlQueryClass();

$query="SELECT `catagory_id`, `name` FROM `catagories` WHERE `level` = 1";
$mysqlQuery->mysqlQueryWOF($query);

while (list($catagory_id,$name) = mysql_fetch_array($mysqlQuery->result))
{
$cats .= "<b>::</b> <a href='cat-$catagory_id,start-0'>$name</a><br>";
}



///(SubMenu level 2 About From Home)//////
$mysqlQuery=new mysqlQueryClass();

$query="SELECT `catagory_id`, `name` FROM `catagories` WHERE `level` = 2";
$mysqlQuery->mysqlQueryWOF($query);

while (list($catagory_id,$name) = mysql_fetch_array($mysqlQuery->result))
{
$cats .= "<b>::</b> <a href='cat-$catagory_id,start-0'>$name</a><br>";
}

图片:http://s12.postimg.org/v2yxhk8gt/2016_01_17_06_21_08.png级别 1 = 菜单级别 2 = 子菜单

最佳答案

$mysqlQuery=new mysqlQueryClass();

$query="SELECT a.`catagory_id` parent_cat_id, a.`name` parent_cat, b.category_id, b.name
FROM `catagories` a
LEFT JOIN `catagories` b on b.level = 2 and a.category_id = b.category
WHERE a.`level` = 1";

$mysqlQuery->mysqlQueryWOF($query);
$id2name = array();
$cat_tree = array();

while (list($parent_id,$parent,$cat_id,$cat) = mysql_fetch_array($mysqlQuery->result)) {
$id2name[$parent_id] = $parent;
$id2name[$cat_id] = $cat;
$cat_tree[$parent_id][] = $cat_id;
}

foreach ($cat_tree as $parent_id => $child_ids) {
$name = $id2name[$parent_id];
$cats .= "<b>::</b> <a href='cat-$parent_id,start-0'>$name</a><br>";

foreach ($child_ids as $id) {
if (is_null($id)) continue;
$child = $id2name[$id];
$cats .= "<b>::::</b> <a href='cat-$id,start-0'>$child</a><br>";
}
}

关于PHP Mysql子菜单数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34834891/

25 4 0