2013-03-25 59 views
0

Live page can be seen here創建和從引用的foreach PHP變量比較

我的目標到具體的類別應用到基於它出現在列表中第二UL每個列表項。第二個json調用返回一個ID,該ID匹配可以在第一個列表中找到的ID和名稱對。

我想將每個listID分配給一個名爲$ listID的$變量,其值是該列表的名稱。

$514fc8993f53cfb366006851 = "To Do";

然後我可以使用if語句來分配基於一系列的類,如果在PHP中的第二塊語句。

任何幫助生成這些變量將不勝感激。

相關的代碼如下:

<p>The sample board contains the following lists:</p> 
<ul> 
    <? 
     $lists_enc = file_get_contents("https://api.trello.com/1/board/".$BOARD."/lists?key=9ee57574e9f968cedf9ac9964d2f7c4e"); 
     $lists = json_decode($lists_enc, true); 
     $testing = array(); 

     foreach ($lists as $list) { 
      $id = $list["id"]; 
      $name = $list["name"]; 

      echo "<li>".$name." (".$id.")</li>\n"; 
     } 
     echo "</ul>"; 
    ?> 

<p>The sample board contains the following cards:</p> 
<ul> 
    <? 
     $cards_enc = file_get_contents("https://api.trello.com/1/board/".$BOARD."/cards?fields=name,idList&key=9ee57574e9f968cedf9ac9964d2f7c4e");  
     $cards = json_decode($cards_enc, true); 

     foreach ($cards as $card) { 
     echo "<li class=".$status.">".$card["name"]."</li>\n"; 
     } 

    ?> 
</ul> 
+0

目標是從這個json創建3個變量 - https://api.trello.com/1/board/ 514fc8993f53cfb366006850 /列表?關鍵= 9ee57574e9f968cedf9ac9964d2f7c4e ,讓我的ID(包含在其他的JSON輸出)的名稱(這是隻有在這個JSON輸出) – 2013-03-25 23:48:08

回答

1
<?php 
//Reference the trello API's 
$lists_enc = file_get_contents("https://api.trello.com/1/board/".$BOARD."/lists?key=9ee57574e9f968cedf9ac9964d2f7c4e"); 
$lists = json_decode($lists_enc, true); 
$cards_enc = file_get_contents("https://api.trello.com/1/board/".$BOARD."/cards?fields=name,idList&key=9ee57574e9f968cedf9ac9964d2f7c4e");  
$cards = json_decode($cards_enc, true); 
//Go through every card 
foreach ($cards as $card) { 
    $idCard = $card['id']; 

    //Go through all lists 
    foreach ($lists as $list) { 
     $idList = $list['id']; 

     //If card is related to list, then create an associative variable 
     //with the id of list 
     if ($card['id'] == $list['id'] { 
      $idList[$idList][] = $idCard; 
     } 
    } 
    //gone through all lists 
} 
//-- gone through all cards 
?> 

現在你將有類似$ IDLIST [514fc8993f53cfb366006851],$ IDLIST [514fc8993f53cfb366006852]和$ IDLIST [514fc8993f53cfb366006853]這些應包含卡相關到名單。我還沒有測試代碼,但我希望你能得到圖片...

+0

轉換對不起我那可憐的解釋。 – 2013-03-25 23:43:52

+0

我想爲每個頁面ID分配一個單獨的變量,以便爲我提供頁面名稱。 – 2013-03-25 23:44:27

+0

嗯。我有這個權利嗎? – bestprogrammerintheworld 2013-03-26 16:40:39

0
<? 
    // Create array $convert[] to allow translation of listID into listName 
    for ($i=0; $i < count($lists); $i++) { 
     $convert[$lists[$i][id]]=$lists[$i][name]; 
    } 

    // iterate over all cards, create list item for each with class that has a slug equivalent of the list name 
    foreach ($cards as $card) { 
     $id = $card[idList]; 
     echo "<li class=".slug($convert[$id]).">".$card["name"]."</li>\n"; 
    } 

?> 
+0

好的工作!爲什麼你在第一個循環中使用($ i = 0 ...)而不是foreach()的格式? – bestprogrammerintheworld 2013-03-27 05:48:15