PHP Mysql子菜单数组

时间:2016-01-17 03:46:57

标签: php mysql css submenu

你能帮忙吗? 使用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 =子菜单

2 个答案:

答案 0 :(得分:0)

您可以像以下一样创建菜单和子菜单:

$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>";   

    $query="SELECT `catagory_id`, `name` FROM `catagories` WHERE `level` = 2 AND main_category = ". $catagory_id ;
     $mysqlQuery->mysqlQueryWOF($query);
        while (list($scatagory_id,$sname) = mysql_fetch_array($mysqlQuery->result)) 
       {                   
      $cats .= "<b>::</b> <a href='cat-$scatagory_id,start-0'>$sname</a><br>";   

      }
}

答案 1 :(得分:0)

$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>";
  }
}