TrevorM Posted September 4, 2014 Share Posted September 4, 2014 (edited) HI, Im trying to adapt a shopping cart script for an alternative use, on my local host XXMP scrip code works fine, but when i upload to my host I have problems, Im making a Role-play page to assist with npc encounters, where you can add npc's to the encounter (Shopping Cart) see http://rptoolkit.com/encounter/ which is active and working The issue is when I go to remove 1 of the 4 people from the encounter (Cart) it will remove all and duplicate the first entry on 2 lines, where this doesn't happen on my localhost. Hope you can help shed some light as I'm confused why it will work on 1 but not the other maybe has to do with my PHP Versions. Thanks. ---------------------------------------------------- Script code is below for the remove link button on the index page echo '<td><span> <a href="include/encounter_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'"><img src="images/icon_delete.gif" alt="Delete"></a> </span></td>'; Script below is the encounter update script (encounter_update.php) <?php session_start(); include_once("config.php"); //empty cart by distroying current session if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1) { $return_url = base64_decode($_GET["return_url"]); //return url session_destroy(); header('Location:'.$return_url); } //add item in encounter if(isset($_POST["type"]) && $_POST["type"]=='add') { $id = filter_var($_POST["id"], FILTER_SANITIZE_STRING); //product code $product_qty = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code $return_url = base64_decode($_POST["return_url"]); //return url //limit quantity for single product if($product_qty > 10){ die('<div align="center">This demo does not allowed more than 10 quantity!<br /><a href="http://sanwebe.com/assets/paypal-shopping-cart-integration/">Back To Products</a>.</div>'); } //MySqli query - get details of item from db using product code $results = $mysqli->query("SELECT chrname,initmod FROM products WHERE id='$id' LIMIT 1"); $obj = $results->fetch_object(); if ($results) { //we have the product info //prepare array for the session variable $new_product = array(array('name'=>$obj->chrname, 'code'=>$id, 'qty'=>$product_qty, 'initmod'=>$obj->initmod)); if(isset($_SESSION["products"])) //if we have the session { $found = false; //set found item to false foreach ($_SESSION["products"] as $cart_itm) //loop through session array { if($cart_itm["code"] == $id){ //the item exist in array $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'initmod'=>$cart_itm["initmod"]); $found = true; }else{ //item doesn't exist in the list, just retrive old info and prepare array for session var $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'initmod'=>$cart_itm["initmod"]); } } if($found == false) //we didn't find item in array { //add new user item in array $_SESSION["products"] = array_merge($product, $new_product); }else{ //found user item in array list, and increased the quantity $_SESSION["products"] = $product; } }else{ //create a new session var if does not exist $_SESSION["products"] = $new_product; } } //redirect back to original page header('Location:'.$return_url); } //remove item from encounter if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])) { $id = $_GET["removep"]; //get the product code to remove $return_url = base64_decode($_GET["return_url"]); //get return url foreach ($_SESSION["products"] as $cart_itm) //loop through session array var { if($cart_itm["code"]!=$id){ //item does,t exist in the list $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'initmod'=>$cart_itm["initmod"]); } //create a new product list for cart $_SESSION["products"] = $product; } //redirect back to original page header('Location:'.$return_url); } ?> Edited September 4, 2014 by mac_gyver code tags please Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 4, 2014 Share Posted September 4, 2014 i have a recommendation that will greatly reduce the amount of code. use the item id/code as the array index. this will mean that you can directly test for or access the element in the array. you should also only store the quantity in the cart. the rest of the information is redundant and if you ever allow the chrname or initmod values to be edited or you add more columns of data, you will end up with out of sync data. Quote Link to comment Share on other sites More sharing options...
TrevorM Posted September 5, 2014 Author Share Posted September 5, 2014 Hi mac_gyver Thanks for the advise I will look into changing the array, I have downloaded a sample script which I have edited the values and changed few details, I have a good understanding of basic to med level PHP script but not to the point I could write the Shop cart code script but this is generally how I learn new things by seeing how others have managed to get it to work, In regards to fixing my problem anything you can suggest to why it doesn't delete a single item from the cart pool rather then duplicating the first line and deleting all else? Thanks again for your response. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 5, 2014 Share Posted September 5, 2014 the most likely cause of the symptom is because the following line - $_SESSION["products"] = $product; is inside the foreach(){} loop and is adding elements to the array that the loop is iterating over. Quote Link to comment Share on other sites More sharing options...
TrevorM Posted September 5, 2014 Author Share Posted September 5, 2014 No problems, thanks for your help , I will look into it and also the prior mentioned advise. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.