Arranging output of opendir function by last modification?

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

User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well ya. You're creating the $date variable instead of the $filename variable. If no $filename variable is being instantiated, its certainly not going to print out.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

Sould i t look like this?

Code: Select all

<?php
$arrayFiles=glob('*.*');
$date=date ("F d Y H:i:s.", filemtime($filename));
if($date){
  foreach ($date as $filename) {
   echo "$filename <br>";
  }
}
else
  echo"File not found."

?>
If so, it is returning this error:

Code: Select all

Warning: Invalid argument supplied for foreach() in /host/htdocs/vuns/index.php on line 5
Last edited by ozzy on Sun Apr 16, 2006 2:00 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ozzy wrote:Sould i t look like this?

Code: Select all

<?php
$arrayFiles=glob('*.*');
$date=date ("F d Y H:i:s.", filemtime($filename));
if($date){
  foreach ($date as $filename) {
   echo "$filename <br>";
  }
}
else
  echo"File not found."

?>
If so, it is returning this error:

Code: Select all

Warning: Invalid argument supplied for foreach() in /host/bluemicro.digibase.ca/htdocs/vuns/index.php on line 5
Come on dude you know you can do a little more reading around ;)

http://www.php.net/foreach

php.net is your friend (and my friend!).

foreach() is a speacial loop. It loops over an array. The basic structure is one of the two possible variations below:

Code: Select all

foreach ($some_array as $value)
{
    //Look at each value in the array and tore it temporarily in $value
}

foreach ($some_array as $key => $value)
{
    //In an array like:
    // $some_array['foo'] = 'bar';
    // Look at each key (foo) and each value (bar)
}
Also... the error messages you are seeing are extremely descriptive and as such should be a clear indication at the very least of which line in your code to check.... then you can refer to the manual at php.net again ;)

As you may have noticed... we don't often just write out code for you... you learn far more struggling through it by yourself :D
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post by ozzy »

I have looked through the foreach manual on php.net.. but i didnt fully understand it.

I also know what the problem is, but requests are of how to reslove them.

Code: Select all

Warning: Invalid argument supplied for foreach() in /host/htdocs/vuns/index.php on line 5
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you are not passing an array to the foreach().
Post Reply