0

How can I write all values ​​into an HTML table?

This is the Array:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [0] => 
                    [1] => sortprice
                    [2] => sortdate
                )

            [1] => Array
                (
                    [0] => a lorem ipsum
                    [1] => 1. Preis
                    [2] => 1998
                )

            [2] => Array
                (
                    [0] => b lorem ipsum
                    [1] => 2. Preis
                    [2] => 1997
                )

            [3] => Array
               (
                    [0] => c lorem ipsum
                    [1] => 3. Preis
                    [2] => 1996
                )
        )

)

I can only output all values. Like this:

foreach ($table['data'] as $v1) {
        foreach ($v1 as $v2) {
            echo "$v2\n";
        }
    }

How can I access the values ​​$ table ['data'] [1] .. with a foreach? To write them into a table cell like this:

<table>
    <tr>
        <td>a lorem ipsum</td>
        <td>1. Preis</td>
        <td>1998</td>
    </tr>
    .....
</table>
2
  • Your question is not clear. Do you want to access the values of the $table[data] array with one foreach or do you want to access them directly? Commented Aug 21, 2017 at 7:09
  • @azrm, hope you have got my answer, if not click here Commented Aug 21, 2017 at 7:28

4 Answers 4

1

As you have column headers embedded within the data, I'd print the table header first and then loop through the rest of the data.

<?php

$table = [
    'data' => [
        [
            'Column 1',
            'Column 2',
            'Column 3'
        ],
        [
            'foo',
            'bar',
            'baz'
        ],
        [
            'big',
            'fat',
            'mamma'
        ]
    ]
];

$data = $table['data'];
?>
<table>
    <thead>
        <tr>
            <th><?= $data[0][0] ?></th>
            <th><?= $data[0][1] ?></th>
            <th><?= $data[0][2] ?></th>
        </tr>
    </thead>
    <tbody>
    <?php
    array_shift($data);
    foreach($data as $value)
    {
    ?>
        <tr>
            <td><?= $value[0] ?></td>
            <td><?= $value[1] ?></td>
            <td><?= $value[2] ?></td>
        </tr>
    <?php
    }
    ?>
    </tbody>
</table>

Output:

<table>
    <thead>
        <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
        </tr>
    </thead>
    <tbody>
            <tr>
            <td>foo</td>
            <td>bar</td>
            <td>baz</td>
        </tr>
            <tr>
            <td>big</td>
            <td>fat</td>
            <td>mamma</td>
        </tr>
        </tbody>
</table>
Sign up to request clarification or add additional context in comments.

2 Comments

yeah great. Exately what I was looking for, thank you! :) So was my mistake that I need only one foreach... ($data as $value)
@azrm, your method avoids referring to indexes. You just needed to wrap your values within table cells (see Shaunak's solution). And split the table columns from the data, if that's what you wished for.
1

Your foreach loop will be as below :

echo "<table border='1'>";
foreach ($table['data'] as $v1) { //loop for ROW
    echo "<tr>";
    foreach ($v1 as $v2) { //loop for CELL
        echo "<td>$v2</td>";
    }
    echo "</tr>";
}
echo "</table>";

Comments

0
<table>
    <tr><td>ID</td><td colspan="2"><?php echo $table['id']?></td></tr>
    <tr><td>Name</td><td colspan="2"><?php echo $table['nmae']?></td></tr>
    <tr><td>Description</td><td colspan="2"><?php echo $table['description']?></td></tr>
    <tr><td>Author</td><td colspan="2"><?php echo $table['author']?></td></tr>
    <tr><td>Last Modified</td><td colspan="2"><?php echo $table['last_modified']?></td></tr>
    <?php foreach($table['data'] as $v1){ ?>
    <tr><td><?php echo $v1[0];?></td><td><?php echo $v1[1];?></td><td><?php echo $v1[2];?></td></tr>
    <?php } ?>
</table>

You can try this.

Comments

0
<?php
if(!empty($table['data'])){ 
    echo "<table><body>";
    foreach($table['data'] as $data){
        echo "<tr>";
        echo "<td>".$data[0]."</td>";
        echo "<td>".$data[1]."</td>";
        echo "<td>".$data[2]."</td>";
        echo "</tr>";
    }
    echo "</body></table>";
}

?>

Try this Code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.