Jump to content

My foreach statement is not finding the array. Can anyone see why?


Recommended Posts

I am having a go at writing my own simple gallery.

 

As the I want to display an image from a folder in a directory of all the images... for example, I have 10 folders each containing images. I want my code to take image 01.jpg out of each folder and display it on my page, so that when I add folders to my main images folder, they will automatically be added to the page... but I only want the last 10 folders to display... so once I have 20 folders, it only shows 10.

 

I also want the images to be 5 across and then make a new row.

 

My folders will be numbered in order... 001, 002, 003, etc... and I need them sorted in reverse order, so the folder added last is displayed first.

 

So... this is where I am at with the code :

 


$dir = "gallery/";

if (is_dir($dir)) {
[color=red]     foreach(array_keys($file) as $n) {[/color]
        if ($n+1 == 5) {
            echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td></tr><tr>";
        }
	elseif ($n+1 > 10) {
		echo "";
		}
	else {
		echo "<td><img src=\"" . $dir . $file . "/01.jpg\" width=\"140px\" height=\"93px\" alt=\"\" /></td>";
		}
        closedir($dh);
    }
}

 

For some reason, I am getting the following error on the page :

 

Warning: First argument to array_keys() should be an array in /path/to/gallery.php on line 8

 

Warning: Invalid argument supplied for foreach() in /path/to/gallery.php on line 8

 

line 8 is the one in red above.

 

So... I have a couple of questions...

 

1. What have I done wrong so far? I cannot see it  :confused:

2. How would I then go about putting the galleries in reverse order? I know it must be rsort, but not to sure if I need to do this first or after... ?

 

Any help greatly appreciated, and I would also be greatful f an idiot's explanation, as I am really trying to understand all this!  :D

 

I'm getting there... slowly!  ::)

 

Just to add to the above...

 

I have just used some old code from another page I did a while back and it seems to work listing the images... but I need them in reverse order... how would I do this in the following code?... I have been reading around and I think KRSORT would be better than RSORT...?

 

<?php
$images = "gallery/"; # Location of gallery
$cols   = 5; # Number of columns to display 

if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != "..") { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$colCtr = 0; 

echo "<table><tr>"; 

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    echo "</tr><tr>"; 
  echo "<td><img src=\"" . $images . $file . "/01.jpg\" width=\"140px\" alt=\"\" /></td>"; 
  $colCtr++; 
} 

echo "</table>" . "\r\n"; 

?>

Hoorah! done it...

 

<?php
$images = "gallery/"; # Location of gallery
$cols   = 5; # Number of columns to display 

if ($handle = opendir($images)) { 
   while (false !== ($file = readdir($handle))) { 
       if ($file != "." && $file != "..") { 
           $files[] = $file; 
       } 
   } 
   closedir($handle); 
} 

$colCtr = 0; 

echo "<table><tr>"; 

krsort($files);

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    echo "</tr><tr>"; 
  echo "<td><img src=\"" . $images . $file . "/01.jpg\" width=\"140px\" alt=\"\" /></td>"; 
  $colCtr++; 
} 

echo "</table>" . "\r\n"; 

?>

 

Now... I just need to limit it to the last 10 items...

Where would I use this? Ive just done a search on PHP.net for info on 'limit' but I cannot find any... probably because it's such a commonly used word  :D

 

do you have a link to anything that would explain it, or a searchphrase that would pull up results, as I've not seen that function before :)

This is just a suggestion:

Remove:

krsort($files);

 

Then change:

foreach($files as $file) 
{ 
  if($colCtr %$cols == 0) 
    echo "</tr><tr>"; 
  echo "<td><img src=\"" . $images . $file . "/01.jpg\" width=\"140px\" alt=\"\" /></td>"; 
  $colCtr++; 
} 

 

To

$c = count($files) - 1; //$c = the number of files in the array, minus 1 = highest index in the array.
for($i = $c; $i >= ($c - 10); $i--) //$i = $c, $i is greater or equal to the array count minus 10, $i de-increments on each loop.  This will give us a 10 countdown.
{ 
  if($colCtr %$cols == 0) 
    echo "</tr><tr>"; 
  echo "<td><img src=\"" . $images . $files[$i] . "/01.jpg\" width=\"140px\" alt=\"\" /></td>"; //echo'ing out the $file[$i] on the loop, will give us the last 10 files in the file array.
  $colCtr++; 
} 

 

That should work.  Although I have been known to have blonde moments.

Thanks for the help :)

 

I tried the above, as I see what you are doing there, and changed the $c - 10 to $c - 5 because I needed to test it, and I only have 6 folders in there at present... and it's showing all 6. I cannot see anywhere where this would be effected so I'm a little confused  :confused:

 

 

Try removing part of the conditional in the for loop.

//From
for($i = $c; $i >= ($c - 10); $i--)
//To
for($i = $c; $i > ($c - 10); $i--)  //changed "greater than or equal to" into "greater than";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.