How do I add a tag to a PHP session?

Rather than continue down this line, what exactly needs to be stored? As in, if an order was going to be made, what details would you need at this step?

Hello!
Well, it must store the color id and the number of the color.
Each product can have an unlimited number of colors. Hearing is quite specific as we are talking about yarn and it is bought in a ball and for this reason certain colors (orbs) are ordered from the yarn.

		function filter_data() {

			var action = 'fetch_data';
			var pColors = get_filter('pColor');

		    $('.addToCart').click(function(){
		        $.ajax({
	                url:""+realLink+"cart.php",
	                method:"POST",
	                data:{action:action,pColors:pColors},
		                success:function(data){
		                   //alert(data);
		                }
            	});
		    });

		}

I have not added the number of colors, because the addition of colors, camels and the number to send it is not yet working: D

You seem to be adding a product to the cart and then selecting the color(s) and quantity(ies) as a separate step, without providing a way of relating the data that’s submitted in the second step with the product it belongs with. Submitting the product id as part of the second step would solve your immediate problem.

Why not use a single step that lets you pick a product and the color(s) and quantity(ies) all at once. See the following example code -

<?php
session_start();

// post method form processing
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	switch($_POST['action'])
	{
		case 'C':
			// Create - add to cart
			
			// examine the submitted data
			// echo '<pre>'; print_r($_POST); echo '</pre>';
			
			// you would validate the input data here...

			// add the products to the cart
			foreach($_POST['qty'] as $color_id=>$quantity)
			{
				if($quantity > 0)
				{
					$_SESSION['cart'][$_POST['product_id']][$color_id] = $quantity;
				} else {
					// zero (or negative) quantity, remove product from the cart
					unset($_SESSION['cart'][$_POST['product_id']][$color_id]);
				}
			}
		break;
		
		// code for other actions here...
	}
}
?>

<?php
// display the cart
if(empty($_SESSION['cart']))
{
	echo 'The cart is empty.<br><br>';
} else {
	// using the product id's from the cart (see array_keys()) you would retrieve all the product names, descriptions, and prices needed to display the cart
	echo 'Your cart contains:<br>';

	// for demo purposes, display the raw cart data
	ksort($_SESSION['cart']); // sort by product id
	foreach($_SESSION['cart'] as $product_id=>$arr)
	{
		ksort($arr); // sort by color id
		echo "Product $product_id<br>";
		foreach($arr as $color_id=>$quantity)
		{
			echo "Color $color_id, Quantity $quantity<br>";
		}
		echo '<br>';
	}
	echo '<br>';
}
?>

<?php
// product display/selection - product id, color id, and quantity

// fake 10 products for demo purposes
foreach(range(1,10) as $product_id)
{
	// display product information here - name, description, price, image ...

	// for demo purposes, display the product id only
	echo "Product $product_id<br>";
	// add to cart form
	echo "<form method='post'>";
	echo "<input type='hidden' name='action' value='C'>"; // Create - add to cart
	echo "<input type='hidden' name='product_id' value='$product_id'>";
	// you would output only the color ids and color names for the current product. this example code uses the same color ids 1-4 for all products
	echo 'Select a non-zero quantity for each color you want to order.<br>';
	// fake 4 colors for demo purposes
	foreach(range(1,4) as $color_id)
	{
		// get the existing quantity, if any, from the cart
		$quantity = $_SESSION['cart'][$product_id][$color_id] ?? 0;
		// for demo purposes, display the color id only. in your real code display the color name and/or a color image
		echo "Color $color_id Quantity <input type='number' name='qty[$color_id]' value='$quantity'><br>";
	}
	echo "<input type='submit' value='Add To Cart'></form><br><br>";
}
1 Like

this code from phdr is perfect! stick with this design.

Sponsor our Newsletter | Privacy Policy | Terms of Service