DraugDev
DraugDev

Reputation: 81

Print a PHP array in an HTML table

I have some troubles with creating of my html table with data from Array. It should be a simple price table with other price for every quantity*format.

Here is my array:

Array
(
    [item] => Array
        (
            [id] => 1
            [name] => item_name
            [description] => description
            [category_id] => 2
        )

    [format] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [item_id] => 1
                    [title] => 25x140mm
                )

            [2] => Array
                (
                    [id] => 2
                    [item_id] => 1
                    [title] => 50x215mm
                )

            [3] => Array
                (
                    [id] => 3
                    [item_id] => 1
                    [title] => 25x100mm
                )

            [4] => Array
                (
                    [id] => 4
                    [item_id] => 1
                    [title] => 25x150mm
                )

        )

    [quantity] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [item_id] => 1
                    [quantity] => 100
                )

            [2] => Array
                (
                    [id] => 2
                    [item_id] => 1
                    [quantity] => 250
                )

            [3] => Array
                (
                    [id] => 3
                    [item_id] => 1
                    [quantity] => 500
                )

            [4] => Array
                (
                    [id] => 4
                    [item_id] => 1
                    [quantity] => 1000
                )

        )

    [prices] => Array
        (
            [1] => Array
                (
                    [1] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 100
                            [price] => 111.00
                        )

                    [4] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 1000
                            [price] => 114.00
                        )

                    [3] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 500
                            [price] => 113.00
                        )

                    [2] => Array
                        (
                            [format] => 25x140mm
                            [quantity] => 250
                            [price] => 112.00
                        )

                )

            [3] => Array
                (
                    [4] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 1000
                            [price] => 134.00
                        )

                    [3] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 500
                            [price] => 133.00
                        )

                    [2] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 250
                            [price] => 132.00
                        )

                    [1] => Array
                        (
                            [format] => 25x100mm
                            [quantity] => 100
                            [price] => 131.00
                        )

                )

            [2] => Array
                (
                    [4] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 1000
                            [price] => 124.00
                        )

                    [3] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 500
                            [price] => 123.00
                        )

                    [2] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 250
                            [price] => 122.00
                        )

                    [1] => Array
                        (
                            [format] => 50x215mm
                            [quantity] => 100
                            [price] => 121.00
                        )

                )

            [4] => Array
                (
                    [3] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 500
                            [price] => 143.00
                        )

                    [2] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 250
                            [price] => 142.00
                        )

                    [1] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 100
                            [price] => 141.00
                        )

                    [4] => Array
                        (
                            [format] => 25x150mm
                            [quantity] => 1000
                            [price] => 144.00
                        )

                )

        )  
)

Im getting headers for the table from $item['format'] and quantity from $item['quantity'].

Here is my html table I tried to create:

<table id="table">
        <thead>
          <tr>
            <th>Anzahl \ Format</th>
            <?php foreach ($item['format'] as $format) {  ?>
            <th><?php print_r($format['title']) ?></th>
            <?php } ?>
          </tr>
        </thead>

        <tbody>
          <?php foreach ($item['quantity'] as $quantity) {
            echo "<tr>";
            echo "<td>";
            print_r($quantity['quantity']);
            echo "</td>";
              foreach ($item['prices'] as $key => $value) {
                echo "<td>";
                print_r($value);
                echo "</td>";
              }
            echo "</tr>";
          } ?>
        </tbody>
</table>

Here is how my table should looks like: enter image description here

And here is how it looks right now: enter image description here

My problem is to get real prices for each quantity \ format.

Upvotes: 1

Views: 2145

Answers (1)

Yury Fedorov
Yury Fedorov

Reputation: 14928

After fixing syntax errors, you may try something like this:

<?php
foreach ($item['quantity'] as $quantity) {
    echo "<tr>";
    echo "<td>";
    print_r($quantity['quantity']);
    echo "</td>";
    foreach ($item['prices'] as $key => $value) {
        echo "<td>";
        // since we know a quantity for this row,
        // check each element in this price array for equality 
        foreach ($value as $price) {
            if ($price['quantity'] == $quantity['quantity']) {
                echo $price['price'];
                break; // we will not continue further
            }
        }
        print_r($value);
        echo "</td>";
    }
    echo "</tr>";
}
?>

Upvotes: 1

Related Questions