2013-08-05 17 views
0

我想創建一個嵌套的無序列表使用PHP和MySQL(通過CodeIgniter,但我認爲這不相關)。PHP和MySQL中的嵌套列表

我見過一些解決方案似乎適用於有兩級嵌套的列表,但我需要的解決方案必須有三個級別。這是諸如此類的事情,我需要:

<ul> 
    <li>Top Level, Item 1 
     <ul> 
      <li>Second Level, Item 1 
       <ul> 
        <li>Third Level, Item 1</li> 
        <li>Third Level, Item 2</li> 
        <li>Third Level, Item 3</li> 
       </ul> 
      <li>Second Level, Item 2 
       <ul> 
        <li>Third Level, Item 4</li> 
        <li>Third Level, Item 5</li> 
        <li>Third Level, Item 6</li> 
       </ul> 
      </li> 
     </ul> 
    </li> 
</ul> 

從我的數據庫的輸出,主要有:

TOP LEVEL | SECOND LEVEL | THIRD LEVEL 
Item 1 | Item 1 | Item 1 
Item 1 | Item 1 | Item 2 
Item 1 | Item 1 | Item 3 
Item 1 | Item 2 | Item 4 
Item 1 | Item 2 | Item 5 
Item 1 | Item 2 | Item 6 

我試圖通過從數據庫中輸出去,使用的變量要註冊的水平我在等等,但是在關閉每個列表時我陷入了一個糟糕的境地。

任何人都可以提出一個這樣做的方法嗎?

+0

你的數據庫結構和查詢是什麼樣的? – Maximus2012

+0

一切事物總是三分之一還是變化? – DevlshOne

+0

@ Maximus2012 - 我的數據庫輸出是固定的,所以在描述時可能沒什麼意義。它輸出類似於我在問題中提出的內容(顯然有更多細節!)。 – davidofyork

回答

0

感謝所有幫助,但我想我已經破解了我自己,在一個完全不同的方式!

@DevishOne提供的解決方案讓我思考,並將其作爲模板的一部分添加到變量中,以確定是應該打開還是關閉新的子列表或子子列表。

我不是說這是萬無一失的,而且我也不是聲稱這是特別有效的,但它的工作對我來說(至少到目前爲止)

我的解決辦法:

//Placeholder variables 
$current_top = ''; 
$current_second = ''; 
$current_third = ''; 

echo '<ul>'; 
foreach($array as $row) { 
    //If it is a new top level item... 
    if ($row['top_level'] != $current_top) { 
     //If it isn't the very first top level item... 
     if ($current_top != '') { 
      //Close off all of the other first, second, and third level branches. 
      echo '</ul></li></ul></li>'; 
     } 
     //Echo the top level item, including the start of the sub-list. 
     echo '<li>'.$row['top_level'].'<ul>'; 
     //Set the current top level variable to the current top level item. 
     $current_top = $row['top_level']; 
     //Set the current second level variable back to null (if it isn't already). 
     $current_second = ''; 
    } 
    //If it is a new second level... 
    if ($row['second_level'] != $current_second) { 
     //If it isn't the very first second level item... 
     if ($current_second != '') { 
      //Close off all of the other second and third level branches. 
      echo '</ul></li>'; 
     } 
     //Echo the second level item, including the start of the sub-sub-list. 
     echo '<li>'.$row['second_level'].'<ul>'; 
     //Set the current second level variable to the current second level item. 
     $current_second = $row['second_level']; 
     //Set the current third level variable back to null (if it isn't already). 
     $current_third = ''; 
    } 
    //If it is a new third level... 
    if ($row['third_level'] != $current_third) { 
     //Echo the third level item, and close it off. 
     echo '<li>'.$row['third_level'].'</li>'; 
     //Set the current third level variable to the current third level item. 
     $current_third = $row['third_level']; 
    } 
} 
echo '</ul>'; 
0

查詢

SELECT a.item, b.item, c.item FROM TOP_LEVEL a JOIN SECOND_LEVEL b ON b.level = a.level JOIN THIRD_LEVEL c ON c.level = b.level ORDER BY a.item, b.item, c.item 

代碼

<?php 
$q = "SELECT a.item, b.item, c.item FROM TOP_LEVEL a JOIN SECOND_LEVEL b ON b.level = a.level JOIN THIRD_LEVEL c ON c.level = b.level ORDER BY a.item, b.item, c.item "; 
...... your database stuff...... 
...... resulting array should be two-dimensional `$items` ...... 
$c = 0; 
echo "<ul>\n"; 
while ($c < count($items)) { 
    for ($j = 0; $j < count($items[$c][0]); $j++) { 
     echo "<li>$items[$c][0]\n"; 
     echo "<ul>\n"; 
     for ($k = 0; $k < count($items[$c][1]); $k++) { 
      echo "<li>$items[$c][1]\n"; 
      echo "<ul>\n"; 
      for ($i = 0; $i < count($items[$c][2]); $i++) { 
       echo "<li>$items[$c][2]<\li>\n"; 
       $c++; 
      } 
      echo "</ul>\n"; 
      echo "</li>\n"; 
        $c++; 
     } 
     echo "</ul>\n"; 
     echo "</li>\n"; 
      $c++; 
    } 
    echo "</ul>\n"; 
} 
?> 
+0

對不起,我應該更清楚地解釋 - 這不是我以後的SQL。我已經有了,而且它是相當固定的。這是SQL查詢結果的處理,將其轉換爲HTML中的3級嵌套無序列表。 – davidofyork

+0

你使用mysqli還是PDO? – DevlshOne

+0

這應該爲你做。 – DevlshOne