【问题标题】:Create html table with arrays and json data使用数组和 json 数据创建 html 表
【发布时间】:2016-07-14 06:24:13
【问题描述】:

我需要创建 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 创建表?

【问题讨论】:

    标签: php html arrays json foreach


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      打开下面的链接有完整的指南。如何用php创建html表,包括设置属性、数据和打印。

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

      【讨论】:

        【解决方案3】:

        你在数组索引中得到一个对象。如果你遍历数组,每个实例都会返回一个对象,你只需要调用它的属性,所以你不必再次遍历这个值。

        试试:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-14
          相关资源
          最近更新 更多