简体   繁体   English

使用数组和json数据创建html表

[英]Create html table with arrays and json data

I need to create html table... 我需要创建html表...

With this code: 使用此代码:

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

$json = json_decode($result);

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

I got this data: 我得到了这些数据:

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.

How to create html with that data? 如何使用该数据创建html? How to use foreach and create table and in that row for every ID ? 如何使用foreach并创建表,并为每个ID在该行中使用表?

Knowing the Structure of the JSON Data you expect, you can dynamically construct a Table within a foreach Loop using the JSON Data like so: 了解所需的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;

Open following link there is complete guide. 打开以下链接有完整的指南。 how to create html table with php including setting attribute, data and print. 如何用php创建html表,包括设置属性,数据和打印。

link :- https://pear.php.net/manual/en/package.html.html-table.intro.php 链接: -https : //pear.php.net/manual/en/package.html.html-table.intro.php

You getting an object inside the array index. 您在数组索引中获得一个对象。 If you iterate over the array, every instance will return an object and you only need to call for its property, so you don't have to iterate over the value again. 如果对数组进行迭代,则每个实例都将返回一个对象,并且只需要调用其属性即可,因此您不必再次迭代该值。

Try: 尝试:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM