Re: Accessing Session Variable



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.

.



Relevant Pages

  • Re: Extremely slow debugging performance in VS2003 with interop
    ... in the debugger now executes CreateInstance in 12 secs instead of 45. ... However, the second time I run the app, I'm back ... to 45 seconds every first time I run the app in the debugger. ... Here is the 12 second output: ...
    (microsoft.public.vsnet.debugging)
  • Re: Extremely slow debugging performance in VS2003 with interop
    ... > rename the file to symsrv.dll.hide, start VS and the initial run of the app ... > in the debugger now executes CreateInstance in 12 secs instead of 45. ... > to 45 seconds every first time I run the app in the debugger. ... > Here is the 12 second output: ...
    (microsoft.public.vsnet.debugging)
  • Re: Delphi 4 Pro vs Standard
    ... Another weird thing is that the first time through, the debugger showed the contents of the received file, but on the second pass, where it failed, the debugger seemed not to work. ... Of course, I could just install my D4 Pro on their computer and I'm sure the Borland cops won't suddenly appear with search warrants and Uzis, but it would be nice to have the ability to maintain this application, using their computer and software. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Delphi 4 Pro vs Standard
    ... However, when I ran the program, it threw an exception, but it was only a misread of data from the serial port and otherwise the program seemed to run normally. ... Another weird thing is that the first time through, the debugger showed the contents of the received file, but on the second pass, where it failed, the debugger seemed not to work. ... Of course, I could just install my D4 Pro on their computer and I'm sure the Borland cops won't suddenly appear with search warrants and Uzis, but it would be nice to have the ability to maintain this application, using their computer and software. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Free software / freeware / billing invoicing
    ... It should be noted that "free software" has a very definite meaning, ... source software, and the crapware/malware that is out there. ... the first time its turned on. ...
    (alt.vacation.las-vegas)