2016-03-15 28 views
0

我似乎無法找到一種方法來使用我的自定義編碼論壇鏈接類別和標題。正確標題下顯示類別下的自定義編碼論壇

下面是分類表的圖片:

enter image description here

和頭表的圖片:

enter image description here

現在這兩個表中有一個FOREIGN KEY鏈接,是:

ALTER TABLE categories ADD FOREIGN KEY(cat_head) REFERENCES headers(head_id) ON DELETE CASCADE ON UPDATE CASCADE; 

我的論壇的圖片:

enter image description here

(注意每個類別如何「PrevailPots服務器」標題下滴下來。

+0

改進格式並直接在帖子中添加圖片。 – AdrienXL

回答

0

如果我正確理解你的問題。

  • 2個表上的查詢都可以做到。
用於從標題表中檢索細節

第一查詢 =>得到head_id

然後

在類別表第二查詢 =>與比較cat_head head_id並檢索貓的詳細信息。

更新

我使用上述理念爲我定製的論壇。
示例代碼

   $result1=queryMysql("SELECT * FROM headers"); //My custom function for query execution, returns the result  

      while($row1 =$result2->fetch_array(MYSQL_ASSOC)) 
       {    
         echo $row1['head_name']; 
       $result2=queryMysql("SELECT * FROM categories WHERE cat_head=$row[head_id]"); // My custom function for query execution, returns the result  

        while($row2=$result2->fetch_array(MYSQL_ASSOC)){ 

         echo $row2['cat_name']; 
         echo $row2['cat_description']; 
        } 

       } 

這就是我的意思。
今天我肯定會嘗試一下JOIN操作。

  $result=queryMysql('SELECT * FROM headers LEFT JOIN categories ON head_id = cat_head'); //My custom function for query execution, returns the result  
      $new_head=""; 
      $pre_head=""; 
      while($row =$result->fetch_array(MYSQL_ASSOC)) 
       {    
         $new_head= $row['head_name']; 

         if($new_head!=$pre_head){ 
          echo $new_head; 
         } 
         if($row['cat_name']!=null or $row['cat_description']!=null){ 
          echo $row['cat_name']; 
          echo $row['cat_description']; 
         } 
         else { 
          echo "No categories yet"; 
         } 

         $pre_head=$row['head_name']; 
       } 

我覺得你有這個想法了。

+0

嘗試獲取head_id並將其與cat_head進行比較,運氣不佳。你能告訴我一個你的意思的例子代碼嗎?我試過這個: CODE: '$ currentCategory =「」; if($ currentCategory ==「」|| $ currentCategory!= $ category_title){' 我也對兩個表進行了「全外連接」來比較,沒有運氣。 – PlayinCODMC

+0

我覺得左連接在你的情況下更好,因爲每個類別都會在一些標題下。 – Sachin

+0

在論壇管理面板中創建時,沒有分類標題。哪些是FOREIN KEY鏈接到cat_head和head_id。 – PlayinCODMC