简体   繁体   中英

Create html table with arrays and json data

I need to create html table...

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? How to use foreach and create table and in that row for every 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:

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

link :- 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>");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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