|
Posted by Robert Cummings on 10/02/05 08:01
On Sun, 2005-10-02 at 00:37, John Taylor-Johnston wrote:
> Robert Cummings wrote:
>
> > <?php
> > session_name( 'CCLTrolley' );
> > session_start();
> > // Initialize the trolley.
> > if( !isset( $_SESSION['TrolleyContents'] ) )
> > {
> > $_SESSION['TrolleyContents'] = array();
> > }
> > // Add new entry.
> > if( isset( $_POST['AddToTrolley'] ) )
> > {
> > $_SESSION['TrolleyContents'][$_POST['AddToTrolley']] =
> > $_POST['AddToTrolley']
> > }
> > echo implode( ',', $_SESSION['TrolleyContents'] );
> > ?>
>
> I've never been very good getting my head around arrays.
> Then how do I check to know if $mydata->RNum is in
> $_SESSION['TrolleyContents'] ?
> Thanks for your patience.
If you go with an array system might I suggest the following change to
what I wrote:
<?php
session_name( 'CCLTrolley' );
session_start();
//
// Initialize the trolley.
//
if( !isset( $_SESSION['TrolleyContents'] ) )
{
$_SESSION['TrolleyContents'] = array();
}
//
// Add new entry.
//
if( isset( $_POST['AddToTrolley'] ) )
{
if( isset( $_SESSION['TrolleyContents'][$_POST['AddToTrolley']] ) )
{
$_SESSION['TrolleyContents'][$_POST['AddToTrolley']] += 1;
}
else
{
$_SESSION['TrolleyContents'][$_POST['AddToTrolley']] = 1;
}
}
echo implode( ',', array_keys( $_SESSION['TrolleyContents'] ) );
phpinfo();
?>
To check if something is in the trolley:
<?php
if( isset( $_SESSION['TrolleyContents'][$myData->RNum] ) )
{
echo 'Yaaaaaaaaaaaaaaaaay!';
}
?>
To decrement the quantity of an item in the trolley:
<?php
if( isset( $_SESSION['TrolleyContents'][$myData->RNum] ) )
{
$_SESSION['TrolleyContents'][$myData->RNum] -= 1;
if( $_SESSION['TrolleyContents'][$myData->RNum] <= 0 )
{
unset( $_SESSION['TrolleyContents'][$myData->RNum] );
}
}
?>
I'll leave it as an exercise for you to add or delete X quantity.
Cheers,
Rob.
--
..------------------------------------------------------------.
| 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]
|