0

I have an array with a reference, a price and a quantity in an array. I have a foreach set in a html table, and I would like a line break after each quantity but I can't make it work.

 if (isset($_SESSION["buy"])) :
        $cart = $_SESSION["buy"];
    endif;

 if (!empty($_POST["quantity"])) :
     array_push($cart, $_POST['ref']);
     array_push($cart, $_POST['price']);
     array_push($cart, $_POST["quantity"]);
     $_SESSION["buy"] = $cart;
 endif;




<table>
    <thead>Your order</thead>
        <tr>
            <td>Ref</td>
            <td>Price</td>
            <td>Quantity</td>
        </tr>
        <tr>
            <?php foreach ($cart as $row => $data) : ?>
                <td>
                    <?php echo $data; ?>
                </td>
            <?php endforeach; ?>
        </tr>
   </table>

Whatever I try, the result will only be a single column or row. How can I force it to line break after each $data(quantity)

1
  • let me see the result of <?php echo $data; ?> Commented Oct 26, 2019 at 7:01

2 Answers 2

0

Firstly, it would help that when you store the details, you store them together - at the moment you are mixing all the details in the same array...

 if (!empty($_POST["quantity"])) :
     array_push($cart, $_POST['ref']);
     array_push($cart, $_POST['price']);
     array_push($cart, $_POST["quantity"]);
     $_SESSION["buy"] = $cart;
 endif;

I would suggest an associative array with the details of each item in the same element of $cart...

 if (!empty($_POST["quantity"])) {
     array_push($cart,["ref" => $_POST['ref'], 
                "price" =>$_POST['price'] 
                "quantity" => $_POST["quantity"]);
     $_SESSION["buy"] = $cart;
 }

Then to display the results, echo each line in it's own <tr> tags with each item in a <td> , you could also apply formatting to each item according to what you need...

foreach ( $cart as $item ) {
     echo "<tr><td>{$item[ref]}</td><td>{$item[price]}</td><td>{$item[quantity])</td></tr>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I added some " " there and there to make it work. I only started php this week and I was not able to do it on my own. Thank you for your code :)
0

Try This code, maybe this code will be help you :

<table>
                    <thead>Your order</thead>
                    <tr>
                        <td>Ref</td>
                        <td>Price</td>
                        <td>Quantity</td>
                    </tr>
                    <?php foreach ($cart as $row) { ?>
                    <tr>
                        <td><?= $row['ref']; ?></td>
                        <td><?= $row['price']; ?></td>
                        <td><?= $row['quantity']; ?></td>
                    </tr>
                    <?php } ?>
                </table>

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.