2013-02-06 68 views
0

我看過各種答案,我假設它是由於缺乏foreach循環,我只接收一個變量。有32個類別,每個類別都有多個鏈接。foreach,JSON和PHP

表結構

CREATE TABLE `cattb` (
    `catNo` int(11) NOT NULL, 
    `catName` varchar(100) NOT NULL, 
    PRIMARY KEY (`catNo`) 
) 

CREATE TABLE `linktb` (
    `catId` int(11) NOT NULL, 
    `URL` varchar(255) NOT NULL, 
    `title` varchar(50) NOT NULL, 
    `description` longtext NOT NULL, 
    KEY `catId` (`catId`) 
) 

SQL查詢

SELECT catId, catName, URL, title, description FROM linktb, cattb WHERE linktb.catId=cattb.catNo 

PHP

while ($row = mysql_fetch_assoc($result)){ 
    $json['categories'][] = array( 
     "catId"=> $row[catId], 
     "catName"=> $row[catName], 
     'links' => array( 
      "URL"=> $row[URL], 
      "title"=> $row[title], 
      "description"=> $row[description] 
     ) 
    ); 

響應:

"categories":[ 
    {"catId":"1", 
    "catName":"Career Resources", 
    "links": 
     {"URL":"http:\/\/www.designanddesign.com\/jobs.php", 
     "title":"Design & Design", 
     "description":"World-Wide Job Offers" 
     } 
    }, 
    {"catId":"1", 
    "catName":"Career Resources", 
    "links": 
     {"URL":"https:\/\/www.nuans.com\/rts-str\/en\/home-accueil_en.cgi", 
     "title":"NUANS Report", 
     "description":"Naming A Company (Federal)" 
     } 
    } 

我希望看到的:

{ categories: [ 
    { catName: "Career Resources", 
     catId: "1", 
     links: [ 
      {URL:"http://www.designanddesign.com/jobs.php",title:"Design & Design",description:"World-Wide Job Offers"}, 
      {URL:"https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/hm.html?locale=en_CA",title:"Corporations Canada",description:"Incorporating a Business"}, 
      {URL:"http://lea.verou.me/css3patterns/",title:"CSS3 Patterns",description:"Patterns to use for backgrounds"}, 
      {URL:"http://webdesignerwall.com/tutorials/cross-browser-css-gradient",title:"CSS Gradients",description:"Cross-browser Compatible"} 
      ], 
    }, 
    { catName: "CSS", 
     catId: "2", 
     links: [ 
      {URL:"http://www.designanddesign.com/jobs.php",title:"Design & Design",description:"World-Wide Job Offers"}, 
      {URL:"https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/hm.html?locale=en_CA",title:"Corporations Canada",description:"Incorporating a Business"}, 
      {URL:"http://lea.verou.me/css3patterns/",title:"CSS3 Patterns",description:"Patterns to use for backgrounds"}, 
      {URL:"http://webdesignerwall.com/tutorials/cross-browser-css-gradient",title:"CSS Gradients",description:"Cross-browser Compatible"} 
      ], 
    } 

我怎麼會寫for-each循環的陣列?

+1

用你的代碼,'links'不會有多個條目,但只有一個。沒有循環來生成上面列出的列表。 – SachinGutte

+0

雖然您沒有任何Feed數據,但您希望如何顯示鏈接數組! 看到@phazorRise輸入了什麼 – semsem

+0

因此,我無法讓我的鏈接用我的數據創建第二個數組? –

回答

1

通過以id爲索引來跟蹤您已經看到了哪些類別。如果您已經看到該類別,請添加到鏈接數組中。完成後設置$ json ['categories']。

$catRows = array(); 

while ($row = mysql_fetch_assoc($result)) { 
    $id = $row['catId']; 
    $links_entry = array(
     'URL' => $row['URL'], 
     'title' => $row['title'], 
     'description' => $row['description'] 
    ); 
    if (!isset($catRows[$id])) { 
     $catRows[$id] = array(
      'catId' => $id, 
      'catName' => $row['catName'], 
      'links' => array($links_entry) 
     ); 
    } else { 
     $catRows[$id]['links'][] = $links_entry; 
    } 
} 

$json['categories'] = array_values($catRows); 
+0

正是我需要的。謝謝! –

+0

很高興我能幫到你。 – 6124j50n

3

在您的while循環中,您將在每次迭代中重置$json['categories']

嘗試

while ($row = mysql_fetch_assoc($result)){ 
    $json['categories'][] = array( 
     "catId"=> $row[catId], 
     "catName"=> $row[catName], 
     'links' => array( 
      "URL"=> $row[URL], 
      "title"=> $row[title], 
      "description"=> $row[description] 
     ) 
    ); 
+0

這絕對讓我有更多的結果!但是現在第二個陣列仍然無法正常工作。我已經將我的帖子更新爲我當前的PHP響應。屬於一個類別的所有鏈接應顯示在該類別下,而不是在具有相同名稱的新類別下顯示。 –

+0

這是因爲每次你進入你的while循環時,你將擦除'$ json'中的內容,嘗試將'$ jon'移出循環,然後將項目推入數組;這將在第二次進入循環之前保留數組內的數據。 –

+0

我不完全確定如何在這種情況下應用array_push –

0

爲了那就是 「讓單一入口」 你的第一個問題。使用

$items = array(); 
while ($row = mysql_fetch_assoc($result)){ 
    $json['categories'] = array( 
     "catId"=> $row[catId], 
     "catName"=> $row[catName], 
     'links' => array( 
      "URL"=> $row[URL], 
      "title"=> $row[title], 
      "description"=> $row[description] 
     ) 
    ); 
    $items[] = $json['categories']; 
} 

關於你在links數組中有單個條目的下一個問題。目前的模式存在問題。我現在看到的方式是,你已經有catName,並且你已經有了關於catName在單個表中的鏈接和內容。這是多餘的。你可以進一步標準化,並有單獨的表格說cat_meta爲url鏈接,標題和說明和catId,將指出你的貓。接下來在選擇數據時使用連接。這將返回與您的查詢匹配的cat_meta的entires數組。並且您使用foreach並重復它以形成links陣列。

+0

我已經添加了我目前的表格結構,我相信這就是你的建議。我假設我應該改變我的查詢,包括左/右連接? –