Array splice and unset

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
Tsail
Forum Newbie
Posts: 3
Joined: Sun Jan 24, 2010 3:51 pm

Array splice and unset

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array splice and unset

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply