1

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.

8
  • 1
    so, what's not working here exactly? Commented Dec 15, 2015 at 14:43
  • I dont realy know how to get price from $item['prices'] for each cell.. Commented Dec 15, 2015 at 14:45
  • 2
    I think you should use echo or print instead of print_r Commented Dec 15, 2015 at 14:45
  • Robert, print_r is not a problem :) Commented Dec 15, 2015 at 14:45
  • so, what gets printed and what doesn't? edit: you have an answer below. Commented Dec 15, 2015 at 14:46

1 Answer 1

1

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>";
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

it's happening in foreach()
Yes thank you :), now its closing. But its not the problem, the problem is to get prices from array for each cell. So i can get price from cell "Quant1 \ Format1" etc..
print_r or echo or print is not a problem. I just need to get a value from prices to each cell...

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.