Page 1 of 1
Web Shop
Posted: Mon Nov 01, 2004 4:16 pm
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?
Posted: Mon Nov 01, 2004 8:43 pm
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";
}
?>
Posted: Fri Nov 05, 2004 3:44 am
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
Posted: Fri Nov 05, 2004 4:26 am
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;