/    Sign up×
Community /Pin to ProfileBookmark

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;
}
?>
[/code]

thanks.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmFeb 10.2014 — As I write this, you have 216 views and not one response. That might indicate that nobody understands what you are asking.
Copy linkTweet thisAlerts:
@BezzzZFeb 11.2014 — 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;

[/quote]




when you update product 458 with an extra unit, you currently update this :



$_SESSION['products'][458]['qty']+=1;

[/quote]


not this :



$_SESSION['products'][1][458]['qty']+=1;

[/quote]




you have 2 options to fix it you can merge the two arrays when adding a new item :

<i>
</i> 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 :
<i>
</i> if($_POST['action']=='update'){
foreach($_SESSION['products'] as $index=&gt;$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> </i>}


I have not tested any of the code, so please check it.

Hope this helps
Copy linkTweet thisAlerts:
@BezzzZFeb 19.2014 — any progress yet ?
×

Success!

Help @Tossyy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...