0

Array

$item= "stackoverflow";
$price = "30.00";
$total += ($sum * $price);

$cs1_array = array();
$cs1_array[] = array(
    'item'=>array('item'=>$item, 'price'=>$price, 'quantity' => $sum),
    'total' => $total
);
 $_SESSION['session_inputs'] = $cs1_array;

Foreach loop in other page

$cs1_array = $_SESSION['session_inputs'];
echo "<table>";
foreach ($cs1_array as $item){
echo "<tr>";
foreach ($item as $item2){
  echo "<td>{$item2['item']}</td><td>{$item2['price']}</td><td>{$item2['quantity']}</td>";
}

echo "<td>{$item['total']}</td>";
echo "</tr>";
}
echo "</table>";

Above the the codes that used to display the items in my array but it will only display out 1 row of output, like this

stackoverflow 30.00 2 60

What I want is whenever I submit the html form, it will add new row new value into the array and the output will be like this if I submit 3 times

stackoverflow 30.00 2 60

stackoverflow 30.00 2 60

stackoverflow 30.00 2 60

1 Answer 1

1

You're overwriting $_SESSION['session_inputs'] each time. Just make it an array and append to it:

$cs1_array = array(
    'item'=>array('item'=>$item, 'price'=>$price, 'quantity' => $sum),
    'total' => $total
);
$_SESSION['session_inputs'][] = $cs1_array;
Sign up to request clarification or add additional context in comments.

2 Comments

In that case, $cs1_array should be a one-dimensional array, not an array of arrays as the OP has specified.
That would be easier to work with. Updated my answer accordingly.

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.