djkirstyjay Posted August 19, 2010 Share Posted August 19, 2010 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 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! I'm getting there... slowly! Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/ Share on other sites More sharing options...
djkirstyjay Posted August 19, 2010 Author Share Posted August 19, 2010 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101330 Share on other sites More sharing options...
djkirstyjay Posted August 19, 2010 Author Share Posted August 19, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101342 Share on other sites More sharing options...
Rifts Posted August 19, 2010 Share Posted August 19, 2010 try LIMIT 0, 10 where 0 = the start 10 = the number you want Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101348 Share on other sites More sharing options...
djkirstyjay Posted August 19, 2010 Author Share Posted August 19, 2010 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101356 Share on other sites More sharing options...
Rifts Posted August 19, 2010 Share Posted August 19, 2010 Oh you aren't using mysql to store the images sorry my bad normally you would do something like SELECT * FROM `your_table` LIMIT 0, 10 Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101362 Share on other sites More sharing options...
djkirstyjay Posted August 19, 2010 Author Share Posted August 19, 2010 No probs... thanks for the help... it has put me sort of on the right track, as I have now found the $range(0,10) function... just have to figure out where to put it now! Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101368 Share on other sites More sharing options...
jcbones Posted August 20, 2010 Share Posted August 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101467 Share on other sites More sharing options...
djkirstyjay Posted August 20, 2010 Author Share Posted August 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101553 Share on other sites More sharing options...
jcbones Posted August 20, 2010 Share Posted August 20, 2010 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"; Quote Link to comment https://forums.phpfreaks.com/topic/211203-my-foreach-statement-is-not-finding-the-array-can-anyone-see-why/#findComment-1101724 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.