-1

I am implementing an Ajax Cart in PHP5 at core level with this code:

$resp = $cart->getDetails(filter_var($_POST["pid"], FILTER_SANITIZE_NUMBER_INT));

if($resp == "" OR $resp == null)
{
    echo "Some Error!";
}

elseif($resp != "" || $resp != null)
{
    while($row = $resp->fetch_assoc())
    {
        $itemArray[] = array(
            $resp => array(
                'name' => $row["product_name"],
                'id' => $row["id"],
                'discount' => $row["discount"],
                'quantity' => 1,
                'price' => $row["price"]
            )
        );
    }

    print_r($itemArray);

    if(!empty($_SESSION["cart_item"]))
    {
        if(in_array($itemArray[], $_SESSION["cart_item"]))
        {
            foreach($_SESSION["cart_item"] as $k => $v)
            {
                if($itemArray[] == $k)
                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
            }
        }
        else
        {
            $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
        }
    }
    else
    {
        $_SESSION["cart_item"] = $itemArray;
    }
}

I get the values properly in $resp since I checked it using num_rows > 0 but there is an error, that cannot use [] for reading and that illegal offsets are used.

I am new to this thing and I have also tried modifying the code all over. Where am I going wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Akshay Shrivastav
  • 1,115
  • 4
  • 17
  • 43

2 Answers2

1

I’m not sure what you meant, but you have an error in this line:

if(in_array($itemArray[], $_SESSION["cart_item"])) …

The first parameter, $itemArray[] is causing the problem.

The notation $itemArray[] is a special PHP short hand for pushing an element on to an array. You use it this way:

$itemArray[]='new value';

However, you can only use it on the left hand side of an assignment expression. As you see, it generates an error if you try to read from it, which has no meaning.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • i am searching whether the product user is adding is already present in array or not if then then the product quantitiy will be increased by +1 – Akshay Shrivastav Apr 04 '17 at 12:56
  • @AkshayShrivastav It doesn’t make any difference. You can only use `$itemArray[]` in an assignment. It’s the equivalent of `array_push($itemArray,'new value')`. You cannot use it in a context where you’re supposed to be reading something, which I see you have done more than once. – Manngo Apr 04 '17 at 13:01
  • can you please help in editing the code, i am trying to achieve this `what i am trying to achieve is when user adds a product to cart the ajax call will send they request to server with product id, then i will extract all the data related to that product id then the data that i receive i response i want to store it in an array like JSON array in PHP, then i want to check whether the S_SESSION['cart'] is emprty or not if its empty then push the values in array else make multiple arrays in an array with values of various products` – Akshay Shrivastav Apr 04 '17 at 13:03
  • As I said, I’m not sure what you meant, but you could try removing the `[]` altogether. I don’t see that you have an existing array called `$itemArray`, so I don’t see that you’re really pushing onto it. I see 3 instances where you have used it. It might fix it if you remove all three. – Manngo Apr 04 '17 at 13:08
1

I am not understanding well what you want but I put this on answer becouse is too large to put on the comments :

 $resp = $cart->getDetails(filter_var($_POST["pid"], FILTER_SANITIZE_NUMBER_INT));

    if($resp == "" OR $resp == null)
    {
        echo "Some Error!";
    }

    elseif($resp != "" || $resp != null)
    {
        while($row = $resp->fetch_assoc())
        {
            $itemArray = array(
             array(
                'name' => $row["product_name"],
                'id' => $row["id"],
                'discount' => $row["discount"],
                'quantity' => 1,
                'price' => $row["price"]
            )
            );
        }

    print_r($itemArray);

    if(!empty($_SESSION["cart_item"]))
    {
        if(in_array($itemArray, $_SESSION["cart_item"]))
        {
            foreach($_SESSION["cart_item"] as $k => $v)
            {
                if(in_array($k, $itemArray))
                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
            }
        }
        else
        {
            $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
        }
    }
    else
    {
        $_SESSION["cart_item"] = $itemArray;
    }
    }
Nerea
  • 2,107
  • 2
  • 15
  • 14
  • That perfectly worked for me @Nerea that's what i wanted, thanks a lot :) i just had one more question to ask no i can see the result in Array { 0 ans so inside the elements if i have generated one more array then that will be array { 1 how to append that array with the previous one ? – Akshay Shrivastav Apr 04 '17 at 13:25
  • To merge two arrays you can use `$final_array = array_merge($array1,$array2);` – Nerea Apr 04 '17 at 13:31