|
Posted by Robert Cummings on 10/03/05 17:24
On Sun, 2005-10-02 at 01:01, Robert Cummings wrote:
>
> If you go with an array system might I suggest the following change to
> what I wrote:
I'm resending a better way to do the simple trolley since you seemed to
have created a Frankensteinian merge of the two examples I sent
previously. This version uses functions which should probably be put
into their own source file and included (let's call it lib.trolley.php).
<?php
session_name( 'CCLTrolley' );
session_start();
//
// Initialize the trolley.
//
if( !isset( $_SESSION['TrolleyContents'] ) )
{
$_SESSION['TrolleyContents'] = array();
}
function trolleyItemQuantity( $itemId )
{
$trolley = &$_SESSION['TrolleyContents'];
//
// Return the $quantity of $item in the $trolley.
//
if( isset( $trolley[$item] ) )
{
return $trolley[$item];
}
return 0;
}
function trolleyItemExists( $itemId, $quantity )
{
$trolley = &$_SESSION['TrolleyContents'];
//
// Return true if $item is in the $trolley; otherwise return false.
//
if( isset( $trolley[$item] ) )
{
return $trolley[$item];
}
return 0;
}
function trolleyItemAdd( $itemId, $quantity )
{
$trolley = &$_SESSION['TrolleyContents'];
//
// Add $quantity of $item to the $trolley.
//
if( isset( $trolley[$item] ) )
{
$trolley[$item] += $quantity;
}
else
{
$trolley[$item] = $quantity;
}
}
function trolleyItemRemove( $itemId, $quantity )
{
$trolley = &$_SESSION['TrolleyContents'];
//
// Remove $quantity of $item from the $trolley.
//
if( isset( $trolley[$item] ) )
{
$trolley[$item] -= $quantity;
if( $trolley[$item] < 1 )
{
unset( $trolley[$item] );
}
}
}
function trolleyPrint()
{
//
// Display the trolley.
//
echo "<pre>\n"
.implode( ',', array_keys( $_SESSION['TrolleyContents'] ) )
."\n</pre>\n";
}
?>
Now the following would facilitate the action you appear to be
performing in your example for adding to the trolley:
<?php
include_once( 'lib.trolley.php' );
if( isset( $_POST['ShopAction'] ) && isset( $_POST['RNum'] ) )
{
echo $_POST['ShopAction'].'!!<br />';
if( 'add' == $_POST['ShopAction'] )
{
echo 'Adding '.$_POST['RNum'].'...<br />';
trolleyItemAdd( $_POST['RNum'], 1 );
}
else
if( 'delete' == $_POST['ShopAction'] )
{
echo 'Deleting '.$_POST['RNum'].'...<br />';
trolleyItemRemove( $_POST['RNum'], 1 );
}
}
else
{
echo 'No action specified!!<br />';
}
trolleyPrint();
?>
--
..------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting |
| a powerful, scalable system for accessing system services |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for |
| creating re-usable components quickly and easily. |
`------------------------------------------------------------'
Navigation:
[Reply to this message]
|