user3277691
user3277691

Reputation: 11

Array increment in session

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

Upvotes: 0

Views: 78

Answers (1)

John Conde
John Conde

Reputation: 219894

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;

Upvotes: 1

Related Questions