将产品尺寸添加到购物车

时间:2013-03-05 10:46:02

标签: php store cart shopping

基本上我已经按照教程在php中创建了一个基本的购物车系统,它连接了mysql数据库。 This is the link to the tutorial

我能够使用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>';
}

1 个答案:

答案 0 :(得分:0)

你有一个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/