php - Remove an Item from the shopping cart on dropdown change event -


i building e-commence system user can select item category dropdown list. want user add 1 item each category , when change item category must remove 1 exist in cart. every categories items have same item id user cannot add 2 or more items same category.

i can add items shopping cart, i'm struggling make sure if item same category exists in cart must deleted first. e.g if user want add sony chase cart , lg chase in cart, lg chase must replaced sony because belong same category chassis. don't know if i'm clear

<form method="post" action="" class="jcart"> <fieldset> <input type="hidden" name="my-item-url" value="" />     <input type="hidden" name="my-item-qty" value="1"/>     <table>     <tr><td width="180"> <select name="chasis" id="chasis" onchange="grabinfo(this.value)" class="styled-select">     <option value="">-- select chasis --</option>     <?php              $querychasis = mysql_query("select * chasis");                   while ($row = mysql_fetch_array($querychasis))             {             ?>                 <option value="<?php echo $row['itemid']; ?>"><?php echo $row['cname']; ?></option>             <?php             }              ?>       </select>       </td>       </tr>       </table>       </fieldset>  </form>   <!-- ajax passing id --> function grabinfo(str) { if (str=="") {     document.getelementbyid("contentdiv").innerhtml = "";     return; } if (window.xmlhttprequest) {// code ie7+, firefox, chrome, opera, safari     xmlhttp=new xmlhttprequest(); } else {// code ie6, ie5     xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() {     if (xmlhttp.readystate==4 && xmlhttp.status==200)     {         document.getelementbyid("contentdiv").innerhtml = "&nbsp;";         document.getelementbyid("contentdiv").innerhtml = xmlhttp.responsetext;     } } xmlhttp.open("get","getinfochasis.php?q="+str,true); xmlhttp.send(); } 

if user attempts add cart product page. try remove existing item same category

if (isset($_get["q"])) { $pid = $_get["q"]; $wasfound = false; $i = 0; // if cart session variable not set or cart array empty if (!isset($_session["cart_array"]) || count($_session["cart_array"]) < 1) {      // run if cart empty or not set     $_session["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else {     // run if cart has @ least 1 item in     foreach ($_session["cart_array"] $each_item) {            $i++;           while (list($key, $value) = each($each_item)) {               if ($key == "item_id" && $value == $pid) {                    // item in cart let's remove                   $key_to_remove = $pid;                 if (count($_session["cart_array"]) <= 1) {                     unset($_session["cart_array"]);                 } else {                     unset($_session["cart_array"]["$pid"]);                     sort($_session["cart_array"]);                 }                   $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));        } } } 

render cart user view on page

$cartoutput = ""; $carttotal = ""; if (!isset($_session["cart_array"]) || count($_session["cart_array"]) < 1) { $cartoutput .= "<h2 align='center'>your shopping cart empty</h2>"; } else { $i = 0;  foreach ($_session["cart_array"] $each_item) {     $i++;     $item_id = $each_item['item_id'];     $sql = mysql_query("select * chasis id='$item_id' limit 1");     while ($row = mysql_fetch_array($sql)) {         $product_name = $row["cname"];         $price = $row["price"];     }     $carttotal = $price + $carttotal;     /*$cartoutput .= "<h2>cart item $i</h2>";     $cartoutput .= "item id: ".$each_item['item_id']."<br/>";     $cartoutput .= "quantity: ".$each_item['quantity']."<br/>";*/     $cartoutput .= "<tr><td>".$product_name."</td>";     $cartoutput .= "<td>".$price."</td></tr>"; } //total price $carttotal = $carttotal; } echo '<table border="1" cellspacing="0" cellpadding="6"> <tr bgcolor="#ccffcc"><td width="220">item name</td> <td width="120">price</td></tr>'; echo $cartoutput; echo '</table>';  echo '<table border="1" cellspacing="0" cellpadding="6"> <tr align="right"><td width="220"><strong>total price:</strong></td> <td width="120">'.$carttotal.'</td></tr></table>'; ?> 

i developed 3 ecommerce application , solved these kind of problems in standard manner.

  1. first of all, need save category id along product id of product added in cart. lets come problem. want add 1 item per category user right? suppose, have several products under juice category. if category id juice 4 example, products under juice category have 4 category id.

what can is, when user selects product dropdown, pass product id , category id using ajax in ajax page. please try using session array using session array not professional approach , if array long, can effect loading speed. can is, after passing product id , category id, can check cart using select query find if same category product exist in table. if so, run delete query delete product , run insert query insert new product. otherwise can add new product using insert query directly.

if need know more details queries need deal with, please let me know. try out as possible.

regards.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -