PHP: Invalid argument supplied for foreach ()
-
Hi all, I am writing a webshop. In the header of the "products" page, I have the following:
session\_start (); include "php/init.php";
In the init.php, I fill a numer of session variables with product information like:
if (!isset ($_SESSION['products']))
{
$_SESSION['products'][ 0]['id'] = '1';
$_SESSION['products'][ 0]['name'] = 'Castle Lager';
$_SESSION['products'][ 0]['number'] = 0;
$_SESSION['products'][ 0]['price'] = 1.25;
$_SESSION['products'][ 0]['disp'] = '1,25';
$_SESSION['products'][ 0]['VAT'] = 2;
$_SESSION['products'][ 1]['id'] = '2';
$_SESSION['products'][ 1]['name'] = 'Hunters Dry';
$_SESSION['products'][ 1]['number'] = 0;
$_SESSION['products'][ 1]['price'] = 1.70;
$_SESSION['products'][ 1]['disp'] = '1,70';
$_SESSION['products'][ 1]['VAT'] = 2;
etc...
and in the Products.php page, I use these variabes to fill a product list:foreach ($\_SESSION\['products'\] as $item) { etc.... }
The reason that I am filling all the individual items is that I have a PC program that maintains all this information, and automatically generates the init.php file on the server. My issue is that when I first start the page, everything works just fine. However, when I do a refresh of the page, I get the error message "Invalid argument supplied for foreach". If I test if the $_SESSION['products'] has been set, then it says that is has been set, however, if I test for "is_array ($_SESSION['products'])" then it returns false. Any ideas on how this can have happened would be greatly appreciated!!! Regards, William
-
Hi all, I am writing a webshop. In the header of the "products" page, I have the following:
session\_start (); include "php/init.php";
In the init.php, I fill a numer of session variables with product information like:
if (!isset ($_SESSION['products']))
{
$_SESSION['products'][ 0]['id'] = '1';
$_SESSION['products'][ 0]['name'] = 'Castle Lager';
$_SESSION['products'][ 0]['number'] = 0;
$_SESSION['products'][ 0]['price'] = 1.25;
$_SESSION['products'][ 0]['disp'] = '1,25';
$_SESSION['products'][ 0]['VAT'] = 2;
$_SESSION['products'][ 1]['id'] = '2';
$_SESSION['products'][ 1]['name'] = 'Hunters Dry';
$_SESSION['products'][ 1]['number'] = 0;
$_SESSION['products'][ 1]['price'] = 1.70;
$_SESSION['products'][ 1]['disp'] = '1,70';
$_SESSION['products'][ 1]['VAT'] = 2;
etc...
and in the Products.php page, I use these variabes to fill a product list:foreach ($\_SESSION\['products'\] as $item) { etc.... }
The reason that I am filling all the individual items is that I have a PC program that maintains all this information, and automatically generates the init.php file on the server. My issue is that when I first start the page, everything works just fine. However, when I do a refresh of the page, I get the error message "Invalid argument supplied for foreach". If I test if the $_SESSION['products'] has been set, then it says that is has been set, however, if I test for "is_array ($_SESSION['products'])" then it returns false. Any ideas on how this can have happened would be greatly appreciated!!! Regards, William
i may be wrong but try using "" round the numbers in the second level of the array. ie: $_SESSION['products']['0']['id'] = '1'; if that doesnt work, try outputting the array on the page by doing this print_r(_SESSION['products']); That will show you what you have in the array
-
i may be wrong but try using "" round the numbers in the second level of the array. ie: $_SESSION['products']['0']['id'] = '1'; if that doesnt work, try outputting the array on the page by doing this print_r(_SESSION['products']); That will show you what you have in the array
Thanks a lot. Meanwhile, I found the real cause of the problem and I am ashamed of myself..... Somewhere hidden in an includefile, I used $_SESSION['products'] for a completely different purpose (shame, shame, blush). I still did not quite figure out how I get there from the code that I mentioned, but when I renamed $_SESSION['products'] to $_SESSION['merchandise'], everything worked as expected!! Thanks again, William
-
Hi all, I am writing a webshop. In the header of the "products" page, I have the following:
session\_start (); include "php/init.php";
In the init.php, I fill a numer of session variables with product information like:
if (!isset ($_SESSION['products']))
{
$_SESSION['products'][ 0]['id'] = '1';
$_SESSION['products'][ 0]['name'] = 'Castle Lager';
$_SESSION['products'][ 0]['number'] = 0;
$_SESSION['products'][ 0]['price'] = 1.25;
$_SESSION['products'][ 0]['disp'] = '1,25';
$_SESSION['products'][ 0]['VAT'] = 2;
$_SESSION['products'][ 1]['id'] = '2';
$_SESSION['products'][ 1]['name'] = 'Hunters Dry';
$_SESSION['products'][ 1]['number'] = 0;
$_SESSION['products'][ 1]['price'] = 1.70;
$_SESSION['products'][ 1]['disp'] = '1,70';
$_SESSION['products'][ 1]['VAT'] = 2;
etc...
and in the Products.php page, I use these variabes to fill a product list:foreach ($\_SESSION\['products'\] as $item) { etc.... }
The reason that I am filling all the individual items is that I have a PC program that maintains all this information, and automatically generates the init.php file on the server. My issue is that when I first start the page, everything works just fine. However, when I do a refresh of the page, I get the error message "Invalid argument supplied for foreach". If I test if the $_SESSION['products'] has been set, then it says that is has been set, however, if I test for "is_array ($_SESSION['products'])" then it returns false. Any ideas on how this can have happened would be greatly appreciated!!! Regards, William