I am trying to make a shopping cart with Session items So basically, one person will click "Add to cart" button below a product and, the productId is going to get pushed into the s_session array.
Next step I did was show the products the user has selected and make a MySQL connection, and select from database only the IDs (Products) he selected
foreach($_SESSION['cartarray'] as $one){
$sql = "SELECT * FROM (subproducts) WHERE spId=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "s", $one);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
var_dump($row);
}
mysqli_close($conn);
Right now when I var_dump($row);
I get good result like this
array(4) { ["spId"]=> int(12) ["spName"]=> string(14) "60USD GIFTCARD" ["spMain"]=> int(21) ["spPrice"]=> string(1) "6" } array(4) { ["spId"]=> int(12) ["spName"]=> string(14) "60USD GIFTCARD" ["spMain"]=> int(21) ["spPrice"]=> string(1) "6" } array(4) { ["spId"]=> int(11) ["spName"]=> string(14) "50USD GiftCard" ["spMain"]=> int(21) ["spPrice"]=> string(1) "5" }
But I can not find any way to echo my products I'm using this code right now
<?php foreach($row as $cartitem): ?>
<tr>
<td><?php echo $cartitem['spId'] ?></td>
<td><?php echo $cartitem['spName'] ?></td>
<td><?php echo $cartitem['spPrice'] ?></td>
</tr>
<?php endforeach; ?>
I am getting this error:
Trying to access array offset on value of type int in C:\xampp\htdocs\bulk\cart.php on line 42
Line 42 is the echo part: <?php echo $cartitem['spId'] ?>