gosulove
gosulove

Reputation: 1645

PHP how to count total number of times being added to cart ( in_array() )

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!";
}

Upvotes: 1

Views: 871

Answers (2)

Jonas Wilms
Jonas Wilms

Reputation: 138537

Use a key value pair:

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

To add a new value do:

$cart[$name]=1;

To increase:

$cart[$name]++;

Upvotes: 1

Alok Patel
Alok Patel

Reputation: 8032

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>";
}

Upvotes: 1

Related Questions