Sifer
Sifer

Reputation: 57

PHP For Loop doesn't iterate

I am currently learning PHP at college. I am trying to make a function to check whether or not an item is already in the cart session. If it is then increment the quantity by 1, if not, then add it the item to the cart array.

It seems to work fine adding the new item and the function works when adding multiples of the same item however, the problem lies when I add another item to the array. It adds the new item but then if I try and place another of the same item it adds another item to the array instead of increasing the quantity by 1.

It seems like the for loop doesn't work how I want it to. Any advice would be appreciated thanks!

    function addItemToCart($mysqli) {

  $itemID = (int)$_GET['add'];
  $counterItemsInCart = 0;
  $alreadyInCart = false;

  if(is_int($itemID)) { //makes sure the ID is passed as an int

    $query = "SELECT product_id, product_name, product_price FROM product WHERE product_id = ?";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("i", $itemID); // bind variables
    $stmt->execute();
    $result = $stmt->get_result();

    if($rows = $result->fetch_assoc())
    {
      //setts all info needed to variables.
      $pID = $rows['product_id'];
      $pName = $rows['product_name'];
      $pPrice = $rows['product_price'];
    }
    else
    {
      //if the item id is not an int display this message...
      echo "not working";
    }
    if(isset($_SESSION['cart'])) { //check if any item in cart already
      $intArraySize = count($_SESSION['cart']);
      //loop through cart array
      for($i = 0; $i < $intArraySize; $i+=1)
      {
        //check if item already exists in cart.
        if($_SESSION['cart'][$i]["item_id"] === $pID)
        {
          $_SESSION['cart'][$i]["item_quantity"] += 1;
          $alreadyInCart = true;
          echo "<script type='text/javascript'>alert('item in cart');</script>";
        }
      }//loop to cycle through items in array...
      if($alreadyInCart === false) { echo "<script type='text/javascript'>alert('item not in cart');</script>";
        array_push($_SESSION['cart'], array($intArraySize =>array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1)));
      }//close if statement check if same item is in array, if not add it.
    }//close if statement checking if any item at all in shopping cart.
    else {echo "<script type='text/javascript'>alert('new item');</script>";
      $_SESSION['cart'] = array(array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1));
    }
  }//ends if statement checking if the id of the item is a int or string.
}//ends function

var_dump output

array(3) { [0]=> array(4) { ["item_id"]=> int(1) ["item_name"]=> string(10) "Pink Shirt" ["item_price"]=> string(4) "9.99" ["item_quantity"]=> int(3) } [1]=> array(1) { [1]=> array(4) { ["item_id"]=> int(2) ["item_name"]=> string(14) "Green w/Button" ["item_price"]=> string(5) "14.99" ["item_quantity"]=> int(1) } } [2]=> array(1) { [2]=> array(4) { ["item_id"]=> int(2) ["item_name"]=> string(14) "Green w/Button" ["item_price"]=> string(5) "14.99" ["item_quantity"]=> int(1) } } }

This was adding three pink shirts one after another and then two green. For some reason the green ones are seen as different items.

Sorry if the format is bad, not sure how I should format a var_dump will check this now...

Upvotes: 1

Views: 249

Answers (3)

Sifer
Sifer

Reputation: 57

Ok I finally realised what I had done wrong. When I pushed another item in to the array see below:

array_push($_SESSION['cart'], array($intArraySize =>array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1)));

It should have been:

array_push($_SESSION['cart'], array("item_id" => $pID, "item_name" => $pName, "item_price" => $pPrice, "item_quantity" => 1));

So it was creating another dimension to the array which was messing up my for loop. If that makes any sense, as I said I am still learning php and am not sure of the terminology to explain it any better.

Many Thanks to all for the advice. Using some of the debugging techniques Marius advised helped me see this error.

Upvotes: 0

Marius
Marius

Reputation: 399

=== should not be used to compare the values. try == instead :

if($_SESSION['cart'][$i]["item_id"] == $pID)
    {
      $_SESSION['cart'][$i]["item_quantity"] += 1;
      $alreadyInCart = true;
      echo "<script type='text/javascript'>alert('item in cart');</script>";
    }

http://www.w3schools.com/php/php_operators.asp

EDIT:

its hard to say what else could be wrong without knowing how the $_SESSION['cart'] structured

Upvotes: -1

GBnewbie
GBnewbie

Reputation: 88

Shouldn't it be :

 $_SESSION['cart'][$i][$pID]["item_quantity"] += 1;

Upvotes: 1

Related Questions