Web Shop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sturmovik
Forum Newbie
Posts: 7
Joined: Thu Oct 21, 2004 2:18 pm

Web Shop

Post by sturmovik »

How to use array for a web shop. Currently I'm trying to use 1d array and name indexes with shop item IDs, and then values use as number of items in the basket. But the problem is how to add that kind of data dynamically...

I know how to do that this way:

Code: Select all

$shoppingChart = array(
			36 => 1,
			334 =>2,
			45 => 1,
			);
But how to add new or modify existing quantity?


O should I just use 2d array?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

<?php
$shoppingChart = array(
  [0] => array (
      'sku' => '36',
      'quantity' => 1,
      ),
  [1] => array (
      'sku' => '334',
      'quantity' => 2,
      ),
  [2] => array (
      'sku' => '45',
      'quantity' => 1,
      ),
   );

foreach ($shoppingChart as $n => $item) {
    echo "$n. sku={$item['sku']}, quantity={$item['quantity']}<br/>\n";
}
?>
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Here is a little snipet of code that will allow you to add and remove items from a 1d array.

I haven't added any error checking code. That, as they say in the best circles, if left as an excercise for the reader.

Code: Select all

<?php 

function AddItem(& $ShoppingCart, $ItemId, $Quantity)
{
  // should check that
  //  a) $ShoppingCart is an array
  //  b) $Quantity is a number
  if (true == isset($ShoppingCart[$ItemId]))
  {
    $ShoppingCart[$ItemId] += $Quantity;
  }
  else
  {
    $ShoppingCart[$ItemId] = $Quantity;
  }
}

function RemoveItem(& $ShoppingCart, $ItemId, $Quantity)
{
  // should check that
  //  a) $ShoppingCart is an array
  //  b) $Quantity is a number
  if (true == isset($ShoppingCart[$ItemId]))
  {
    // Should check that there are at least $Quantity items
    // in array before removing them, otherwise issue some
    // kind of error
    $ShoppingCart[$ItemId] -= $Quantity;
  }
}

$shopping_cart = array();

echo '<br />Start<br />';
print_r ($shopping_cart);
AddItem ($shopping_cart, 'Item1', 25);
AddItem ($shopping_cart, 'Item5', 5);
AddItem ($shopping_cart, 'Item1', 10);
echo '<br />After Add<br />';
print_r ($shopping_cart);
RemoveItem ($shopping_cart, 'Item1', 4);
echo '<br />After Remove<br />';
print_r ($shopping_cart);

?>
Hope this helps
sturmovik
Forum Newbie
Posts: 7
Joined: Thu Oct 21, 2004 2:18 pm

Post by sturmovik »

Well, a few hours ago I've developed this code:
(comments wanted)

Code: Select all

//if want to empty the schart...
	if ( $_GET['action'] == "deleteAll" )
	{
		unset($_SESSION['shoppingChart']);
	}
	//if want to add or remove from the chart
	elseif($_GET['action']=="addToChart" && isset($_GET['itemID']))
	{
		$itemID = $_GET['itemID'];
		//array_push($shoppingChart,array('id' =>$itemID,'count'=>1));


		if(isset($_SESSION['shoppingChart']))
		{
			$shoppingChart = $_SESSION['shoppingChart'];
		}
		else
		{
			$shoppingChart = array();
		}

		static $de;
		foreach ($shoppingChart as $index => $val)
		{
			if($val['id']==$itemID)
			{
				$shoppingChart[$index]=array('id' => $itemID,'count'=>$val['count']+1);
				$de = 1;
				break;
			}
		}
		if($de!=1) array_push($shoppingChart,array('id' =>$itemID,'count'=>1));
	}
	elseif($_GET['action']=="removeItem" && isset($_GET['itemID']))
	{
		$itemID = $_GET['itemID'];
		//array_push($shoppingChart,array('id' =>$itemID,'count'=>1));


		if(isset($_SESSION['shoppingChart']))
		{
			$shoppingChart = $_SESSION['shoppingChart'];
		}
		else
		{
			$shoppingChart = array();
		}
		
		foreach ($shoppingChart as $index => $val)
		{
			if($val['id']==$itemID)
			{
				$shoppingChart[$index]=array('id' => $itemID,'count'=>0);
				break;
			}
		}				
	}
	$_SESSION['shoppingChart'] = $shoppingChart;
Post Reply