Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I have a problem with my session array. I can't seem to add a new item in the array.

$new_product = array($_GET['id']);
	
	if(isset($_SESSION['products'])) //if we have the session
	{
		$found = false; //set found item to false
		            
		foreach ($_SESSION["products"] as $cart_itm) //loop through session array
		{
			if($cart_itm == $_GET['id']){ //the item exist in array
		
				$product = array($cart_itm);
				$found = true;
			}else{
				//item doesn't exist in the list, just retrive old info and prepare array for session var
				$product = array($_GET['id']);
			}
			
			if($found == false) //we didn't find item in array
			{
				//add new user item in array
				$_SESSION["products"] = array_merge($new_product, $product);
			}else{
				//found user item in array list, and increased the quantity
				$_SESSION["products"] = $product;
			}
		}
	}
	else{
		//create a new session var if does not exist
		$_SESSION['products'] = $new_product;
	}


I want my code to check it session is set then else creates new session array. Then in if session is set, it will check if item has duplicate in array. If there is a duplicate in the array it should add it inside the array.
Posted

1 solution

I couldn't understand everything you were attempting from the question you asked, yet after sifting though your code, this is what I got.

You wanted a session containing products. If that product was found (a duplicate) then the quantity should be increased. However, to effectively organize it all, having multiple duplicate entries into the array would make sorting though it all a pain. So, I've revised the code to house a multidimensional array, that keeps track of each item added to the products session and a quantity count for each duplicate item.

PHP
// Make sure session is started
session_start();

// Make sure id actually exists
if(isset($_GET['id']))
{
	// A product is placed in cart, add initial item with count
	$newProduct = array('item' => $_GET['id'], 'count' => 1);

	// Check if session exists, if not create it
	if(isset($_SESSION['products']))
	{
		$found = false; // init found, so item can be updated accordingly
		$currentProducts = $_SESSION['products']; // Grab any products already saved in session array

		// Loop through all entries
		for($i = 0; $i < count($currentProducts); $i++)
		{
			// Loop through the array within each entry
			foreach($currentProducts[$i] as $key => $value)
			{
				// Grab item key
				if($key == "item")
				{
					// if item is found, then set found to true
					if($value == $_GET['id'])
					{
						$found = true;
						$currentItem = $currentProducts[$i];
					}
				}
			}

			// after looping through item's entry array, update that item's count
			// Be sure to grab the item that needs updated
			if($currentProducts[$i]["item"] == $_GET['id'])
				$currentProducts[$i]["count"] = $currentProducts[$i]["count"] + 1;
		}

		if($found) // Item was found
		{
			// Reset session, but set it to currentProducts, so the session
			// is updated accordingly
			$_SESSION['products'] = $currentProducts;
		}
		else // Item is not found
		{
			// Append new product to session
			$_SESSION['products'][] = $newProduct;
		}
	}
	else
	{
		$_SESSION['products'] = array($newProduct);
	}

	echo "<pre>", print_r($_SESSION['products']), "</pre>";
}

// session_destroy(); // Uncomment to destroy session and restart | For debugging


It's not the cleanest way to do it, but it makes working with the session array a bit easier. Especially when you decide to display the items in a shopping cart fashion.


I hope this helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900