1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Numbered Loop

Discussion in 'PHP' started by MrLeN, Dec 14, 2012.

  1. #1
    If I have these files in a directory:

    13.php
    17.php
    210.php

    How can I make a loop that will list them in numerical order?

    I know how to make a while loop, but not to take a numerical filename into account.
     
    MrLeN, Dec 14, 2012 IP
  2. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #2
    ie:

    
    <?php
    if ($handle = opendir('websites')) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
    				   $entry = str_replace('.php','', $entry);
                echo '<a href="http://'.$entry.'" target="_blank">'.$entry.'</a><br />';
            }
        }
        closedir($handle);
    }
    ?>
    
    Code (markup):
    How can I get the above code to display the files in numerical order?
     
    MrLeN, Dec 14, 2012 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    EricBruggema, Dec 14, 2012 IP
  4. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #4
    MrLeN, Dec 15, 2012 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    I would glob the files into an array... since Glob automatically sorts by filename. As such those numbered names would be auto-sorted.

    
    <?php
    foreach (glob('websites/*.php') as $siteFile) {
    	$entry=pathInfo($siteFile,PATHINFO_BASENAME);
    	echo '<a href="http://',$entry,'">',$entry,'</a><br />';
    }
    ?>
    [code]
    
    Though be warned you may end up having to build a usort it if you want 12 listed after 9, since you're not using leading zero's. This is probably why Eric linked you to the sort routines as in that case you would want to glob it in unsorted, then run a usort on it.
    
    P.S. This is 2013, not 1997... as such you shouldn't be using TARGET since that pisses on accessibility by not giving the user the choice of if it's in a new window or not... and comma delimits are a fraction faster than string addition and less prone to problems on echo, since it sends each section as it's built instead of having to build the new string before sending -- just saying.
    Code (markup):
     
    deathshadow, Dec 15, 2012 IP
  6. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #6
    I was going to help, but this was really rude so I decided not to help. He gives you most of the answer and you insult him.
     
    DomainerHelper, Dec 15, 2012 IP
  7. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Yeah, I was rude. I was rude for a reason.
     
    MrLeN, Dec 15, 2012 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    Something like this should work:

    
    <?php
    if ($handle = opendir('websites')) {
        $data = array();
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != "..") {
    	    $data[] = str_replace('.php','', $entry);            
            }
        }
        closedir($handle);
        natsort($data);
        foreach ($data as $entry) {
            echo '<a href="http://'.$entry.'" target="_blank">'.$entry.'</a><br />';
        }
    }
    
    PHP:
     
    ThePHPMaster, Dec 15, 2012 IP
    DomainerHelper likes this.
  9. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #9
    You are both childish.
     
    DomainerHelper, Dec 16, 2012 IP