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

PHP natsort function

Community Expert ,
Nov 01, 2012 Nov 01, 2012

Copy link to clipboard

Copied

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
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media
TOPICS
Server side applications

Views

1.5K

Translate

Translate

Report

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.

Votes

Translate

Translate
Guru ,
Nov 02, 2012 Nov 02, 2012

Copy link to clipboard

Copied

Perhaps what you want is natcasesort($fileList)

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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>";

        }

    }

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

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