counter problem

Discussion in 'PHP' started by izlik, Feb 26, 2009.

  1. #1
    Hello. i have this code bellow that is supposed to list av images in a folder wich is do, and then list them with 5 per row but the counter is not working and it's a long line of links insteed of 5 per row as it is supposed to be, why is this? i hope someone can help.

    <?
    $files = glob('*.jpg');
    rsort($files);
    
    $max=5;
    $i=1;
    foreach($files as $file){
        echo '<A HREF="pop.php?file=' . $file . '" onClick="return popup(this)">' . $file . '</A>';
        if($i==$max) echo '<br>'; $i=0;
        $i++;
    } 
    ?>
    PHP:
     
    izlik, Feb 26, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Putting things on the same line doesn't have any magic effect.

    What's happening here is that the "echo" only happens if the "if" is satisfied, but "$i=0" always happens either way.

    Change it to this:

    if($i==$max) { echo '<br>'; $i=0; }
    Code (markup):
    The curly braces tell it to group both of those together as the consequence of the "if".
     
    SmallPotatoes, Feb 26, 2009 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    thank you very much! :)

    also i have one more question about the code. the images are named 1.jpg, 2.jpg and so on all the way up to 4000+ how can i make the sort function sort the images from 1 to 4000 and so on? currently it's mixing the up and are in no order at all :/
     
    izlik, Feb 26, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try natsort().
     
    SmallPotatoes, Feb 26, 2009 IP