www.webdeveloper.com
Results 1 to 4 of 4

Thread: Same product id, quantity not growing

  1. #1
    Join Date
    Feb 2014
    Posts
    1

    Same product id, quantity not growing

    When I add the product twice, it keep displaying in a new line, with new quantity. I want the quantity to update. what I'm doing wrong? here's the code:

    Code:
     <?
    include "functions.php";
    if(!$_SESSION['logged']){die();}
    $_POST['taxes']=str_replace('undefined,','',$_POST['taxes']);
    if(empty($_SESSION['products']))$_SESSION['products']=array();
     
        if($_POST['action']=='add'){
        $_POST[$qty]=str_replace(',','.',$_POST[$qty]);
            $_SESSION['products'][]=$_POST;
        }
        if($_POST['action']=='delete'){
            unset($_SESSION['products'][$_POST['id']]);
        }    
     
        if($_POST['action']=='update'){
            if($_POST['rule']==0)$_SESSION['products'][$_POST['id']]['qty']-=1;
            if($_POST['rule']==1)$_SESSION['products'][$_POST['id']]['qty']+=1;
        }
    ?>
    thanks.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    878
    As I write this, you have 216 views and not one response. That might indicate that nobody understands what you are asking.

  3. #3
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    16
    much of this answer will be out of assumptions, so please excuse me if I have misunderstood.

    based on what I can see, when you add a new product it goes into $_SESSION['products'][], therefore lets say you added 3 products of varying quantities product Id 567 = 3 units, product Id 458 = 5 units and product Id 1452= 2 units. You would end up with this:
    $_SESSION['products'][0][567]['qty'] = 3;
    $_SESSION['products'][1][458]['qty'] = 5;
    $_SESSION['products'][2][1452]['qty'] = 2;
    when you update product 458 with an extra unit, you currently update this :
    $_SESSION['products'][458]['qty']+=1;
    not this :
    $_SESSION['products'][1][458]['qty']+=1;
    you have 2 options to fix it you can merge the two arrays when adding a new item :
    Code:
        if($_POST['action']=='add'){
          $_POST[$qty]=str_replace(',','.',$_POST[$qty]);
            $_SESSION['products'] = isset($_SESSION['products'] ? array_merge($_SESSION['products'],$_POST) : $_POST);
        }
    or when updating do a loop to find the correct item to update :
    Code:
       if($_POST['action']=='update'){
          foreach($_SESSION['products'] as $index=>$details){
              if($details['id'] == $_POST['id']){
                       if($_POST['rule']==0)$_SESSION['products'][$index][$_POST['id']]['qty']-=1;
                  elseif($_POST['rule']==1)$_SESSION['products'][$index][$_POST['id']]['qty']+=1;
              }
          }
           
        }
    I have not tested any of the code, so please check it.
    Hope this helps

  4. #4
    Join Date
    Feb 2014
    Location
    south africa
    Posts
    16
    any progress yet ?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles