By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

order alfabetical many images

P: n/a
Hi all.

With this simple script I can see all my jpg in a web page..
but they are confused randomly in the page.

is there a way to put them alfabetically? from A to Z?
thanx

<?php
if ($handle = opendir('/cover')) {
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..")) {
echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
}
}
closedir($handle);
}

?>

Oct 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Davide wrote:
Hi all.

With this simple script I can see all my jpg in a web page..
but they are confused randomly in the page. <?php
if ($handle = opendir('/cover')) {
while (false !== ($file = readdir($handle))) {
if (($file != "." && $file != "..")) {
echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
}
}
closedir($handle);
}

?>


Put all the files in an array and sort it, then print

$files = array();

if (($file != "." && $file != "..")) {
$files[] = $file
}
all done:

asort($files);

for each($files as $file){
echo " <img src=\"cover/$file\" alt=\"$file\" border=0> ";
}
Oct 15 '05 #2

P: n/a
jamen wrote:
Put all the files in an array and sort it [...]
asort($files);


No need for asort(), just use sort(). See http://php.net/sort

--
E. Dronkert
Oct 15 '05 #3

P: n/a
I correct the script with yours suggests but seems doesn't work :(

<?php
if ($handle = opendir('./cover')) {
while (false !== ($file = readdir($handle))) {
$files = array($file);
if (($file != "." && $file != "..")) {
sort($files);
foreach($files as $file){
echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
}
}
}
closedir($handle);
}

?>

Oct 16 '05 #4

P: n/a
Davide wrote:
I correct the script with yours suggests but seems doesn't work :(

<?php
if ($handle = opendir('./cover')) {
while (false !== ($file = readdir($handle))) {
$files = array($file);
if (($file != "." && $file != "..")) {
sort($files);
foreach($files as $file){
echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
}
}
}
closedir($handle);
}

?>


if ($handle = opendir('./cover')) {
$files = array();

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

foreach($files as $file){
echo "<img src=\"cover/$file\" alt=\"$file\" border=0>";
}

}
Oct 16 '05 #5

P: n/a
it works!

Thanx, really :)

Oct 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.