Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP购物车-根据价格添加产品_Php_Shopping Cart - Fatal编程技术网

PHP购物车-根据价格添加产品

PHP购物车-根据价格添加产品,php,shopping-cart,Php,Shopping Cart,我正在建立一个简单的购物车,用户可以选择订购比萨饼,并从三个选项中选择小型、中型和大型。选中后,我希望将产品放置在购物车中并计算总数。我的代码只允许我接受小披萨 <form id="form1" name="form1" method="post" action="cart.php"> <label>Small - <?php echo $small_price; ?></label>

我正在建立一个简单的购物车,用户可以选择订购比萨饼,并从三个选项中选择小型、中型和大型。选中后,我希望将产品放置在购物车中并计算总数。我的代码只允许我接受小披萨

            <form id="form1" name="form1" method="post" action="cart.php">
                <label>Small - <?php echo $small_price; ?></label>
                <input type="hidden" name="pid" id="pid" value="<?php echo $product_id; ?>" />
                <input type="hidden" name="price" id="price" value="<?php echo $small_price; ?>" />
                <input type="submit" name="button" id="button" value="Add" class="btn btn-default" />
                </form>

                <form id="form2" name="form2" method="post" action="cart.php">
                <label>Medium - <?php echo $medium_price; ?></label>
                <input type="hidden" name="pid2" id="pid2" value="<?php echo $product_id; ?>" />
                <input type="hidden" name="medium_price" id="medium_price" value="<?php echo $medium_price; ?>" />
                <input type="submit" name="button" id="button" value="Add" class="btn btn-default" />
            </form>





            if (isset($_POST['pid']) || isset($_POST['pid2'])) {  //the id of the item being added to the cart
                $pid = $_POST['pid'];
               // $pid2 = $_POST['pid2'];

                $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 this code 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) {
                                  // 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, "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();
            }


            /**
            * Section 2 (if user chooses to empty their shopping cart)
            **/
            if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
                unset($_SESSION["cart_array"]);
            }


            /***
            * Section 3 (if user chooses to adjust item quantity)
            ***/

            if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") {
                // execute some code
                $item_to_adjust = $_POST['item_to_adjust'];
                $quantity = $_POST['quantity'];
                $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers
                if ($quantity >= 100) { $quantity = 99; }
                if ($quantity < 1) { $quantity = 1; }
                if ($quantity == "") { $quantity = 1; }
                $i = 0;
                foreach ($_SESSION["cart_array"] as $each_item) { 
                          $i++;
                          while (list($key, $value) = each($each_item)) {
                              if ($key == "item_id" && $value == $item_to_adjust) {
                                  // 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" => $item_to_adjust, "quantity" => $quantity)));
                              } // close if condition
                          } // close while loop
                } // close foreach loop
            }


            /*
            * Section 5  (render the cart for the user to view on the page)
            */

            $cartOutput = "";
            $cartTotal = "";
            $pp_checkout_btn = '';
            $pid_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="you@youremail.com">';


                // Start the For Each loop
                $i = 0; 

                foreach ($_SESSION["cart_array"] as $each_item) { 

                    $item_id = $each_item['item_id'];

                    $sql = mysqli_query($conn, "SELECT * FROM product WHERE product_id='$item_id' LIMIT 1");

                    while ($row = mysqli_fetch_array($sql)) {
                        $pid = $row["product_id"];
                        $product_name = $row["product_name"];
                        $small_price = $row["small_price"];
                        $medium_price = $row["medium_price"];
                        $large_price = $row["large_price"];
                        $product_category = $row["product_category"];
                        $product_desc = $row["product_desc"];
                    }

                    $pricetotal = $small_price  * $each_item['quantity']; 
                    $cartTotal = $pricetotal + $cartTotal;
                    setlocale(LC_MONETARY, "en_JA");
                    $pricetotal = money_format("%10.2n", $pricetotal);

                    // 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="' . $small_price . '">
                    <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';


                    // Create the product array variable
                    $pid_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/' . $product_name . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';


                    $cartOutput .= '<td>' . $product_desc . '</td>';


                    /*if ($small_price ) {
                        $cartOutput .= '<td>$' . $small_price  .'</td>';
                    }

                    else if ($medium_price ){
                        $cartOutput .= '<td>$' . $medium_price  .'</td>';
                    } 
             */
                    $cartOutput .= '<td>$' . $small_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" class="btn btn-default"/>
                    <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++; 

            } 

$pid=$_POST['pid'];-好吧,如果他们选择中等,它将是$pid=$_POST['pid2'];但这从未分配?我分配了它,但我无法让购物车根据该选择进行计算。在发布的代码中,我只看到$pid=$\u POST['pid'];-如果我单击了中间项,它不需要设置$pid=$\u POST['pid2'];?看起来这已经被注释掉了。