Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2013, 02:26 PM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Exclamation problems reading files from directory - doesnt display in order :(

Hi All,

I have a gallery php page which gathers the photos from a specified directory or images, it is working ok but the major probem im having is that it doesnt always display the files in order.

By this i mean if my images are titled img0, img1, img2 .... img10, img11, img12

they are ordered like so
img0, img1, img10, img11, img12, img2

and not like
img0, img1, img2, img10, img11, img12

does anyone know how to fix this?

here is my code
PHP Code:
            <?php
            
if(isset($is_gallery))//has the user opened a portfolio category
            
{
                
$breadcrumbs "<a href='portfolio.php'>Portfolio</a>"." / "."<a href='portfolio.php?gallery=$is_gallery'>".ucwords($is_gallery)."</a>";
                echo 
$breadcrumbs;
                
#echo "<h3>".ucwords($is_album)."</h3>";
                
$dir "portfolio/".$is_gallery;
                
$dir2 $dir."/";    
                                                
                echo 
"<h3>".$is_gallery." Gallery</h3>";
                
$count 0;
                
//display thumbs
                
if ($handle opendir($dir))
                {
                    
$x 1;?>
                    <div class='wrapper'>
                    <ul class='gallery clearfix'>
                    <?php
                    
while (false !== ($entry readdir($handle)))
                    {
                        if(
$entry != "." && $entry != ".." && strpos($entry,"."))
                        {
                            
$default_file $dir."/".$entry;
                            
$target_file $dir."/".$entry;
                            
$kaboom explode("."$target_file); // Split file name into an array using the dot
                            
$fileExt end($kaboom); // Now target the last array element to get the file extension
                            
$resized_file $dir."/resized/".$entry;
                            
$wmax 250;
                            
$hmax 250;
                            
ak_img_resize($target_file$resized_file$wmax$hmax$fileExt);
                        
                            
// ----------- End Adams Universal Image Resizing Function ----------
                            // ------ Start Adams Universal Image Thumbnail(Crop) Function ------
                            
$target_file $resized_file;
                            
$thumbnail $dir."/thumbs/".$entry;
                            
$wthumb 150;
                            
$hthumb 150;
                            
ak_img_thumb($target_file$thumbnail$wthumb$hthumb$fileExt);
                                
                            
$filename explode(".",$entry);
                            
$filename $filename[0];?>
                            <div class="gallery-image">
                                <figure class="img-box">
                                <li>
                                    <a class="lightbox-image" href="<?php echo $default_file?>" rel="prettyPhoto[]" title="<?php echo $desc;?>">
                                    <img src="<?php echo $thumbnail?>" width="150" height="150" alt="<?php echo $alt?>" title="<?php echo $alt?>" /></a>
                                </li>
                                </figure>
                            </div><?php
                            $count
++;
                        }
                    }
?>
                    </ul>
                    </div><?php
                
}
            }
any help would be greatly appreciated
Thanks

Luke
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook
LJackson is offline   Reply With Quote
Old 02-23-2013, 03:24 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Age old problem because the sort reads from left to right. First the i, then the m, then the g and then the 1 (everything with a 1 in fourth slot).

One way to do this is to sort by length first then by number.
sunfighter is offline   Reply With Quote
Old 02-23-2013, 05:11 PM   PM User | #3
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
hmm ok thanks! any idea how i can integrate the idea in to my above code? thanks

Luke
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook
LJackson is offline   Reply With Quote
Old 02-23-2013, 07:03 PM   PM User | #4
gvre
Regular Coder

 
Join Date: May 2011
Posts: 212
Thanks: 1
Thanked 50 Times in 49 Posts
gvre is an unknown quantity at this point
You could add the filenames in an array, sort it using natsort and loop through it.
gvre is offline   Reply With Quote
The Following 2 Users Say Thank You to gvre For This Useful Post:
LJackson (02-23-2013), sunfighter (02-23-2013)
Old 02-23-2013, 11:06 PM   PM User | #5
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
@gvre Thanks for the heads up on natsort. Never know it existed.
sunfighter is offline   Reply With Quote
Old 02-23-2013, 11:29 PM   PM User | #6
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
thanks @gvre natsort is a life saver exactly what i was after!

appreciate your time folkes!
Luke
__________________
Kernow Connect: Online Shopping, Price Comparison, Maximum Savings On Top UK Stores
Follow Us On: Twitter | Facebook
LJackson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 AM.


Advertisement
Log in to turn off these ads.