1

all_product.php -> list out all the products

cart.php->show items being added to cart.

Every time I click add to cart, it will be added to array and i list out all of them. The result will be something like the following (before using in_array() )

Products that are added to cart, ID -> 4
Products that are added to cart, ID -> 7
Products that are added to cart, ID -> 6
Products that are added to cart, ID -> 5
Products that are added to cart, ID -> 5
Products that are added to cart, ID -> 6
Products that are added to cart, ID -> 4

I use in_array() function to add items to cart only if it exists. So the result will become like the following instead :

Products that are added to cart, ID -> 4
Products that are added to cart, ID -> 7
Products that are added to cart, ID -> 6
Products that are added to cart, ID -> 5

So my question is how to count the total number of times being added for each product; Basically just like each product's quantity.

all_products.php

require 'config.php';
$q = mysqli_query( $db->conn(), "SELECT * FROM product" );

if( mysqli_num_rows($q) > 0 ) { // Check if there are results
while( $row = mysqli_fetch_assoc($q)){
echo '<p> '.$row['product_name'].' | <a href="cart.php?id='.$row['id'].'">Add To Cart!</a></p>';
}           
}

cart.php

session_start();

if(isset($_GET['id'])){

if(!isset($_SESSION['cart'])){
    $_SESSION['cart']=[]; //Create session 1st time

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

    if(!in_array($_GET['id'],$_SESSION['cart'])){ //add item only if doesn't exist
        $_SESSION['cart'][]=$_GET['id'];
    }
    foreach($_SESSION['cart'] as $x => $item_added) { // all products being added to cart
    echo "Products that are added to cart, ID -> " . $item_added;
    echo "<br>";
    }
}
}else{ // if user view this page directly without "?id=" , show error
echo "I hate php!";
}
5
  • count($_SESSION['cart']) no? Commented Aug 15, 2016 at 13:10
  • @ineersa : nope he wants quantity for each product Commented Aug 15, 2016 at 13:12
  • @ineersa no, this function is to count how may items in an array. I am doing a add to cart function. I want to check each individual product's total number of times being clicked -> Add to cart. Commented Aug 15, 2016 at 13:12
  • 1
    I'd change my array structure. Something like $_SESSION['cart'][$_GET['id']] = array('count' => 2, 'somethingelse' => 'test'); Commented Aug 15, 2016 at 13:13
  • 1
    Try using twodimensional arrays or sth like that and increase a counter if the element already exist Commented Aug 15, 2016 at 13:13

2 Answers 2

1

Instead of saving product id as a value save it as a key of an array and increment the counter each time someone adds the product in cart.

So your code will look something like this,

if(!array_key_exists($_GET['id'],$_SESSION['cart'])){
        $_SESSION['cart'][$_GET['id']]=1;
    } else {
        $_SESSION['cart'][$_GET['id']]++;
}

Using this method both of your purpose would be solved, only unique products will be added in cart and with their quantity.

http://php.net/manual/en/function.array-key-exists.php

array_key_exists() checks if the given key or index exists in the array.


To retrieve the products with their quantity run a foreach loop like this,

foreach($_SESSION['cart'] as $productid=>$quantity) {
   echo $productid." added ".$quantity." times <br>";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use a key value pair:

$cart=["name" => 1, "name2" => 2]

To add a new value do:

$cart[$name]=1;

To increase:

$cart[$name]++;

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.