Re: Accessing Session Variable
- From: "Joseph S." <js_dev@xxxxxxxxxxxxxx>
- Date: 5 Nov 2005 03:27:27 -0800
joyn...@xxxxxxxxxxxxx wrote:
> Thanks for the info. I tried that and I did not work for me.
>
> $_SESSION[products] = serialize($products);
>
> The above line is there because of this
> $products = array_unique(
> array_merge(unserialize($_SESSION[products]),
> $_POST[form_products]));
Correct. I did not notice the functionality to add new products to the
existing list.
The problem I get is the warning:
Warning: Invalid argument supplied for foreach() in
C:\xampp\htdocs\php\listing16.5.php on line 13
when I run the script for the first time.
The code to handle subsequent choices looks correct.
$products is not getting initialized after the first post - it is
directly getting assigned to $_SESSION[products] - in an empty state.
That seems to be the problem.
To fix that,
instead of :
if (isset($_POST[form_products])) {
if (!empty($_SESSION[products])) {
$products = array_unique(
array_merge(unserialize($_SESSION[products]),
$_POST[form_products]));
}
$_SESSION[products] = serialize($products);
print "<p>Your products have been registered!</p>";
}
do this:
if (isset($_POST[form_products])) {
if (!empty($_SESSION[products])) { /* if $_SESSION[products] is
not empty, merge */
$products = array_unique(
array_merge(unserialize($_SESSION[products]),
$_POST[form_products]));
}else{ /* if $_SESSION[products] is empty, fill $products with the
POSTed items */
$products = $_POST[form_products];
}
$_SESSION[products] = serialize($products);
print "<p>Your products have been registered!</p>";
}
This is working here. If it still gives a problem, your PHP install
should be looked at.
If you are not using a debugger, get one. PHPEclipse and XAMPP make an
excellent combination. That's free software. PHPEdit (NuSphere) and
Zend Studio are the commercial variety - They are great, but not free.
If you are not using any debugger, use the PHP functions, print_r() and
echo at correct places to see if the values of variables are being
passed around correctly.
Joseph S.
.
- Follow-Ups:
- Re: Accessing Session Variable
- From: joyn297
- Re: Accessing Session Variable
- References:
- Accessing Session Variable
- From: joyn297
- Re: Accessing Session Variable
- From: Joseph S.
- Re: Accessing Session Variable
- From: joyn297
- Accessing Session Variable
- Prev by Date: Re: Posting coding on a website
- Next by Date: Use PhP to create a new page
- Previous by thread: Re: Accessing Session Variable
- Next by thread: Re: Accessing Session Variable
- Index(es):
Relevant Pages
|