基本上我已经按照教程在 php 中创建了一个与 mysql 数据库链接的基本购物车系统。这是教程的链接
我能够使用 switch 语句在产品页面上显示正确的尺寸选项/下拉框。(通过管理面板在后端添加调整大小的选项)。
产品.php:
<?php
switch ($sizing) {
case "Womens_t":
echo '<select name= "size">
<option value="xsmlsml">X-Small - Small</option>
<option value="smlmed">Small - Medium</option>
<option value="medlge">Medium - Large</option>
</select>';
break;
case "Womens_b":
echo '<select name= "size">
<option value="8">AUS 8</option>
<option value="10">AUS 10</option>
<option value="12">AUS 12</option>
<option value="14">AUS 14</option>
</select>';
break;
case "Mens_t":
echo '<select name="size">
<option value="xsmlsml">X-Small - Small</option>
<option value="smlmed">Small - Medium</option>
<option value="medlge">Medium - Large</option>
</select>';
break;
case "Mens_b":
echo '<select name="size">
<option value="8">AUS 8</option>
<option value="10">AUS 10</option>
<option value="12">AUS 12</option>
<option value="14">AUS 14</option>
</select>';
break;
case "":
echo '';
break;
}
?><input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Add to cart" />
<div class="table">
</form>
当它连接到 cart.php 页面时,它会出现一个错误,指出“size”是一个未定义的变量(第 135 行),它位于呈现购物车供用户查看的代码下方:
第 135 行
$cartOutput .= '<td>' . $size . '</td>';
所以我不确定大小是否只是在开始时没有定义,或者我是否必须将它从 cart_array 中拉出来?我有点难过,我还没有找到任何解决方案。我试过改变变量,但没有任何效果。
这是 cart.php 代码:
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$size = $_POST['size'];
$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, "size" => $size, "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"&&"size" && $value == $pid&&$size) {
// 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, "size" => $size, "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,"size" => $size, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
然后这是呈现购物车供用户查看的代码:
$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
// Start PayPal Checkout Button
$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="email@email.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 id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
}
$pricetotal = $price * $each_item['quantity'];
$cartTotal = $pricetotal + $cartTotal;
setlocale(LC_MONETARY, "en_AUS");
$pricetotal = number_format($pricetotal, 2, '.',' ');
// Dynamic Checkout Btn Assembly
$x = $i + 1;
$pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
<input type="hidden" name="amount_' . $x . '" value="' . $price . '">
<input type="hidden" name="on0_' . $x . '" value="size">
<input name="size" type="hidden" value="' . $each_item['size'] . '" />
<input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> ';
// Create the product array variable
$product_id_array .= "$item_id-".$each_item['quantity'].",";
// Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.png" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
$cartOutput .= '<td>' . $details . '</td>';
$cartOutput .= '<td>' . $size . '</td>';
$cartOutput .= '<td>$' . $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>' . $each_item['quantity'] . '</td>';
$cartOutput .= '<td>' . $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++;
}
setlocale(LC_MONETARY, "en_AUS");
$cartTotal = number_format($cartTotal, 2, '.',' ');
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="127.0.0.1/storescripts/my_ipn.php">
<input type="hidden" name="return" value="127.0.0.1/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="127.0.0.1/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_AUS/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}