2013-06-18 130 views
1

我的代碼存在問題。用n-subcategories顯示所有類別的最佳方式是什麼?使用PHP + MySQL顯示具有子類別和n-sub類別的類別

我的MySQL數據庫的樣子:

CREATE TABLE categories ( 
category_id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
parent_id INT UNSIGNED NOT NULL DEFAULT 0, 
name TEXT NOT NULL, 
user INT NOT NULL, 
PRIMARY KEY (category_id), 
INDEX parent (parent_id) 
); 

哪裏user是從另一個表ID(如果類別有user = '2',與id = '2'用戶可以看到該類別)

category_id parent_id name     user 
1    0   Main category 1  2 
2    1   Subcategory 1   2 
3    1   Subcategory 2   2 
4    3   SubSubcategory 1  2 

我的PHP代碼:

<?php 
    include('config.php'); //Mysql connect file 
//get id from user 

if($_GET['id'] != $user_id) { 

    $id = $_GET['id']; 

//check to make sure the user is an admin, who can change mysql 

$result = mysql_query("SELECT * FROM users WHERE username = '$username' AND admin = '1'"); 

$rowCheck = mysql_result($result, 0); 



//if the query returns a number, we know the user is an admin. And here we can VIEW our categories 

if($rowCheck > 0) { 

$user_result = mysql_query("SELECT * FROM users, categories WHERE id = '$id'"); 

    while($row = mysql_fetch_array($user_result)) { ?>  

     <ol> 
     <li><a href=""><?php echo $row['name']; ?></a></li> 
     </ol> 

    <?php } 

    } else { 

     echo "You are not admin"; 
    }  
//bla bla bla etc. 
... 
} 

How我可以查看類別,如

<ol> 
<li> 
<a href="">Main category 1</a> 
     <ol> 
     <li><a href="">Subcategory 1</a></li> 
     <li><a href="">Subcategory 2</a> 
      <ol> 
       <li><a href="">Subsubcategory 1</a></li> 
      </ol> 
     </li> 
     </ol> 
</li> 
</ol> 
+0

是否有子類的任何限制嗎?\ –

+0

三級.... – oboshto

回答

3

試試這個

$result = mysql_query("SELECT * FROM users WHERE username = '$username' AND admin = '1'"); 

$rowCheck = mysql_result($result, 0); 

//if the query returns a number, we know the user is an admin. And here we can VIEW our categories 

if($rowCheck > 0) { 

//First fetch only parent categories 
$first_level_cats = mysql_query("SELECT * FROM categories WHERE user = '$id' AND parent_id = 0"); 
    echo '<ol>'; 
    while($row = mysql_fetch_array($first_level-cats)) { ?>  

     <li><a href=""><?php echo $row['name']; ?></a></li> 

     <?php 
     $second_level_cats = mysql_query("SELECT * FROM categories WHERE user = '$id' AND parent_id = $row['id']"); 
    echo '<ol>'; 
    while($row = mysql_fetch_array($second_level-cats)) { ?>  

     <li><a href=""><?php echo $row['name']; ?></a></li>    
     <?php 
      $third_level_cats = mysql_query("SELECT * FROM categories WHERE user = '$id' AND parent_id = $row['id']"); 
    echo '<ol>'; 
    while($row = mysql_fetch_array($sthird_level-cats)) { ?>  

     <li><a href=""><?php echo $row['name']; ?></a></li>    
     <?php 
      } 
      echo '</ol>'; //third level close 
     ?> 
      } 
     echo '</ol>'; // second level close 
     ?>  
    <?php } 
    echo '</ol>'; // first level close 
    } else { 

     echo "You are not admin"; 
    }  
//bla bla bla etc. 
... 
} 

我已經盡我所能來解決您的查詢,根據您的schema交叉檢查table name and columns

嘗試使用Mysqli延伸,而不是Mysql

+0

非常感謝!完美:) – oboshto

2
$user_result = mysql_query("SELECT * FROM users, categories WHERE id = '$id'"); 
    $categories = array(); 
    while($row = mysql_fetch_array($user_result)) {  
     $categories[$row['parent_id']][] = $row['name']; 
    } 
    echo "<ol><li><a href''>$categories[0][0]</a><ol>"; 
    $counterj = count($categories)-1; 
    for($i=1,$j=$counterj;$i<$j;$i++){ 
     if(count($categories[$i])==1){ 
      echo "<li><a href="">$catgeories[$i][0]</a></li>"; 
     } else { 
      echo "<li><a href="">$categories[$i][0]</a><ol>"; 
     } 
     $counterl = count($categories[$i])-1; 
     for($k=1,$l=$counterl;$k<$l;$k++){ 
      echo "<li><a href="">$categories[$i][$k]</a></li>"; 
     } 
     if ($l>1) echo "</ol></li>"; 
    } 
    echo "</ol></li></ol>"; 

} else { 
    echo "You are not admin"; 
} 
+0

我不完全確定'

  1. ' - 標籤因爲有很多,但你有一個查詢中的所有數據。其他數據庫查詢不是必需的。 – RST

    +1

    對不起,應該是'==',代碼編輯。 – RST