我是PHP的新手,我正在为我的网站制作购物车。问题是,当我点击“添加到购物车”按钮它进入cart.php页面,我试图显示产品名称,详细信息,价格,数量调整,总价格,删除项目,但它是没有显示产品的所有细节,它只显示数量。这是我显示购物车产品的代码。
$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 (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();
}
显示产品
$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";
}
答案 0 :(得分:0)
尝试这个
<?php
$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
}
$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++;
}
}
?>