道歉这是我第一次这样做,我自学,想建立自己的网站。我有cart.php,我在脚本开头使用session_start()。稍后在header.php中我想在标题中包含一个按钮推车,每当用户将一个项目添加到购物车时,它将显示购物车中的总数量。
我认为当前的解决方案并不实用是我从cart.php复制并粘贴我的脚本并将其粘贴到header.php,以便我可以在cart.php中获取值$ cartquantity并在我的按钮中使用它大车。结果我两次调用session_start。
在大多数电子商务网站中,我可以用什么方法或人们用来更新标题中的按钮购物车的方法呢?
//part of my cart.php
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
$cartquantity = 0 ;
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 id=".'cartoutput'.">YOUR CART IS CURRENTLY EMPTY.</h2><br><p id=".'cartoutput1'.">Continue browsing <b><a href=".'shopallthings.php'.">here</a></b>.</p>";
} else {
// Start PayPal Checkout Button
//$paypalbutton = '<img src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-medium.png" alt="Check out with PayPal" />';
$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="you@youremail.com">';
// Start the For Each loop
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE ProductID='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$ProductName = $row["ProductName"];
$Price = $row["Price"];
$ProductShortText = $row["ProductShortText"];
$Image=$row["Image"];
}
$pricetotal = $Price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
$pricetotal = number_format($pricetotal, 2);
setlocale(LC_MONETARY, "en_US");
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $ProductName . '">
<input type="hidden" name="amount_' . $x . '" value="' . $Price . '">
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="productdetails.php?ProductID=' . $item_id . '"><img src="'.$Image.'" alt="' . $ProductName. '" width="150" height="150" /></a></td>';
$cartOutput .= '<td><h3>' . $ProductName . '</h3><form action="cart.php" method="post"><input id="deletebutton" name="deleteBtn' . $item_id . '" type="submit" value="Remove" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
$cartOutput .= '<td><p>RM' . $Price . '</p></td>';
$cartOutput .= '<td><form action="cart.php" method="post">
<input id="quantity" name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
<input id="adjustbutton" name="adjustBtn' . $item_id . '" type="submit" value="Change" />
<input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
</form></td>';
//$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td><p>RM' . $pricetotal . '</p></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>';
$cartquantity = $cartquantity + $each_item['quantity'];
$i++;
}
//header.php
<div id="cartbutton">
<a href="cart.php"><p><?php echo $cartquantity;?> items</p></a>
</div>