Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. PHP: Invalid argument supplied for foreach ()

PHP: Invalid argument supplied for foreach ()

Scheduled Pinned Locked Moved Web Development
helpphpsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    William Engberts
    wrote on last edited by
    #1

    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

    N M 2 Replies Last reply
    0
    • W William Engberts

      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

      N Offline
      N Offline
      neilmajithia
      wrote on last edited by
      #2

      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

      W 1 Reply Last reply
      0
      • N neilmajithia

        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

        W Offline
        W Offline
        William Engberts
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • W William Engberts

          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

          M Offline
          M Offline
          Mohammad Dayyan
          wrote on last edited by
          #4

          Please ask these questions here[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups