Array
am 24.10.2009 12:57:16 von Ron Piggott
The following line gives me an error message when there aren't any
values in the array --- how do I accommodate this?
Warning: Invalid argument supplied for foreach()
foreach ($_SESSION['order'] AS $key => $value ) {
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array
am 24.10.2009 12:59:13 von Ashley Sheridan
--=-TZnjyxTAXP1/lpIwUd/v
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Sat, 2009-10-24 at 06:57 -0400, Ron Piggott wrote:
> The following line gives me an error message when there aren't any
> values in the array --- how do I accommodate this?
>
> Warning: Invalid argument supplied for foreach()
>
> foreach ($_SESSION['order'] AS $key => $value ) {
>
>
Do an isset() on $_SESSION['order'] first to determine if the variable
even exists, then do is_array() to determine if it's an array or not
before trying to iterate it. My guess is that $_SESSION['order'] isn't
an array all the time.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-TZnjyxTAXP1/lpIwUd/v--
Re: Array
am 24.10.2009 16:50:14 von Martin Scotta
--00032555d43e3215530476af720a
Content-Type: text/plain; charset=ISO-8859-1
On Sat, Oct 24, 2009 at 7:59 AM, Ashley Sheridan
wrote:
> On Sat, 2009-10-24 at 06:57 -0400, Ron Piggott wrote:
>
> > The following line gives me an error message when there aren't any
> > values in the array --- how do I accommodate this?
> >
> > Warning: Invalid argument supplied for foreach()
> >
> > foreach ($_SESSION['order'] AS $key => $value ) {
> >
> >
>
>
> Do an isset() on $_SESSION['order'] first to determine if the variable
> even exists, then do is_array() to determine if it's an array or not
> before trying to iterate it. My guess is that $_SESSION['order'] isn't
> an array all the time.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
foreach works with array and instances.
Unless the class implements Transversable, it's public properties are used
on the loop.
foreach($object as $prop => $value )
//php translates the foreach into something like this...
foreach(get_object_vars($object) as $prop => $value )
--
Martin Scotta
--00032555d43e3215530476af720a--
Re: Array
am 24.10.2009 17:42:59 von Ron Piggott
--=-lDy4auvfa7IoQ4UIVx1s
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
The code I have so far for orders is below. When a product hasn't been
added it does what I want it to --- in giving the message "Your shopping
cart is empty". When a product is added, but then the user changes
their mind I use the following lines of code to remove the selection:
UNSET($_SESSION['order'][$reference]['quantity']);
UNSET($_SESSION['order'][$reference]);
It still leaves the variable $_SESSION['order'] as an array, even if
there are no selections in it. The PHP command is_array is useless of
weed out when there are no products.
What I would like to have happen is if the shopping cart is empty then
the message "Your shopping cart is empty" be displayed 100% of the time.
How do I achieve this? What changes to my code below need to happen?
if ( isset($_SESSION['order']) ) {
#customer has begun creating order
foreach ($_SESSION['order'] AS $key => $value ) {
echo "Product: " . $key . " Quantity: " .
$_SESSION['order'][$key]['quantity'] . "
\r\n";
}
} else {
#no products selected
echo "
\r\n";
echo "- Your shopping cart is empty
\r\n";
echo "
\r\n";
}
-----Original Message-----
From: Martin Scotta
To: ash@ashleysheridan.co.uk
Cc: ron.piggott@actsministries.org, PHP General
Subject: Re: [PHP] Array
Date: Sat, 24 Oct 2009 11:50:14 -0300
On Sat, Oct 24, 2009 at 7:59 AM, Ashley Sheridan
wrote:
On Sat, 2009-10-24 at 06:57 -0400, Ron Piggott wrote:
> The following line gives me an error message when there aren't
any
> values in the array --- how do I accommodate this?
>
> Warning: Invalid argument supplied for foreach()
>
> foreach ($_SESSION['order'] AS $key => $value ) {
>
>
Do an isset() on $_SESSION['order'] first to determine if the
variable
even exists, then do is_array() to determine if it's an array or
not
before trying to iterate it. My guess is that $_SESSION['order']
isn't
an array all the time.
Thanks,
Ash
http://www.ashleysheridan.co.uk
foreach works with array and instances.
Unless the class implements Transversable, it's public properties are
used on the loop.
foreach($object as $prop => $value )
//php translates the foreach into something like this...
foreach(get_object_vars($object) as $prop => $value )
--
Martin Scotta
--=-lDy4auvfa7IoQ4UIVx1s--
Re: Array
am 24.10.2009 18:15:30 von Ron Piggott
AHH. The count() command does the trick. Ron
-----Original Message-----
From: Ron Piggott
Reply-to: ron.piggott@actsministries.org
To: Martin Scotta , phpster@gmail.com
Cc: ash@ashleysheridan.co.uk, PHP General
Subject: Re: [PHP] Array
Date: Sat, 24 Oct 2009 11:43:12 -0400
The code I have so far for orders is below. When a product hasn't been
added it does what I want it to --- in giving the message "Your shopping
cart is empty". When a product is added, but then the user changes
their mind I use the following lines of code to remove the selection:
UNSET($_SESSION['order'][$reference]['quantity']);
UNSET($_SESSION['order'][$reference]);
It still leaves the variable $_SESSION['order'] as an array, even if
there are no selections in it. The PHP command is_array is useless of
weed out when there are no products.
What I would like to have happen is if the shopping cart is empty then
the message "Your shopping cart is empty" be displayed 100% of the time.
How do I achieve this? What changes to my code below need to happen?
if ( isset($_SESSION['order']) ) {
#customer has begun creating order
foreach ($_SESSION['order'] AS $key => $value ) {
echo "Product: " . $key . " Quantity: " .
$_SESSION['order'][$key]['quantity'] . "
\r\n";
}
} else {
#no products selected
echo "\r\n";
echo "- Your shopping cart is empty
\r\n";
echo "
\r\n";
}
-----Original Message-----
From: Martin Scotta
To: ash@ashleysheridan.co.uk
Cc: ron.piggott@actsministries.org, PHP General
Subject: Re: [PHP] Array
Date: Sat, 24 Oct 2009 11:50:14 -0300
On Sat, Oct 24, 2009 at 7:59 AM, Ashley Sheridan
wrote:
On Sat, 2009-10-24 at 06:57 -0400, Ron Piggott wrote:
> The following line gives me an error message when there aren't
any
> values in the array --- how do I accommodate this?
>
> Warning: Invalid argument supplied for foreach()
>
> foreach ($_SESSION['order'] AS $key => $value ) {
>
>
Do an isset() on $_SESSION['order'] first to determine if the
variable
even exists, then do is_array() to determine if it's an array or
not
before trying to iterate it. My guess is that $_SESSION['order']
isn't
an array all the time.
Thanks,
Ash
http://www.ashleysheridan.co.uk
foreach works with array and instances.
Unless the class implements Transversable, it's public properties are
used on the loop.
foreach($object as $prop => $value )
//php translates the foreach into something like this...
foreach(get_object_vars($object) as $prop => $value )
--
Martin Scotta
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array
am 24.10.2009 19:01:21 von Shawn McKenzie
Ron Piggott wrote:
> The code I have so far for orders is below. When a product hasn't been
> added it does what I want it to --- in giving the message "Your shopping
> cart is empty". When a product is added, but then the user changes
> their mind I use the following lines of code to remove the selection:
>
> UNSET($_SESSION['order'][$reference]['quantity']);
> UNSET($_SESSION['order'][$reference]);
>
> It still leaves the variable $_SESSION['order'] as an array, even if
> there are no selections in it. The PHP command is_array is useless of
> weed out when there are no products.
>
> What I would like to have happen is if the shopping cart is empty then
> the message "Your shopping cart is empty" be displayed 100% of the time.
> How do I achieve this? What changes to my code below need to happen?
>
>
> if ( isset($_SESSION['order']) ) {
> #customer has begun creating order
>
> foreach ($_SESSION['order'] AS $key => $value ) {
> echo "Product: " . $key . " Quantity: " .
> $_SESSION['order'][$key]['quantity'] . "
\r\n";
> }
>
> } else {
> #no products selected
>
> echo "
\r\n";
> echo "- Your shopping cart is empty
\r\n";
> echo "
\r\n";
>
> }
>
Or use unset, but unset the entire order: unset($_SESSION['order'])
--
Thanks!
-Shawn
http://www.spidean.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Array
am 24.10.2009 19:35:22 von List Manager
Ron Piggott wrote:
> The code I have so far for orders is below. When a product hasn't been
> added it does what I want it to --- in giving the message "Your shopping
> cart is empty". When a product is added, but then the user changes
> their mind I use the following lines of code to remove the selection:
>
> UNSET($_SESSION['order'][$reference]['quantity']);
> UNSET($_SESSION['order'][$reference]);
>
> It still leaves the variable $_SESSION['order'] as an array, even if
> there are no selections in it. The PHP command is_array is useless of
> weed out when there are no products.
>
> What I would like to have happen is if the shopping cart is empty then
> the message "Your shopping cart is empty" be displayed 100% of the time.
> How do I achieve this? What changes to my code below need to happen?
>
>
> if ( isset($_SESSION['order']) ) {
> #customer has begun creating order
>
> foreach ($_SESSION['order'] AS $key => $value ) {
> echo "Product: " . $key . " Quantity: " .
> $_SESSION['order'][$key]['quantity'] . "
\r\n";
> }
>
> } else {
> #no products selected
>
> echo "
\r\n";
> echo "- Your shopping cart is empty
\r\n";
> echo "
\r\n";
>
> }
>
Try this
....
if ( isset($_SESSION['order']) && # Does it exist
is_array($_SESSION['order']) && # Is it an array
count($_SESSION['order']) > 0 # Does it have at least 1 element
) {
#customer has begun creating order
foreach ($_SESSION['order'] AS $key => $value ) {
echo "Product: {$key} Quantity: {$_SESSION['order'][$key]['quantity']}
\r\n";
}
} else {
#no products selected
echo "\r\n";
echo "- Your shopping cart is empty
\r\n";
echo "
\r\n";
}
....
?>
> -----Original Message-----
> From: Martin Scotta
> To: ash@ashleysheridan.co.uk
> Cc: ron.piggott@actsministries.org, PHP General
>
> Subject: Re: [PHP] Array
> Date: Sat, 24 Oct 2009 11:50:14 -0300
>
>
>
> On Sat, Oct 24, 2009 at 7:59 AM, Ashley Sheridan
> wrote:
> On Sat, 2009-10-24 at 06:57 -0400, Ron Piggott wrote:
>
> > The following line gives me an error message when there aren't
> any
> > values in the array --- how do I accommodate this?
> >
> > Warning: Invalid argument supplied for foreach()
> >
> > foreach ($_SESSION['order'] AS $key => $value ) {
> >
> >
>
>
>
>
> Do an isset() on $_SESSION['order'] first to determine if the
> variable
> even exists, then do is_array() to determine if it's an array or
> not
> before trying to iterate it. My guess is that $_SESSION['order']
> isn't
> an array all the time.
>
>
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
>
> foreach works with array and instances.
> Unless the class implements Transversable, it's public properties are
> used on the loop.
>
>
> foreach($object as $prop => $value )
> //php translates the foreach into something like this...
> foreach(get_object_vars($object) as $prop => $value )
>
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php