You are here: Re: [PHP] session_name("CCLTrolley") « PHP « IT news, forums, messages
Re: [PHP] session_name("CCLTrolley")

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация