I'm trying to get an individual element of my array (the product ID) then use a MYSQLi SELECT statement to get the additional information about the product. I've used print_r for testing purposes and everything is stored in the array correctly. I just can't work out how to select the individual item from a nested array in a Session variable and then use that to put it in a SQL statement and then loop through to display the items, as this is for a shopping cart.
Also I tried using mysqli_fetch_array and mysqli_fetch_assoc and both ways it comes up with an error Here is my code:
if (isset($_GET['add'])) {
echo "true";
$pid = $_GET['add'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION['cart_array']) || count($_SESSION['cart_array']) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION['cart_array'] = array(0 => array('item_id' => $pid, 'quantity' => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION['cart_array'] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == 'item_id' && $value == $pid) {
// That item is in cart already so let’s adjust its quantity using array_splice()
array_splice($_SESSION['cart_array'], $i-1, 1, array(array('item_id' => $pid, 'quantity' => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
//print_r ($_SESSION["cart_array"]);
$sql = "SELECT * FROM `products` WHERE `productID` = '$pid'";
$result = mysqli_query(Database::dbConnect(),$sql);
while($row = mysqli_fetch_array($result)) {
$str = <<<EOD
<tbody>
<tr>
<td>{$row['productName']}</td>
<td>{$row['productID']}</td>
<td>'quantity'</td>
<td>{$row['price']}</td>
</tr>
</tbody>
EOD;
echo $str;
}
}
}