Skip to main content
Nancy OShea
Community Expert
Community Expert
November 1, 2012
Answered

PHP natsort function

  • November 1, 2012
  • 1 reply
  • 1802 views

I have a php gallery populated from a folder of thumbnails and slides like this.

GALLERY/    

     +thumbs

          01.gal.jpg

          02.gal.jpg

          03.gal.jpg

     +slides

          01.gal.jpg

          02.gal.jpg

          03.gal.jpg

Works as expected on my local testing server but my live page is displaying images in random order: 11., 04., 10., 02., etc...

My test page with PHP code.

http://alt-web.com/GALLERY/GalleryTest.php

How do I invoke natsort into my existing PHP code?  Everything I've tried so far has failed.

Thanks,

Nancy O.

This topic has been closed for replies.
Correct answer Nancy OShea

Here is an example of code I use with this function. natcasesort gives a more correct alphabetical sort, so I thought that might have been the issue.

function galleryDisplay($path, $dirpath) {

        $handle = opendir($path);

        $fileList = array();

        while ($file = readdir($handle)) {

            if ($file != "." && $file != "..") {

                array_push($fileList,$file);

            }

        }

        closedir($handle);

        natcasesort($fileList); 

        foreach ($fileList as $value) {

echo "<div class='image_item'><img src='$path/$value' draggable='true'/><br/>";  

echo "<div><a href='images.php?messagebox=delete_image&value=$value&dirpath=$dirpath'>DELETE</a>";   

echo "<a href='images.php?messagebox=open&value=$value&dirpath=$dirpath'> | VIEW</a><br/> $value</div></div>";

        }

    }


Thanks for that Rob.

I finally found a solution.  Although I had an array called $file_parts, I needed to create another array to sort.  Not exactly elegant, but it works.

http://alt-web.com/GALLERY/GalleryTest.php

Nancy O.

1 reply

Rob Hecker2
Legend
November 2, 2012

Perhaps what you want is natcasesort($fileList)

Nancy OShea
Community Expert
Community Expert
November 2, 2012

Thanks Rob.  But that doesn't work either. 

I must be overlooking something else in my coding but I don't know what.

Nancy O.

Nancy O'Shea— Product User & Community Expert
Rob Hecker2
Legend
November 2, 2012

Here is an example of code I use with this function. natcasesort gives a more correct alphabetical sort, so I thought that might have been the issue.

function galleryDisplay($path, $dirpath) {

        $handle = opendir($path);

        $fileList = array();

        while ($file = readdir($handle)) {

            if ($file != "." && $file != "..") {

                array_push($fileList,$file);

            }

        }

        closedir($handle);

        natcasesort($fileList); 

        foreach ($fileList as $value) {

echo "<div class='image_item'><img src='$path/$value' draggable='true'/><br/>";  

echo "<div><a href='images.php?messagebox=delete_image&value=$value&dirpath=$dirpath'>DELETE</a>";   

echo "<a href='images.php?messagebox=open&value=$value&dirpath=$dirpath'> | VIEW</a><br/> $value</div></div>";

        }

    }