2016-07-14 150 views
0

我需要創建HTML表JSON數據的HTML表格...創建陣列和

有了這個代碼:

$result = curl_exec($ch); 
curl_close($ch); 

$json = json_decode($result); 

foreach ($json->data->reservations as $element) { 
    print_r($element); 
}// 

我得到了這樣的數據:

stdClass Object ([id] => 11616946 [property] => NNN [propertyName] => nnn [infourl] => https://domain.com [from] => 2016-07-18 [to] => 2016-07-25) [pricing] => stdClass Object ([price] => 1164.24 [clientInfo] => stdClass Object ([firstName] => PERA [lastName] => PETROVI [email] => [email protected]) [status] => 1 [offline] => 0) stdClass Object ([id] => 11589607 [property] ... ... ... ... etc. 

如何創建與該數據的HTML?如何使用foreach和創建表並在該行中爲每個ID?

回答

1

知道你所期望的JSON數據的結構,則可以使用JSON數據像這樣動態地構造一個foreach環內的表:

<?php 
     $result  = curl_exec($ch); 
     curl_close($ch); 

     $json  = json_decode($result); 

     // BUILD THE TABLE INITIAL HEADER SECTION 
     $strTableOutput  = "<table class='reservation-tbl' id='reservation-tbl'>" . PHP_EOL; 
     $strTableOutput .= "<tr class='reservation-header-row'>"      . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Client ID</th>"  . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Property</th>"  . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Property Name</th>" . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>First Name</th>"  . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>LastName</th>"  . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>E-Mail</th>"   . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Price</th>"   . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>From</th>"   . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Till</th>"   . PHP_EOL; 
     $strTableOutput .= "<th class='reservation-header-cell'>Status</th>"   . PHP_EOL; 
     $strTableOutput .= "</tr>"             . PHP_EOL; 
     $strTableOutput .= "<tbody class='reservation-body'>"      . PHP_EOL; 


     // LOOP THROUGH THE JSON DATA & BUILD EACH ROW USING THE 
     // DATA PROVIDED BY THE JSON OBJECT 
     foreach ($json->data->reservations as $element) { 
      $strTableOutput .= "<tr class='reservation-data-row'>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->id}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->property}<br /><a href='{$element->infourl}' target='_blank'>{$element->infourl}</a></td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->propertyName}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->pricing->clientInfo->firstName}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->pricing->clientInfo->lastName}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->pricing->clientInfo->email}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->pricing->price}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->from}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->to}</td>" . PHP_EOL; 
      $strTableOutput .= "<td class='reservation-data-cell'>{$element->pricing->status}</td>" . PHP_EOL; 
      $strTableOutput .= "</tr>" . PHP_EOL; 
     } 
     // CLOSE THE TABLE-BODY AND THE TABLE 
     $strTableOutput .= "</tbody>" . PHP_EOL; 
     $strTableOutput .= "</table>" . PHP_EOL; 

     // DISPLAY THE HTML REPRESENTATION OF YOUR STRUCTURED TABLE-DATA 
     echo $strTableOutput; 
+0

謝謝,我創建了相同的,但我使用數據表 –

0

你得到數組索引中的一個對象。如果遍歷數組,每個實例都會返回一個對象,並且只需要調用它的屬性,所以您不必再次遍歷該值。

嘗試:

foreach ($json->data->reservations as $element) { 
    echo("<table>"); 
    echo("<tr>"); 
    echo("<td>"); 
    echo($element.id); 
    echo($elenent.propertyName); 
    //echo($elenent.etc); 
    echo("</td>"); 
    echo("</tr>"); 
    echo("</table>"); 
}