Help with loops and array.

Hi, long time lurking, finally signed up!, I need some help, I’m learning to programme this PHP stuff and I’m stuck!

This works…

[php]

<?php echo '
    '; $dir0 = '../images0'; $file_display = array('jpg', 'jpeg', 'png'); if (file_exists($dir0)== false) { echo 'Directory \'', $dir0, '\' not found!'; } else { $dir_contents = scandir($dir0); foreach ($dir_contents as $file) { $file_type = strtolower( end (explode ('.', $file))); if($file !== '.' && $file !== '..' && in_array($file_type, $file_display)== true) { echo '
  • ', $file,'
  • '; } } } echo '
'; ?>

[/php]

I would like to be-able to add more directories which can be pulled by this script, I’ve got this …

[php]
<?php
echo ‘

    ’;
    $dir0 = ‘…/imgages0’;
    $dir1 = ‘…/images1’;
    $file_display = array(‘jpg’, ‘jpeg’, ‘png’);
    if (file_exists($dir0)== false)
    {
    echo ‘Directory ‘’, $dir0, ‘’ not found!’;
    }
    else
    {
    $dirlist =array($diro, $dir1);
    $dir_contents = scandir($dirlist);
    foreach ($dir_contents as $file)
    {
    $file_type = strtolower( end (explode (’.’, $file)));
    if($file !== ‘.’ && $file !== ‘…’ && in_array($file_type, $file_display)== true)
    {
    echo ‘
  • ', $file,'
  • ’;
    }
    }
    }
    echo ‘
’;
?>
[/php]

Am I heading down the right road with this ? some help and advice would be amazing thanks guys

First think i see is a type you have imgages0 not images0 in your if then statement which could be causing you issues. Also look into more complex if/then statements using things like || and && but it looks like you are on the right track.

Look at this function which does directory work it should do what you want with a little modification. If you need help implementing it let us know.

thanks I shall check this link out and get back to you,

it’s not the filepath that’s the problem, I just mistyped that removing real file path (paranoid like that lol)

yes, I was thinking some kind of && opperator

I would recommend looking into using PHP’s DirectoryIterator or RecursiveDirectoryIterator classes. I would also personally use pathinfo to determine the files extension.

Here is a quick example using DirectoryIterator:
[php]
$dirs = array(‘images0’, ‘images1’);
foreach($dirs as $dir) {
foreach(new DirectoryIterator($dir) as $file) {
if (!$file->isFile()) {
continue; // skip directories (including . or …)
}

	// validate extension
	$pathinfo = pathinfo($file->getPathname());
	if (in_array($pathinfo['extension'], $file_display)) {

	}
}

}
[/php]

Hey M@tt that’s really helped me out I’ve got it working :slight_smile:

[php]

<?php echo '
    '; $dirs = array('images0', 'images1'); foreach($dirs as $dir) { $file_display = array('jpg', 'jpeg', 'png'); if (file_exists($dir)== false) { echo 'Directory \'', $dir, '\' not found!'; } else { $dir_contents = scandir($dir); foreach ($dir_contents as $file) { $file_type = strtolower( end (explode ('.', $file))); if($file !== '.' && $file !== '..' && in_array($file_type, $file_display)== true) { echo '
  • ', $file,'
  • '; } } } } echo '
'; ?>

[/php]

^ This now works and displays the files from both directories on the one webpage, I like the idea of the iterator (I like using the correcy ways, could you show me clearly how I can change my code to this, I tried your example but it said “wrong extention” I think

What version on PHP are you using? I believe the Iterator classes require 5.1.2 or higher.

I can’t post links but you can Google “PHP SPL iterators”

I’ve no idea I imagine it’s the latest, I use 1and1 hosting, I’ll google it, but this does what I need, I don’t mind having the array for the folders as it suits the way it’s set up

I’ve got a page with 2 gallerys on, and the images in one are in one dir, and the other in another, (hence images0 images 1)
but then I’ve got a page which is more simplified and shows all the images but in a larger format, (hence needing this) and the gallerys them self populate with this script,
[php]
.parrymedia.co.uk/pages/manchesterPride2012Extra.php (as one)

.parrymedia.co.uk/pages/manchester2012.php (split)
[/php]

it’s hard to explain, but I basically wanted to make a website which was as simple as possible, which does not require the client do have anything extra (like java script etc)

Sponsor our Newsletter | Privacy Policy | Terms of Service