I´m looking for some help about my shopping cart. I use a simple script. Everything works as I want, but I don´t know how to find out if some products already exist, if they do I want to increment the value. The script is below:
for ($x=0;$x<count( $_SESSION["sess_name"]);$x++) {
$x_id= $_SESSION["sess_id"];
$x_name_prd= $_SESSION["sess_name_prd"];
$x_number_prd= $_SESSION["sess_number_prd"];
//if there is a product that is already in the cart then go to function check_me(); to do something to make the sess_number_prd +1 everytimes people click on the button.
if ( $x_name_prd[$x]==$name_prd){
check_me($x_name_prd[$x]);
}
}
I have tried this method, but does not work
function check_me ($name_prd) {
for ($i=0;$i<count($_SESSION['sess_name']);$i++) {
if (!in_array($_SESSION['sess_id'][$i],$)) {
$temp_id[]=$_SESSION['sess_id'][$i];
$temp_name[]=$_SESSION['sess_name_prd'][$i];
$temp_num[]=+1;
}
}
$_SESSION['sess_id']=$temp_id;
$_SESSION['number_prd']=+1;
$_SESSION['sess_name']=$temp_name;
}
Can someone help me please..
If you’ve some control over the structure of the array stored in $_SESSION for your basket I’d recommend using something like this.
This approach doesn’t require the iterative comparison of each item in the basket to determine if an instance of it has already been added to the basket. This will allow for a simple
to process the basket once you’re finished adding to it.
Code posted in the comments by Alex, example of the problem not a solution
End of comment code