Page 1 of 1

Array splice and unset

Posted: Tue Jan 26, 2010 3:26 pm
by Tsail
Hi,
I have created this code to scan a directory and create a Light box gallery for each of them but for some reason when I run it i am getting 3 of the first file (the one that name should be) in each gallery and can't work out why?

Here is my code:

Code: Select all

<?php
 
$dir     = '../../Gallery/2009/';
$files1 = scandir($dir);
unset($files1[0], $files1[1]);
 
foreach ($files1 as $value)  {
  $sub = scandir($dir . $value) ;
  unset($sub[0], $sub[1], $sub[2]);
  $name = scandir($dir . $value) ;
  unset($name[0], $name[1]);
  array_splice($name, 1);
  $first = implode($name);
  echo "<p> <a href=\"$dir$value/$first\" rel=\"lightbox[$value]\" title=\"$first\"><img src=\"$dir$value/$first\" Height=25% width=25%</a></p> <p>$value</p>" ;
  foreach($sub as $value2)
  $result = count($sub);
    if ($result > 0)
         foreach ($sub as $value2) {
           echo "<a href=\"$dir$value/$value2\" rel=\"lightbox[$value]\" title=\"$value2\"></a>";
 
}
}
?>
Thanks for any help

Please ask if you want anymore info.

Tsail

Re: Array splice and unset

Posted: Tue Jan 26, 2010 4:07 pm
by AbraCadaver
I can't work out what you're trying to do in your code, but a couple of things:

1. glob() might be better than scandir() as it will give full paths and won't return . current and .. parent
2. Before you scandir() or glob a child file you need to check if it is_dir() first
3. I can't tell what output you want or what $first is used for