繁体   English   中英

将产品尺寸添加到购物车

[英]adding product size to cart

基本上,我已经按照教程创建了一个用php与MySQL数据库链接的基本购物车系统。 这是本教程的链接

我可以使用switch语句在产品页面上显示正确的尺寸选择/下拉框。 (通过管理面板在后端添加了大小调整选项)。

product.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>';
}

您有一个foreach循环,该循环遍历项目的会话数组。 您忘了在这里定义$size

foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id'];
    $size = $each_item['size'];         //Here :)

    $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"];
     }

同样,用这样的查询(单独循环)轰炸数据库是一个坏主意:

$item_IDs = [];
foreach($_SESSION['cart_array'] as $each_item) {
    $item_IDs[] = $each_item['item_id'];
}

//Then the query becomes
SELECT * FROM products WHERE ID IN(". explode(',', $item_IDs) .") //and use this result

并使用mysqli或PDO代替mysql_ ,因为不使用 mysql_ :) http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM