创造&从foreach引用PHP变量进行比较

时间:2013-03-25 06:24:46

标签: php json api trello

Live page can be seen here

我的目标是根据它出现的列表将特定类应用于第二个UL中的每个列表项。第二个json调用返回一个ID,该ID与可在第一个列表中找到的ID和名称对匹配。

我想将每个listID分配给一个名为$ listID的$变量,其值为该列表的名称。

例如$514fc8993f53cfb366006851 = "To Do";

然后我可以使用if语句根据PHP的第二个块中的一系列if语句来分配一个类。

非常感谢产生这些变量的任何帮助。

相关代码如下:

<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>

2 个答案:

答案 0 :(得分: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]这些应该包含与列表相关的卡片。我没有测试代码,但我希望你能得到这个代码......

答案 1 :(得分: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";
    }  

?>