0

Following is my PHP code:

while($row = $resp->fetch_assoc())
{
    $itemArray = array(
      array(
        'name' => $row["product_name"],
        'id' => $row["id"],
        'image' => $row['images'],
        'discount' => $row["discount"],
        'quantity' => $min_quantity,
        'price' => $row["price"]
      )
    );
}

if(!empty($_SESSION["cart_item"]))
{
    if(in_array($itemArray, $_SESSION["cart_item"]))
    {
        foreach($_SESSION["cart_item"] as $item)
        {
            if(in_array($item, $itemArray))
                $_SESSION["cart_item"][$item]["quantity"] = $min_quantity;
            break;
        }
    }
    else
    {
        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
    }
}
else
{
    $_SESSION["cart_item"] = $itemArray;
}

After executing this code i am getting a response like this:

Array
(
[0] => Array
    (
        [name] => Girls Designer Dress
        [id] => 4
        [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am
.jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg";
        [discount] => 10
        [quantity] => 1
        [price] => 1200
    )

)

What I am trying to acheive is if user adds the same product in cart whose data we have got in response then instead of creating once more array and merging it one after another i want to just update the quantity of the same product from 1 to 2.

I am stuck in this part of code

foreach($_SESSION["cart_item"] as $item)
{
    if(in_array($item, $itemArray))
    $_SESSION["cart_item"][$item]["quantity"] = $min_quantity;
    break;
}

I have tried it many times that if same product is encountered then increment the quantity by 1 for the same product and don't create one more array.

Can anyone help with this logic and code?

2
  • What is the value of $_SESSION["cart-item"] ? If it is an array with cart information then looping over it is not the way to go. Commented Apr 4, 2017 at 14:37
  • is there is no value then this will be added in it Array ( [0] => Array ( [name] => Girls Designer Dress [id] => 4 [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am .jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg"; [discount] => 10 [quantity] => 1 [price] => 1200 ) ) if there is present then the array will be updated and the array will extend from 0 to 1 like this from [0] => Array to [1] => Array i wan to update the value which is present in [0] Commented Apr 4, 2017 at 14:52

2 Answers 2

1

is $_SESSION["cart-item"] a single Array like?

Array
(
    [name] => Girls Designer Dress
    [id] => 4
    [image] => s:146:"2017/march/meetfashion01-04-2017_12-19-07_am.jpg,2017/march/meetfashion01-04-2017_12-19-08_am
.jpg,2017/march/meetfashion01-04-2017_12-19-09_am.jpg";
    [discount] => 10
    [quantity] => 1
    [price] => 1200
)

If so then don't call a foreach instead just check directly if that cart-item is in the $itemArray using array_search().

(The function search in a array and if needle is found return its index else return false).

$cartItemPosition = array_search($_SESSION['cart-item'], $itemArray);

if($cartItemPosition == false) {
  // Not found in array, so create new entry
  $itemArray.push($_SESSION['cart-item']);

}else {
  // Found in array so edit existing entry

    $itemArray[$cartItemPosition]['quantity'] = 999;  
}
Sign up to request clarification or add additional context in comments.

3 Comments

and what if a new product is added then? will it create a new array and append it to previous once ?
@AkshayShrivastav . You would do that in the '// Not found' loop. Also why re-create a new array? just append it to the already existing one.
That one worked perfectly for me :) i was creating an additonal array
0

The below works if $itemArray has only 1 item :

Change to :

if(!empty($_SESSION["cart_item"]))
    {
        if(in_array($itemArray[0], $_SESSION["cart_item"]))
            {
                foreach($_SESSION["cart_item"] as $index => $item)
                    {
                        if(in_array($item, $itemArray))
                            $_SESSION["cart_item"][$index]["quantity"] = $_SESSION["cart_item"][$index]["quantity"] + 1;
                        break;
                    }
            }
        else
            {
                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            }
    }
else
    {
        $_SESSION["cart_item"] = $itemArray;
    }
  • Added [0] in first if statement of in_array.
  • Added $index in the foreach loop and used it.

Your code before never went to the if statement. If $itemArray is always containing an array inside an array (that's why we used [0]) then it should be like that.

If it is possible that $itemArray has more than 1 items then we will have to edit the answer as needed.

Also not sure, but if your fetching loop is possible to return more than one items then it's wrong becuse you will only have the last result. If you know that you will only have 1 result back then it's all good.

while($row = $resp->fetch_assoc())
    {
        $itemArray = array(
                             array(
                                    'name' => $row["product_name"],
                                    'id' => $row["id"],
                                    'image' => $row['images'],
                                    'discount' => $row["discount"],
                                    'quantity' => $min_quantity,
                                    'price' => $row["price"]
                                  )
                          );
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.