Invalid argument supplied for foreach

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Invalid argument supplied for foreach

Post by toasty2 »

I'm reading a directory for HTML files and trying to save them to an array (also with information about like when each file was last modified) so they can later be used in a table.
Here's my array of files:
$FILES:

Code: Select all

Array
(
    [0] => Array
        (
            [name] => example
            [mod] => 1184979992
            [created] => 1197347342
        )
    [1] => Array
        (
            [name] => index
            [mod] => 1208139263
            [created] => 1197347342
        )
    [2] => Array
        (
            [name] => menu
            [mod] => 1208124930
            [created] => 1197347342
        )
)
That information is prepared earlier in the script. Inside a function I try to create a table:

Code: Select all

global $FILES, $NAV;
$ctableHTML = '<table id="contenttable"><tr><td>Name</td><td>Date Created</td><td>Last Modified</td><td></td></tr>';
foreach($FILES as $f) // The line it says the error occurs at
{
  $ctableHTML .= "<tr><td>{$f['name']}</td><td>{$f['created']}</td><td>{$f['mod']}</td><td><a href=\"?a=edit&f={$f['name']}\">edit</a></td></tr>";
}
$ctableHTML .='</table';
 
$PAGE = <<<html
$NAV
$ctableHTML
html;
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Invalid argument supplied for foreach

Post by yacahuma »

And your question is?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Re: Invalid argument supplied for foreach

Post by toasty2 »

Why do I get the error with foreach?
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Invalid argument supplied for foreach

Post by nowaydown1 »

Can you post your full script? It would help us track down your issue.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Re: Invalid argument supplied for foreach

Post by toasty2 »

I figured it out. My array didn't exist before I tried to use it. :|
Post Reply