简体   繁体   中英

Trying to generate a 2-column table using PHP from JSON data

This should be a fairly simple problem, however I cannot for the life of me get it to work right.

I have JSON data in the form of an array like so and I am trying to generate a 2-column table where every 2 elements in the array take up a row.

[
    {
        "type": "Ground",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Water",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Sewer",
        "field_location": "",
        "field_user": "",
    },
    {
        "type": "Tap",
        "field_location": "",
        "field_user": "",
    }
]

I have overly simplified the code, however the function currently looks as follows.

function generateHtml($jsonDoc){
    $html = "";
    $html .= "<table>";
    $count = 0;   

    foreach ($jsonDoc as $key => $value) {
        $count ++;

        if ($count % 2 == 0) 
            $html .= "<tr>";

        $html .= "<td>";
        foreach ($value as $key => $val) {
            if(!empty($val))
                $html .= $key.":".$val."<br />";
        }
        $html .= "</td>";

        if ($count % 2 == 0) 
            $html .= "</tr>";
        // $count ++;
    }
    $html .= "</table>";

    print($html);
} 

The resulting HTML output always seems to come out as follows and I cannot seem to understand what is missing.

<table>
    <tr>
        <td>Ground</td>
    </tr>
    <tr>
        <td>Water</td>
    </tr>
    <tr>
        <td>Sewer</td>
    </tr>
    <tr>
        <td>Tap</td>
    </tr>
</table>

as opposed to

<table>
    <tr>
        <td>Ground</td>
        <td>Water</td>
    </tr>
    <tr>
        <td>Sewer</td>
        <td>Tap</td>
    </tr>
</table>
```
which is what I am looking for.

Try this

function generateHtml($jsonDoc){
    $html = "";
    $html .= "<table>";
    $count = 1;   

    foreach ($jsonDoc as $key => $value) {
        //$count ++;

        if ($count % 2 != 0) 
            $html .= "<tr>";

        $html .= "<td>";
        foreach ($value as $key => $val) {
            if(!empty($val))
                $html .= $key.":".$val."<br />";
        }
        $html .= "</td>";

        if ($count % 2 == 0) 
            $html .= "</tr>";
        $count ++;
    }
    $html .= "</table>";

    print($html);
} 

One simple(r) way to achieve this, would be to use array_chunk to split your original data into an array of arrays, and then just use two nested loops to generate the output.

In the outer loop, you create the table row (output <tr> before, and </tr> after), and in the inner loop, you create a table cell for each item.

function generateHtml($jsonDoc){
    $data = array_chunk($jsonDoc, 2); // split this into an array of arrays,
                                      // containing two of the items each
    $html = '<table>';

    foreach ($data as $row) { // loop over the first level
      $html .= '<tr>';
      foreach ($row as $cell) { // loop over the second level
        $html .= '<td>'.$cell->type.'</td>';
      }
      $html .= '</tr>';
    }
    $html .= '</table>';

    print($html);
} 

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