www.webdeveloper.com
Results 1 to 8 of 8

Thread: Displaying HTML links to files in alphabetical order?

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    60

    Displaying HTML links to files in alphabetical order?

    Hello

    I have a script that searches a folder on my server for pdf files then displays links to them on a web page in an unordered list. My questions is... is it possible to change the order that these files are displayed? Eg. alphabeticaly? I can't work out how these files are being displayed by default... it doesn't appear to be by modified date or alphabeticaly?

    Any help greatly appreciated!

    PHP Code:
    <?php 

        $dom 
    = new DOMDOcument (); 
        
    $ul $dom->appendChild ($dom->createElement ('ul')); 
         
        foreach (
    glob ('*.pdf') as $pdf) { 
            
    $li $ul->appendChild ($dom->createElement ('li')); 
            
    $a $li->appendChild ($dom->createElement ('a')); 
            
    $a->appendChild ($dom->createTextNode (str_replace ('_'' 'pathinfo ($pdfPATHINFO_FILENAME)))); 
            
    $a->setAttribute ('href'$pdf); 
            
    $a->setAttribute ('type''application/pdf'); 
        } 
         
        echo 
    $dom->saveHTML(); 

    ?>

  2. #2
    Join Date
    Apr 2010
    Posts
    227
    Hi

    I have a script that allows users to download pdf files from my site.

    To make it simple as possible i just dump a new file in and thats it.

    How i make it work is simple.

    I turn the file name into effectively an array.

    orderdisplayed_Title_FileDescription.pdf

    PHP Code:

                               $x 
    explode("_",$filename);
                               
    $title $x[1];
                               
    $description $x[2]; 
    Order displayed is of course automatic as youve discovered. So just put A or B etc or 1 or 2 etc in the file name at start.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    You could simply assign the result of glob() to a variable, then apply the sorting function of preference (e.g. natcasesort()?) to that array, then run your foreach on the sorted array.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Apr 2010
    Posts
    227
    Quote Originally Posted by NogDog View Post
    You could simply assign the result of glob() to a variable, then apply the sorting function of preference (e.g. natcasesort()?) to that array, then run your foreach on the sorted array.
    Nice

  5. #5
    Join Date
    Apr 2009
    Posts
    60
    Thanks for your replies folks! I'll see what I can do!

  6. #6
    Join Date
    Apr 2009
    Posts
    60
    OK... I'm rubbish at this and am having trouble. I've been trying this:
    PHP Code:
    <?php 

        $dom 
    = new DOMDOcument (); 
        
    $ul $dom->appendChild ($dom->createElement ('ul')); 
        
        
    $articles glob('*.pdf') as $pdf);
        
        
    rsort($articles);
         
        foreach 
    $articles) { 
            
    $li $ul->appendChild ($dom->createElement ('li')); 
            
    $a $li->appendChild ($dom->createElement ('a')); 
            
    $a->appendChild ($dom->createTextNode (str_replace ('_'' 'pathinfo ($pdfPATHINFO_FILENAME)))); 
            
    $a->setAttribute ('href'$pdf); 
            
    $a->setAttribute ('type''application/pdf'); 
        } 
        
        echo 
    $dom->saveHTML(); 

    ?>
    I'm getting this error: Parse error: syntax error, unexpected T_AS in.......

    Any guidance would be so appreciated!

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    PHP Code:
        $articles glob('*.pdf');
        
        
    rsort($articles);
         
        foreach (
    $articles as $pdf) { 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Apr 2009
    Posts
    60
    Quote Originally Posted by NogDog View Post
    PHP Code:
        $articles glob('*.pdf');
        
        
    rsort($articles);
         
        foreach (
    $articles as $pdf) { 
    Aha! I just couldn't see it...

    Thank you so much NogDog!!! You're a star!!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles