I'm learning PHP and doing a basic eCommerce shopping cart for practice. I came across an issue that's stumped me. I made an array to display the current amount of a single item the shopper has in their cart, but rather than displaying the value it displays "array."
Below is what I'm using:
<?php
if(isset($_SESSION['Cart'])){
$sql="SELECT * FROM products where Product_ID IN (";
foreach($_SESSION['Cart'] as $id => $value){
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY Category ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
?>
<p><?php echo $row['Name']?> x <?php echo $_SESSION['Cart'][$row['Product_ID']['quantity']] ?></p>
<?php
}
}else{
echo "<p>There are no products selected</p>";
}
?>
I'm not sure if that's where the exact issue is though. The rest of my site is sourced here(Index) and here(Product page) if anyone would be so kind as to troubleshoot with me.
Based on your feedback if trying print_r($_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity])
gives you Array ( [quantity] => 1 )
Then you can echo $_SESSION['Cart'][$row['Product_ID']['quantity']]['quantity]
to give you 1
But i don't think this was the original intention. i think your real problem is from when you assign values to the array, there is a possible mismatching of keys.
Try the code above and lets know if it helps
I think there is a problem with this line of code:
$_SESSION['Cart'][$row['Product_ID']['quantity']]
You are getting a value from $_SESSION['Cart']
with a dimensional array of $row
there. I think $row['Product_ID']
is not an array but instead a string
or int
so calling $row['Product_ID']['quantity']
is an issue.