2016-02-03 57 views
1

我想要做的是創建一個從數據庫中拉出的類別列表,然後列出這些類別下的每個相關子類別 。使用嵌套的while循環沒有給出所需的輸出

到目前爲止我有:

<?php 
// this query lets us know that we are looking for matches that equal 2,  which points to the subject of vehicles. 
// from this query i am able to list all categories that are relevant to vehicles. 

$getCategoriesQuery = $db->query("SELECT * FROM item_cat WHERE sub_id = '2'"); //2 = vehicles 
?> 

<?php 
while($row = $getCategoriesQuery->fetch()){ 
    echo '<ul>'.$row['category'].''; 
} 
?> 

其中顯示:

- motor vehicles 
- railed vehicles 
- aircraft 

現在,當我添加下面的while循環上面的一個:

<?php 
while($row = $getCategoriesQuery->fetch()){ 
    echo '<ul>'.$row['category'].''; 

    // this has been added to try and get the sub categories 
    $getSubCatQuery = $db->query("SELECT * FROM item_cat, item_sub_cat 
       WHERE item_cat.cat_id = item_sub_cat.cat_id "); // cat_id 
     while($row = $getSubCatQuery->fetch()){ 
      echo '<li><a href="vehicles.php?p='.$row['sub_category'].'" >'.$row['sub_category'].'</a></li>'; 

     } 
    echo '</ul>'; 
} 
?> 

我得到:

- motor vehicles 
    - cars 
    - motorbikes 
    - buses 
    - trucks 
    - trains 
    - planes 
-railed vehicles 
    - cars 
    - motorbikes 
    - buses 
    - trucks 
    - trains 
    - planes 
-aircraft 
    - cars 
    - motorbikes 
    - buses 
    - trucks 
    - trains 
    - planes 

當什麼,我想要做的是:

-motor vehicles 
    - cars 
    - motorbikes 
    - buses 
    - trucks 
-railed vehicles 
    - trains 
-aircraft 
    - planes 

我曾嘗試加入查詢開始的,但沒有快樂,所以我分成2個查詢來試試,看看事情會好一點。 我已經在第二個while循環中將「$ row」更改爲「$ row2」,但是這樣會給我一個未定義的索引錯誤「row2 ['sub_category']」

我也嘗試使用「foreach循環「而不是第二個‘while循環’:

<?php 
// this query lets us know that we are looking for matches that equal 2, which points to vehicles. 
$getCategoriesQuery = $db->query("SELECT * FROM item_cat WHERE sub_id = '2'"); //vehicles 
?> 

<?php 
while($row = $getCategoriesQuery->fetch()){ 
    echo '<ul>'.$row['category'].''; 

    $getSubCatQuery = $db->query("SELECT * FROM item_cat, item_sub_cat 
        WHERE item_cat.cat_id = item_sub_cat.cat_id "); // cat_id 

     foreach ($getSubCatQuery->fetchAll() as $row2) { 
     echo '<li><a href="vehicles.php?p='.$row2['sub_category'].'" >'.$row2['sub_category'].'</a></li>'; 
     } 
    echo '</ul>'; 
} 
?> 

我花了一整天試圖解決這個問題,但沒有喜悅 - 只是頭痛。任何人都可以請告訴我哪裏錯了...我錯過了什麼? - 如果不明確,我也在使用PDO。

在此先感謝!

--UPDATED-- 我已經改變了而循環部分:

<?php 
while($row = $getCategoriesQuery->fetch()){ 
    echo '<ul>'.$row['category'].''; 

    $getSubCatQuery = $db->query("SELECT * FROM item_sub_cat, item_cat WHERE item_sub_cat.cat_id ='".$row['cat_id']."' "); 
     while($sub_row = $getSubCatQuery->fetch()){ 
      echo '<li><a href="vehicles.php?p='.$sub_row['sub_category'].'" >'.$sub_row['sub_category'].'</a></li>'; 

     } 
    echo '</ul>'; 
} 
?> 

和輸出被顯示在正確的類別的東西....然而它是由總複製的每個項目記錄我在數據庫:

- motor vehicles 
    - cars 
    - motorcycles 
    - trucks 
    - buses 
    - cars 
    - motorcycles 
    - trucks 
    - buses 
    - cars 
    - motorcycles 
    - trucks 
    - buses 
    - cars 
    - motorcycles 
    - trucks 
    - buses 
    - cars 
    - motorcycles 
    - trucks 
    - buses 
- railed vehicles 
    - trains 
    - trains 
    - trains 
    - trains 
    - trains 

我已經加入「LIMIT 1」和「GROUP BY」的查詢嘗試過,但後來只顯示每類1項。

- motor vehicles 
    - cars 
- railed vehicles 
    - trains 
+0

你應該使用'GROUP BY'而不是'limit',這應該檢索結果。 –

回答

1

你的內while循環重新分配$row數組變量。將其更改爲其他名稱,例如$sub_row

根據外部查詢的值,您的內部查詢也不會更改,因此每次執行的方式都完全相同。在內部查詢中使用類似 SELECT * FROM item_sub_cat WHERE item_sub_cat.cat_id = $row["category_id"]的內容將允許結果依賴於外部查詢的每個結果。

編輯:請注意在您的查詢中包含表格,輸出方式,您不需要。只有在需要執行兩個表的連接時才包含它們。

+0

我將內部的「$ row」改爲「$ sub_row」,這在現在看事物時很有意義。然後按照您的建議更改內部查詢:'code' $ getSubCatQuery = $ db-> query(「SELECT * FROM item_sub_cat,item_cat WHERE item_sub_cat.cat_id ='」。$ row ['cat_id']。「'」); '代碼'已經把sub_categories放在正確的類別下,但是,它們重複x5(我在相關數據庫中的總行數)。我在查詢中添加了一個LIMIT,但沒有解決。 – amokske

+0

好吧,首先不需要結尾連接,你可以這樣寫:'$ getSubCatQuery = $ db-> query(「SELECT * FROM item_sub_cat,item_cat WHERE item_sub_cat.cat_id ='」。$ row ['cat_id']); '。你的意思是這些子類別在每個主要類別下重複五次? – Defiant

+0

是的,我爲飛機和有軌車輛添加了一些其他子類別,以查看它是否會增加,但它只是重複5次 - 編輯 - 再次查看事情,5來自我有5類類別數據庫。 – amokske

0

我不知道這是否可能是有用的,但我想你應該能夠使用連接來獲取所有相關記錄在一個去

select * from `item_cat` c 
    right join `item_sub_cat` sc on sc.`cat_id`=c.`cat_id` 
where c.`sub_id` = '2' 


create table if not exists `item_cat` (
    `cat_id` smallint(5) unsigned not null auto_increment, 
    `category` varchar(50) default null, 
    primary key (`cat_id`) 
) engine=innodb auto_increment=4 default charset=utf8; 

insert into `item_cat` (`cat_id`, `category`) values 
    (1, 'aircraft'), 
    (2, 'railed vehicles'), 
    (3, 'motor vehicles'); 

+--------+-----------------+ 
| cat_id | category  | 
+--------+-----------------+ 
|  1 | aircraft  | 
|  2 | railed vehicles | 
|  3 | motor vehicles | 
+--------+-----------------+ 


create table if not exists `item_sub_cat` (
    `scid` smallint(5) unsigned not null auto_increment, 
    `cat_id` smallint(5) unsigned not null, 
    `sub_category` varchar(50) not null, 
    primary key (`scid`), 
    key `cat_id` (`cat_id`), 
    constraint `fk_cat_subcat` foreign key (`cat_id`) references `item_cat` (`cat_id`) on delete cascade on update cascade 
) engine=innodb auto_increment=10 default charset=utf8; 

insert into `item_sub_cat` (`scid`, `cat_id`, `sub_category`) values 
    (1, 3, 'car'), 
    (2, 3, 'motorbike'), 
    (3, 3, 'buses'), 
    (4, 3, 'truck'), 
    (5, 2, 'train'), 
    (6, 2, 'wagon'), 
    (7, 1, 'helicopter'), 
    (8, 1, 'plane'), 
    (9, 1, 'microlite'); 

+------+--------+--------------+ 
| scid | cat_id | sub_category | 
+------+--------+--------------+ 
| 1 |  3 | car   | 
| 2 |  3 | motorbike | 
| 3 |  3 | buses  | 
| 4 |  3 | truck  | 
| 5 |  2 | train  | 
| 6 |  2 | wagon  | 
| 7 |  1 | helicopter | 
| 8 |  1 | plane  | 
| 9 |  1 | microlite | 
+------+--------+--------------+ 

select * from item_cat c 
    right join item_sub_cat sc on sc.cat_id=c.cat_id; 

+--------+-----------------+------+--------+--------------+ 
| cat_id | category  | scid | cat_id | sub_category | 
+--------+-----------------+------+--------+--------------+ 
|  3 | motor vehicles | 1 |  3 | car   | 
|  3 | motor vehicles | 2 |  3 | motorbike | 
|  3 | motor vehicles | 3 |  3 | buses  | 
|  3 | motor vehicles | 4 |  3 | truck  | 
|  2 | railed vehicles | 5 |  2 | train  | 
|  2 | railed vehicles | 6 |  2 | wagon  | 
|  1 | aircraft  | 7 |  1 | helicopter | 
|  1 | aircraft  | 8 |  1 | plane  | 
|  1 | aircraft  | 9 |  1 | microlite | 
+--------+-----------------+------+--------+--------------+ 

從上面的記錄應該是比較很容易生成你以後的html佈局 - 儘管我可能錯過了這一點。