ordering a while statement

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

Post Reply
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

ordering a while statement

Post 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
Last edited by slifin on Sun Jul 27, 2008 4:12 pm, edited 1 time in total.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: ordering a while statement

Post by jaoudestudios »

If it worked before but does not now that you have changed servers, I would check php version and permissions
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

Re: ordering a while statement

Post 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
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

Re: ordering a while statement

Post 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
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

Re: ordering a while statement

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: ordering a while statement

Post 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.
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

Re: ordering a while statement

Post 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
slifin
Forum Newbie
Posts: 6
Joined: Sun Jul 27, 2008 3:50 pm

Re: ordering a while statement

Post 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>';
 
?>
Post Reply