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
VKX
Forum Commoner
Posts: 41 Joined: Mon Oct 03, 2005 1:43 pm
Post
by VKX » Mon Oct 03, 2005 1:47 pm
I have a gallery at
http://www.awesomestart.com/gallery
As you can see, the "Pink Floyd" skin (the most recently added skin) is displayed at the last because the gallery organizes the files by date modified.
How can I modify the code so that the thumbnails are displayed in alphabetical order?
Here's my current code to fill the gallery.
Code: Select all
<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr>
<?php
$fileregex = "[gif]"; // Specify only .gif files
$all = opendir('non-music/'); // Define image folder
$photos = array(); // Define array
// Fill the array:
while ($file = readdir($all)) {
if (!is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") {
if (preg_match($fileregex,$file)) {
array_push($photos,$file);
}
}
}
$arlength = count($photos); // Number of images in folder
//The Display Loop
for ($i=0; $i<$arlength; $i++) {
if ($photos[$i] != "_x.gif") {
$url= substr($photos[$i],0,-4);
$url = ltrim($url, "_");
echo '<td><center><a href=http://www.awesomestart.com/'.$url.'/>
<img src="non-music/'.$photos[$i].'" border="0"><br>'; include("non-music/$url.php"); echo'</a></center></td>';
$tablecount++;
if ($tablecount == 4 && ($i+1) != $arlength) {
echo'</tr><tr height="15"><td></td><td></td><td></td><td></td></tr><tr>';
$tablecount = 0;
}
}
}
?>
</td>
</tr>
</table>
<br>
<p><strong>Music:</strong></p>
<table border="0" width="100%" cellpadding="0" cellspacing="0"><tr>
<?php
$fileregex = "[gif]"; // Specify only .gif files
$all = opendir('music/'); // Define image folder
$photos = array(); // Define array
// Fill the array:
while ($file = readdir($all)) {
if (!is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") {
if (preg_match($fileregex,$file)) {
array_push($photos,$file);
}
}
}
$arlength = count($photos); // Number of images in folder
//The Display Loop
$tablecount=0;
for ($i=0; $i<$arlength; $i++) {
if ($photos[$i] != "_plain.gif") {
$url= substr($photos[$i],0,-4);
$url = ltrim($url, "_");
echo '<td><center><a href=http://www.awesomestart.com/'.$url.'/>
<img src="music/'.$photos[$i].'" border="0"><br>'; include("music/$url.php"); echo'</a></center></td>';
$tablecount++;
if ($tablecount == 4 && ($i+1) != $arlength) {
echo'</tr><tr height="15"><td></td><td></td><td></td><td></td></tr><tr>';
$tablecount = 0;
}
}
}
?>
</td>
</tr>
</table>
I appreciate the help.
Last edited by
VKX on Mon Oct 03, 2005 2:13 pm, edited 1 time in total.
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Mon Oct 03, 2005 1:56 pm
Edit your post and replace the code tags with php tags.
I’m not sure but I think your going to have to put all your data in an array, and then sort the array by alphabetical order.
http://us2.php.net/sort
VKX
Forum Commoner
Posts: 41 Joined: Mon Oct 03, 2005 1:43 pm
Post
by VKX » Mon Oct 03, 2005 2:20 pm
I tried adding
and even
right after the array, and nothing happened . . .
help! :)
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Mon Oct 03, 2005 6:33 pm
PHP does not use <> as the "not equal to" operator, this is the correct syntax:
Code: Select all
if (!is_dir($location.'/'.$file) and $file != ".." and $file != ".") {
And your regex is incorrect, [gif] will specify that the images can only have the letters g, i or f in the name. The regex you may be looking for is "/.+\.gif$/" with '/' being the delimeter.
Also take a look at using foreach loops instead of for($i=0; etc.
VKX
Forum Commoner
Posts: 41 Joined: Mon Oct 03, 2005 1:43 pm
Post
by VKX » Mon Oct 03, 2005 10:43 pm
Thanks for the tips on cleaning up the code.
Still, that's not going to help me alphabatize it. Will the foreach loops instead of for($i=0; etc." help? Can you give me more info on this?
ryanlwh
Forum Commoner
Posts: 84 Joined: Wed Sep 14, 2005 1:29 pm
Post
by ryanlwh » Tue Oct 04, 2005 12:03 am
sort() would sort the array, but the indexes still reference the same value. If you use a for loop from 0 to n, you're still going to get the same thing because sort() doesnt not change the indexes.
to better understand this, lets say we have $photos[0] = 'b' $photos[1] ='a' $photos[2]='c', sort($photos) produces:
Code: Select all
array(
[1] = 'a',
[0] = 'b',
[2] = 'c'
);
I think you can see by now for loop doesn't work here... foreach, however, would work because it follows the order of the array, not the indexes.
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Tue Oct 04, 2005 12:57 am
Jenk wrote: Also take a look at using foreach loops instead of for($i=0; etc.
When iterating through arrays I prefare using the following method:
Code: Select all
while (list($key,$val) = each($array)) {
//Do stuff
}
Where:
$Array is the array variable
$key is the array index, and
$val is the value of the array element in place $key
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Tue Oct 04, 2005 4:19 am
Not sure which version this was implemented, but you can also use the following syntax in a foreach loop:
Code: Select all
<?php
foreach ($array as $key => $val) {
//do stuff for each indice (as $key) of the array $array
}
?>
See here for more info
pilau
Forum Regular
Posts: 594 Joined: Sat Jul 09, 2005 10:22 am
Location: Israel
Post
by pilau » Tue Oct 04, 2005 4:49 am
Jenk wrote: Not sure which version this was implemented
I believe PHP4.
VKX
Forum Commoner
Posts: 41 Joined: Mon Oct 03, 2005 1:43 pm
Post
by VKX » Thu Oct 06, 2005 2:19 am
Thanks for your help guys! Everything works now.