Page 1 of 1

foreach issue

Posted: Sat Jun 07, 2003 9:41 pm
by m3rajk
trying to get a list of directories to make links to them, and i'm having an issue in the foreach loop that's to make the links.

the orielly php book says:

Code: Select all

foreach ($addresses as $value){
echo "processing $value\n";
}
and i used that as a basis in...

Code: Select all

foreach ($dirs as $dir){
echo "<a href="phppage.php?variable=$dir" target="otherframe">$dir</a>";
}

the book example has an array $addresses

in my code $dirs is an array. why does my code have a parse error on the line "foreach ($dirs as $dir){"

Posted: Sun Jun 08, 2003 12:56 am
by AVATAr
Im sorry but you dont have an arror on those lines... please show us some more code, maybe the error is there..

i've tested your code and it works ok

Posted: Sun Jun 08, 2003 4:09 am
by twigletmac
The problem is most likely in the code above the foreach loop - parse errors are reported for the line where PHP could no longer continue processing which is generally not the line in which the error exists.

Mac

Posted: Sun Jun 08, 2003 10:36 am
by m3rajk
twigletmac wrote:The problem is most likely in the code above the foreach loop - parse errors are reported for the line where PHP could no longer continue processing which is generally not the line in which the error exists.

Mac
i noticed that. aside from how i create the array (which according tot he book is correct) i did the foreeach just like the book.


i have two sub directories i make to test this. once i get this working i have a different page i'm making to make the galleries.. my mother wants a way to share photos with people and i figured since she keeps complaining i should make something quick to silence her and forcing her to put them in sub directories would be the easiest thing to do.

this is the function to make the list of galleries. each sub directory becomes a list.

Code: Select all

function gallist(){ // should be a list of subdirectories                                                                   
  echo "<html><body bgcolor="#000000" text="#ffffff"><center><p>the galleries are...\n<br>";
  // get directories                                                                                                        
  $dirs=array(); // empty directory array                                                                                   
  $d=opendir(getcwd()) or die($php_errormsg); // open the current directory
  while(false !==($f=readdir($d))){ // read what's in the directory
    $posdir=$d.'/'.$f; // make it the full working path
    if(is_dir($posdir)){ // if it's a directory
      $dirs[] = $f; // add it to the list
    }
  }
  $dirs=sort($dirs);
  // iterate though the list of directories making links calling gallery.php?dir=$dir                                       
  foreach($dirs as $dir){
    echo "<a href="gallery.php?dir=$dir" target="display">$dir</a>\n<br>";
  }
  echo "</body></html>";
}
that's the function for the listing

Posted: Sun Jun 08, 2003 12:04 pm
by discobean
The problem is this line

$dirs=sort($dirs);

just use:

sort($dirs);

sort returns void, not a sorted array...

Posted: Sun Jun 08, 2003 12:30 pm
by m3rajk
ok.. now it's not making links of the two test directories...

aside from that it's fine

Posted: Sun Jun 08, 2003 1:23 pm
by discobean
You have a line in there:

$posdir = $d . '/' . $f;

$d isn't the directory name or path, instead its a resource.

so I replaced it with:

$posdir = $f;

ie. the relative filename, and it should print out the correct directories..

Learn how to debug your code by printing out your variables as your script runs, a simple problem like this could have been solved easily with this process, eg. echo $posdir; in the while() loop, or a echo "Adding to dirs[] array $f"; in the is_dir($posdir) statement