Page 1 of 1

ordering a while statement

Posted: Sun Jul 27, 2008 3:55 pm
by slifin
hi there I recently moved server an suddenly some of my code isn't working as expected
this code would usually output the filenames of images in a folder called img relative to
the php file and output it as an xml file for consumption by flex alphabetically

however it isn't doing that any more so I need your help how can I order my filenames
and echo them out in this while loop alphabetically?

Failing that what other alternatives can I use to the while loop

Code: Select all

<?php
//define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
 
//this section calculates the image names in the directory
$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
 
$dimg = opendir($imgdir);
 
 
 
echo "\n<gallery>
  <image>\n";
 
 
 
 
 
while($imgfile = readdir($dimg))
{
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        echo '    <url>img/' . $imgfile . "</url>\n";
    }
}
 
echo '  </image>
</gallery>';
 
?>
Thanks for reading

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:01 pm
by jaoudestudios
If it worked before but does not now that you have changed servers, I would check php version and permissions

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:10 pm
by slifin
I don't host the server myself my files are on a paid for web host so I figured there's not much I can do there

so I decided I'd have to re-code the script to ensure it comes out of the while loop ordered

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:24 pm
by slifin
Hmm I've had a thought I could feed my

$imgfile string (that holds my filenames in)
into an array and then sort the array then turn my array back into a string
and echo it out

how would I do that?

Code: Select all

 
$imgfile = array ();
sort($imgfile);
?

no idea how I would turn it back into a string though

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:28 pm
by slifin
hmm been googling looks like theres a function to turn an array back into a string

implode(); just not sure how to implement this

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:29 pm
by califdon
I don't see anything in your while loop that affects the order of the array elements. Your loop is doing nothing but reading the directory and if the file type is among the acceptable ones, echoing them. Probably on the previous server, the directory listings were in alphabetic order by default, and at the new server they are not. You might try going into the directory manually and sorting by filename, but I'm not sure if that will change anything.

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:41 pm
by slifin
I figured the same but it appears ordered in my ftp client and auto arranging them does nothing

as the change is client side not server side

I've tried reuploading them in order too but that doesn't help

heres the current order http://dinogod.com/comic/2/list.php

its probably sorted by size on the server

Re: ordering a while statement

Posted: Sun Jul 27, 2008 4:44 pm
by slifin
soultion:

Code: Select all

 
<?php
//define the xml standard
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
 
//this section calculates the image names in the directory
$imgdir = 'img/'; // the directory, where your images are stored
$allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes you want to show
 
$dimg = opendir($imgdir);
 
 
echo "\n<gallery>
  <image>\n";
 
 
 
$imgs = array();//create the blank array
 
while($imgfile = readdir($dimg))
{
    if(in_array(strtolower(substr($imgfile,-3)),$allowed_types))
    {
        $imgs[] = $imgfile;//add to the array in the loop
    }
}
sort($imgs);
foreach($imgs as $img){//loop through the images to output them
 
    
 
echo '    <url>img/' . $img . "</url>\n";
}
echo '  </image>
</gallery>';
 
?>