May 16, 201412 yr Hi there,I'm having an issue with my code and I cannot for the life of me figure out what I'm doing wrong.The best way is probably to just show you and provide a list, so here is a link to the test site:http://uberdelic.com/abtest/index.phpAnd here's your list: When I add an item to the cart everything works fine apart from the item number is 0 If i add another item, the new items number is also 0, next is 1, 2, 3 and so on. Now if i try and empty the cart, it does nothing....but click twice and the cart empties? And I'll paste my cart.php code below. Thankyou to anyone whoactually looks through this, cos it's a mess and as it's not an easyone, or at least I haven't found it to be.... encouraging huh? <?php session_start(); include 'header.php'; ?> <div id="uber-body"> <h1>My cart</h1> <?php ///This section is for pulling the cart id and +1 if(isset($_POST['pid'])){ $pid = $_POST['pid']; $wasFound = false; $i = 0; if(!isset($_SESSION["cart_array"])||count($_SESSION['cart_array'])<1){ $_SESSION["cart_array"]=array(0 => array("id" => $pid, "quantity" => 1)); }else{ foreach($_SESSION["cart_array"] as $each_item){ $i++; while(list($key,$value)=each($each_item)){ if($key == "id"&&$value==$pid){ array_splice($_SESSION['cart_array'],$i-1,1, array(array("id" => $pid, "quantity" => $each_item['quantity']+1))); $wasFound=true; } } } if($wasFound == false){ array_push($_SESSION["cart_array"], array("id"=>$pid,"quantity" => 1)); } } } ?> <div id="cart-products"> <a href="#bottomview"></a> <?php //Render the cart $cartOutput=""; $cartTotal=""; $pp_checkout_btn=""; $product_id_array=""; if(!isset($_SESSION["cart_array"])||count($_SESSION['cart_array'])<1){ $cartOutput="<h3 align='center'>your cart is empty</h3>"; }else{ $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="uberdelic@gmail.com">'; $i=0; foreach($_SESSION["cart_array"] as $each_item){ $id = $each_item['id']; $prod_name = $each_item['prod_name']; $prod_price = $each_item['prod_price']; $prod_short = $each_item['prod_short']; $prod_delivery = $each_item['prod_delivery']; $prod_sku = $each_item['prod_price']; $prod_img = $each_item['prod_img']; $result = $db_conx->query("SELECT * FROM wallhangings WHERE id='$id' "); while($rows = $result->fetch_array()) { $prod_id=$rows["id"]; $prod_name=$rows["prod_name"]; $prod_short=$rows["prod_short"]; $prod_price = $rows["prod_price"]; $prod_img = $rows["prod_img"]; $prod_delivery = $rows["prod_delivery"]; $prod_sku = $rows["prod_sku"]; $producttotal=$rows["prod_price"]+$rows["prod_delivery"]; $subtotal=$prod_price*$each_item['quantity']; $subsubtotal=$prod_delivery*$each_item['quantity']; $pricetotal=$subtotal+$subsubtotal; $x=$i+1; $pp_checkout_btn.='<input type="hidden" name="item_name_'.$x.'" value="'.$prod_name.'"> <input type="hidden" name="amount_'.$x.'" value="'.$producttotal.'"> <input type="hidden" name="on0" value="'.$prod_sku.'"> <input type="hidden" name="quantity_'.$x.'" value="'.$each_item['quantity'].'">'; $product_id_array.="$prod_id-".$each_item['quantity'].","; } $cartTotal = $pricetotal+$cartTotal; $cartOutput.="<div class='cart_prod'><strong>ITEM $i</strong>"; //$cartOutput.= "item id".$each_item['id']."<br />"; $cartOutput.= "<div class='cartimg'><img src='".$prod_img."'/></div><br />"; $cartOutput.= "product name:<h4 class='prod'><br />".$prod_name."</h4></p>"; $cartOutput.= "discription: ".$prod_short."<br />"; $cartOutput.= "price(per item:) £".$prod_price."<br />"; $cartOutput.= "quantity: ".$each_item['quantity']."<br />"; $cartOutput.= "delivery charge: £".$prod_delivery."<br />"; $cartOutput.= "product sku: ".$prod_sku."<br />"; $cartOutput.= "total price: £".$pricetotal."<br />"; $cartOutput.= '<form class="prod_del" action="cart.php#bottomview" method="post"> <input name="index_to_remove" type="hidden" value="'. $i .'"/> <input name="deletebtn'.$id.'" type="submit" value="delete"/> </form></div>'; //while(list($key,$value)=each($each_item)){ // $cartOutput.="$key:$value<br />"; $i++; echo $cartOutput; } $pp_checkout_btn.='<input type="hidden" name="custom" value=".$product_id_array."> <input type="hidden" name="notify_url" value="http://www.uberdelic.com/new/storescripts/my_ipn.php"> <input type="hidden" name="return" value="http://www.uberdelic.com/new/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="http://www.uberdelic.com/new/paypal_cancels.php"> <input type="hidden" name="lc" value="GB"> <input type="hidden" name="currency_code" value="GBP"> <input class="paypal_btn" type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!"> </form>'; } ?> <?php // if item removed if(isset($_POST['index_to_remove'])&&$_POST['index_to_remove']!=""){ $key_to_remove = $_POST['index_to_remove']; if(count($_SESSION["cart_array"])<=1){ unset($_SESSION["cart_array"]); }else{ unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION['cart_array']); } } ?> <?php ///This section is for emptying the ubercart if(isset($_GET['cmd'])&&$_GET['cmd']=="emptycart"){ unset($_SESSION["cart_array"]); } ?> </div> <div id="paynow"> <p class="center"> <strong> Total: £<?php echo $cartTotal; ?> </strong> </p> <?php echo $pp_checkout_btn; ?> <p class="center"> <a href="cart.php?cmd=emptycart#bottomview"> Click here to empty your cart </a> </p> <div class="clear"></div> </div> </div> <?php include 'footer.php'?>
June 6, 201412 yr It looks very over complicated to me I would suggest just storing a product ID and qty in session So on each $_POST submission you would do this if($_POST['item_id']){ session_start() $itm_id = $_POST['item_id']; $qty = $_POST['qty']; $_SESSION['cart']['items'][$itm_id]+=$qty; } Then use MYSQL to query the DB against the ID's in the cart to show the Items. Drop me a PM if your struggling T
Create an account or sign in to comment