I am new to PHP and i am making a shopping cart for my website. The problem is when i click on the button "ADD TO SHOPPING CART" It goes to the cart.php page where i am trying to display the product name, details, price, quantity to adjust , total price, remove the item but it is not display all the details about the product it only shows me the quantity. Here is my code to display the cart product.
$cartOutput="";
$cartTotal="";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1)
{
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
}
else
{
$i=0;
foreach($_SESSION["cart_array"] as $each_item)
{
$i++;
$item_id = $each_item['item_id'];
$result = mysqli_query($con, "SELECT * FROM products WHERE id='$item_id' LIMIT 1 ");
if($result == FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($row = mysqli_fetch_array($result))
{
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
// Dynamic Table row assembly
$cartOutput .= '<tr>';
$cartOutput .= '<td align=\'center\' >' . $product_name . '<br/></td>';
$cartOutput .= '<td align=\'center\' >' . $details . '</td>';
$cartOutput .= '<td align=\'center\' >' . $price . '</td>';
$cartOutput .= '<td>
<form action="cart.php" method="post">
<input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input name="adjustBtn' . $item_id . '" type="submit" value="change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form>
</td>';
//$cartOutput .= '<td align=\'center\' >' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td align=\'center\' >' . $pricetotal . '</td>';
$cartOutput .= '<td> <form action="cart.php" method="post">
<input name="deleteBtn' . $item_id . '" type="submit" value="X" />
<input name="index_to_remove" type="hidden" value="' . $i . '" /></form> </td>';
$cartOutput .= '</tr>';
$i++;
}
}
(if user attempts to add something to the cart from the product page)
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$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) {
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));
}
}
header("location: cart.php");
exit();
}
to show the product
$dynamicList = "";
$result = mysqli_query($con,"SELECT * FROM products ");
$productCount = mysqli_num_rows($result); // count the output amount
if ($productCount > 0) {
while($row = mysqli_fetch_array($result)){
$id = $row["id"];
$product_name = $row["product_name"];
$product_code = $row["product_code"];
$availability = $row["availability"];
$price = $row["price"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$details = $row["details"];
$date_added = strftime("%b %d, %Y",
strtotime($row["date_added"]));
// To show latest items on the home page
$dynamicList .= ' <div class="product-items">
<div class="product-block">
<div class="product-block-inner">
<div class="image">
<a href="product.php?id=' . $id . '"><img
src="product_images/' . $id . '.jpg" /> </a>
</div>
<div class="name">
<a href="product.php?id=' . $id . '">' .
$product_name . '</a>
</div>
<div class="price">
RS. ' . $price . '
<br />
Availability: ' . $availability . '
</div>
<div class="cart">
<form id="form1" name="form1"
method="post" action="cart.php">
<input type="input" name="pid" id="pid"
value="<?php echo $id; ?>" />
<input type="submit" name="button"
id="button" class="button" value="Add to Shopping Cart" />
</form>
</div>
</div>
</div>
</div>
';
}
} else {
$dynamicList = "We have no products listed in our store yet";
}