0

I am trying to generate the alert window if condition is already set. But when the add button is click, the code automatically generate the Alert Window. I am expecting that the window prompt should happen after the item is already added to the cart. In which, if the ID exist in the array, the window prompt is populated upon the session being set. Can someone help me identify what I may be doing wrong with the code shown below?

                            
                            if (isset($_SESSION['cart'])) {

                                 $item_array_id = array_column($_SESSION['cart'], 'id'); //Check if item is already added to cart. 

                                 if (in_array($row['id'], $item_array_id)){//execute if item is already added to cart. 
                                    echo "<script> alert('Product is already added to cart');</script>";
                                    echo "<script> alert('window.location='index.php'</script>";
                                }
                           }
                                                            
                        }else{
                                $item_array = array('id' => $row['id']); 
                                $_SESSION ['cart'][0] = $item_array;
                        }
LEGO CODE
  • 1
  • 2
  • 3
    You have a javascript error in the second echo: a parenthesis is missing before the . – NoDataFound Aug 20 '22 at 20:06
  • This is a php and not Javascript file. – LEGO CODE Aug 20 '22 at 20:13
  • But you still have a javascript error in the PHP echo that's will, in the end, generate javascript. – NoDataFound Aug 20 '22 at 22:18
  • There are two JS errors, 1. no closing parenthesis, 2. can't use same quotes inside the encapsulated string without escaping them. – user3783243 Aug 20 '22 at 22:45
  • You should have `$_SESSION ['cart'][0] = $item_array;` after the `if (in_array($row['id'], $item_array_id))` in that case, right? – user3783243 Aug 20 '22 at 22:49
  • @user3783243 - The change is unsuccessful, I would think that $_Session['cart'][0] = $item_array_id)) needs to be declare before use. – LEGO CODE Aug 21 '22 at 00:16
  • You currently are adding the `id` then checking if the `id` is present, it always will be present in that scenario..unless I'm misreading the flow here. You moved it after the closing `if`? Does `$_SESSION ['cart']` not exist already? I also think you would use `$_SESSION ['cart'][]` and not the `0` index as well so it is always being appended. – user3783243 Aug 21 '22 at 00:24
  • @user3783243 - I have updated the code as shown in the main file but the issue now is that the alert window is not populating. I do not think the code is getting to the inner condition set once session is established. – LEGO CODE Aug 21 '22 at 12:01
  • Do you have `customers` and `cart` tables in your DB? If yes, please track the `customer` against his `cart_id` to check whether the customer has added that product to his cart or not, instead of checking through the session. – Tabassam Ali Aug 21 '22 at 16:30

1 Answers1

0

You're trying check that item is already exist or not, in that scenario you've to check is it by intersecting the array and count the number of intersecting products as if product is already added it'll populate alert as well as redirect to index.php

if (isset($_POST['add'])) { //Action if add button is clicked.
    $item_array = array('id' => $row['id']);
    //new session assigned variable $item to array. 
    $_SESSION['cart'][0] = $item_array;

    if (isset($_SESSION['cart'])) {

        $item_array_id = array_column($_SESSION['cart'], 'id'); //Check if item is already added to cart. 

        if (in_array($row['id'], $item_array_id)) {//execute if item is already added to cart. 
            if(count(array_intersect($item_array_id, $item_array)) >= 2 ) {
                echo "<script> alert('Product is already added to cart');</script>";
                echo "<script> window.location='index.php';</script>";
            }
        }

    }
} 

You can also check this question : in_array multiple values

Kamran Allana
  • 543
  • 1
  • 6
  • 25
  • This solution is unsuccessful. I am trying to check if the item already exist in the array [0]. If it is in the array, then an alert window would prompt that item already exist in the array utilize the product ID. – LEGO CODE Aug 20 '22 at 22:05
  • For that you have to first check before assigning to session as you were assigning first and checking from the same that's why it were populate automatically. – Kamran Allana Aug 21 '22 at 15:00