foreach issue

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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

foreach issue

Post 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){"
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post 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
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

The problem is this line

$dirs=sort($dirs);

just use:

sort($dirs);

sort returns void, not a sorted array...
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

ok.. now it's not making links of the two test directories...

aside from that it's fine
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post 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
Post Reply