loliki
loliki

Reputation: 957

Foreach returns a column and I have to turn it into a table

I have the following JSON table

[
    ["TITLE", "CONTENT", "DATE"],
    ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
    ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]

I use a foreach to get the json contents

$days = json_decode(file_get_contents('json_file'));
        foreach($days as $item){
            if(isset($item)){
                $firstColumn = $item;
            }
            echo $firstColumn[0];

        };

$firstColumn[0] returns "Title", "Monday", "Friday"

How can I add it in a table?

<table class="table">
   <thead>
     <tr>
      <th> Title </th>
      <th>Content</th>
      <th>Date</th>
     </tr>
    </thead>
    <tbody>
      <tr>
       <td></td>
       <td></td>
       <td></td>
      </tr>
     </tbody>
</table>

Upvotes: 1

Views: 77

Answers (3)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Do it like below:-

<?php

$days = [
    ["TITLE", "CONTENT", "DATE"],
    ["Monday", "Content of the monday post goes here", "09:55\n10:00\n10:30\n11:00\n15:45"],
    ["Tuesday", "Content of the tuesday day goes here", "08:00\n11:00\n16:00\n16:00\n21:00\n"],
]; // as you said that you got this array already
unset($days[0]);
?>
<table class="table">
   <thead>
     <tr>
      <th> Title </th>
      <th>Content</th>
      <th>Date</th>
     </tr>
    </thead>
    <tbody>
     <?php foreach($days as $day){?>
      <tr>
       <td><?php echo $day[0];?></td>
       <td><?php echo $day[1];?></td>
       <td><?php echo $day[2];?></td>
      </tr>
     <?php }?>
     </tbody>
</table>

Output at my local end:- https://prnt.sc/h8nit6

Regarding your question:- but how can I add it into a return inside a function?. I think you want like below:-

function returnFullHtml(){
    $days = json_decode(file_get_contents('json_file'));// now you got the array what you shhown in your question input
    unset($days[0]);
    $html = '<table class="table">
        <thead>
            <tr>
                <th> Title </th>
                <th>Content</th>
                <th>Date</th>
            </tr>
        </thead>
        <tbody>';
    foreach($days as $day){
        $html .= '<tr><td>'. $day[0] .'</td><td>' . $day[1] .'</td><td>' . $day[2] .'</td></tr>';
    }
    $html .= '</tbody></table>';
    return $html;

}

Upvotes: 0

Mario Rawady
Mario Rawady

Reputation: 871

Use the below to render the table from any given json obj

<?php
if (sizeof($days) > 0) { //If json is not empty
    $firstColumn = $days[0] ; //First Column (<thead> values)
    ?>
    <table>
        <thead>
            <tr>
                <?php foreach ($firstColumn AS $k => $v) { ?>
                    <th><?php echo $v?></th>
                <?php } ?>
            </tr>
        </thead>
        <tbody>
            <?php
            foreach($days as $k => $v) {
                if ($k != 0) { ?>
                    <tr>
                    <?php
                    foreach ($v AS $k1 => $v1) { ?>
                        <td><?php echo $v1 ?></td>
                    <?php
                    } ?>
                    </tr>
                <?php
                }
            } ?>
        </tbody>
    </table>
    <?php
} ?>

Upvotes: 2

julianstark999
julianstark999

Reputation: 3616

You can try it like this

echo '<table class="table">';
    $items = json_decode(file_get_contents('json_file'));
    $headers = array_shift($items);

    echo '<thead>';
    foreach($headers as $value){
        echo '<th>'.$value.'</th>';
    }
    echo '</thead>';

    echo '<tbody>';
    foreach($items as $item){
        echo '<tr>';
        foreach($item as $value){
            echo '<td>'.$value.'</td>';
        }
        echo '</tr>';
    };
    echo '</tbody>';
echo '</table>';

Upvotes: 0

Related Questions