Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

PHP natsort function

Community Expert ,
Nov 01, 2012 Nov 01, 2012

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.

Nancy O'Shea— Product User, Community Expert & Moderator
TOPICS
Server side applications
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 03, 2012 Nov 03, 2012

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.

Translate
Guru ,
Nov 02, 2012 Nov 02, 2012

Perhaps what you want is natcasesort($fileList)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 02, 2012 Nov 02, 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 & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Nov 02, 2012 Nov 02, 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>";

        }

    }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 03, 2012 Nov 03, 2012
LATEST

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.

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines