2012-07-02 71 views
0

Hy,PHP - SQL制作子菜单的菜单

标题说明它,我想做一个子菜单的菜单。

因此,像这样:

  • parent1
    1. SUB2
      1. 分3
  • parent2

我有了这个已经工作,但是代码并不好看。

那么,我该如何改进我的代码,或者您是否知道它的错误,然后发布它。

这是代码:

<?php 

$le_content .= '<div id="main_menu">'; 


$sQuery_get_all_parents = " SELECT 
          menu.id, 
          menu.`name`, 
          menu.url, 
          menu.parent_id 
          FROM `menu` 
          WHERE 
          menu.actif = 1 AND 
          menu.parent_id = 0 
          ORDER BY 
          menu.volgorde ASC"; 
$rResultaat_get_all_parents = mysql_query($sQuery_get_all_parents) or die(mysqlError($sQuery_get_all_parents)); 

$menu_parents = array(); 
$menu_child_1 = array(); 
$counter = 0; 
$counter_child_1 = 0; 
$counter_child_2 = 0; 

while($row = mysql_fetch_assoc($rResultaat_get_all_parents)){ 
    $menu_parents[$counter]['id'] = $row['id']; 
    $menu_parents[$counter]['name'] = $row['name']; 
    $menu_parents[$counter]['url'] = $row['url']; 
    $menu_parents[$counter]['parent_id'] = $row['parent_id']; 
    $counter++; 
} 

$le_content .= '<ul id="main_menu_ul">'; 
foreach ($menu_parents as $menu_entry => $value) { 
    $le_content .= '<li><a href="'.ROOT_HREF.$value['url'].'" >'.$value['name'].'</a>'; 
    $sQuery_get_first_children = " SELECT 
            menu.id, 
            menu.`name`, 
            menu.url, 
            menu.parent_id 
            FROM `menu` 
            WHERE 
            menu.actif = 1 AND 
            menu.parent_id = ".$value['id']." 
            ORDER BY 
            menu.volgorde ASC"; 
    $rResultaat_get_first_children = mysql_query($sQuery_get_first_children) or die(mysqlError($sQuery_get_first_children)); 
    $row_count = mysql_num_rows($rResultaat_get_first_children); 

    if($row_count != false){ 
     $le_content .= '<ol>'; 
     while($row = mysql_fetch_assoc($rResultaat_get_first_children)){ 
      $menu_child_1[$counter_child_1]['id'] = $row['id']; 
      $menu_child_1[$counter_child_1]['name'] = $row['name']; 
      $menu_child_1[$counter_child_1]['url'] = $row['url']; 
      $menu_child_1[$counter_child_1]['parent_id'] = $row['parent_id']; 
      $counter_child_1++; 
     } 

     foreach ($menu_child_1 as $menu_entry_child_1 => $value_child_1) { 
      $le_content .= '<li><a href="'.ROOT_HREF.$value_child_1['url'].'" >'.$value_child_1['name'].'</a>'; 
      $sQuery_get_second_children = " SELECT 
              menu.id, 
              menu.`name`, 
              menu.url, 
              menu.parent_id 
              FROM `menu` 
              WHERE 
              menu.actif = 1 AND 
              menu.parent_id = ".$value_child_1['id']." 
              ORDER BY 
              menu.volgorde ASC"; 
      $rResultaat_get_second_children = mysql_query($sQuery_get_second_children) or die(mysqlError($sQuery_get_second_children)); 
      $row_count = mysql_num_rows($rResultaat_get_second_children); 
      if($row_count != false){ 
       $le_content .= '<ol>'; 
       while($row = mysql_fetch_assoc($rResultaat_get_second_children)){ 
        $menu_child_2[$counter_child_2]['id'] = $row['id']; 
        $menu_child_2[$counter_child_2]['name'] = $row['name']; 
        $menu_child_2[$counter_child_2]['url'] = $row['url']; 
        $menu_child_2[$counter_child_2]['parent_id'] = $row['parent_id']; 
        $counter_child_2++; 
       } 
       foreach ($menu_child_2 as $menu_entry_child_2 => $value_child_2) { 
        $le_content .= '<li><a href="'.ROOT_HREF.$value_child_2['url'].'" >'.$value_child_2['name'].'</a></li>'; 
       } 
       $le_content .= '</ol>'; 
      } 

      $le_content .= '</li>'; 
     } 
     $le_content .= '</ol>'; 
    } 
    $le_content .= '</li>'; 
} 

$le_content .= '</ul></div>'; 

?> 
到底

,我回声$ le_content .....

+0

请考虑切换到库MySQLi或PDO。 'mysql_ *'函数很旧,很快就会被弃用,并且不会提供针对SQL注入攻击的可靠对策。 – Polynomial

+0

好吧,我会这么做;) – Mathlight

回答

0

好的,我搜索了一下,并带有一个解决方案...使查询更好。我已经按照本教程和工作柠好:

sql and categories

感谢所有帮助;)

4
  1. 查询所有菜单项
  2. 建立他们的分级阵列(创建一个递归函数做到这一点)每个菜单项都应该有一个属性为它的子集合
  3. 创建另一个递归函数来编写输出
+0

听起来不错。但我如何建立这样的层次数组不错? – Mathlight