[英]Create html table with arrays and json data
我需要创建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] => nnn@ravel.com ) [status] => 1 [offline] => 0 ) stdClass Object ( [id] => 11589607 [property] ... ... ... ... etc.
如何使用该数据创建html? 如何使用foreach并创建表,并为每个ID在该行中使用表?
了解所需的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;
打开以下链接有完整的指南。 如何用php创建html表,包括设置属性,数据和打印。
链接: -https : //pear.php.net/manual/en/package.html.html-table.intro.php
您在数组索引中获得一个对象。 如果对数组进行迭代,则每个实例都将返回一个对象,并且只需要调用其属性即可,因此您不必再次迭代该值。
尝试:
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>");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.