SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Thread: loop: if last

  1. #1
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    loop: if last

    I have seen for $i loops which give special instructions if it is the last element in a loop.

    I have this "if last in loop, do this" script working, but can anyone revise it? I have seen a simpler version somewhere using $i < satan, etc

    PHP Code:
    // This outputs "1, 2, 3, 4, 5, 6, 7, 8, 9"
    $counter 1
    $limit 10;

    while(
    $counter $limit) {

        if (
    $counter == $limit 1){
            print 
    $counter;
                    
        } else {
        
        print 
    $counter;
        echo 
    ", ";
        
        }
         
    $counter++;


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code PHP:
    // This outputs "1, 2, 3, 4, 5, 6, 7, 8, 9"
    $counter = 1; 
    $limit = 10;
    echo $counter;
    for (var $i = $counter + 1; $i < $limit; $i++) {
        echo ', ' . $counter;
    }

  3. #3
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats it, thanks!

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    or just rtrim(); the trailing comma you want to get rid of
    PHP Code:
    $counter 1
    $limit 10;
    $output='';

    while(
    $counter $limit) {
     
    $output . = $counter ',' ;
    }

    echo 
    rtrim$output ',' ); 
    I suppose it depends on where/when you want to echo the values out.

  5. #5
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about this

    Code:
    $start = 1; 
    $limit = 10;
    echo implode(', ', range($start, $limit-1));

  6. #6
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's a pretty neat way of doing it

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I like that. I was wanting to use implode earlier, but couldn't recall about the range function.

    I'll be adding that to my repertoire now, thanks galen.

  8. #8
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sweet, glad I asked

  9. #9
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    I like that. I was wanting to use implode earlier, but couldn't recall about the range function.

    I'll be adding that to my repertoire now, thanks galen.

  10. #10
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How can I apply your script to this while loop? I am trying to put a pipe after every result except the last.

    PHP Code:
    // outputs "red | green | yellow | orange | blue"

    $sql "SELECT DISTINCT category FROM script ORDER BY category ASC";
    $result mysql_query($sql) or die(mysql_error().': '.$sql);
    $num mysql_num_rows($result); 
    $count 0;

    while(
    $row mysql_fetch_assoc($result)) 
    {
        
    $category $row['category'];
        echo 
    "<a href='view.php?category=$category'>$category</a> ";
        
    $count++;
        
        if (
    $num $count){
        echo 
    " | ";
        }


  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // outputs "red | green | yellow | orange | blue"

    $sql "SELECT DISTINCT category FROM script ORDER BY category ASC";
    $result mysql_query($sql) or die(mysql_error().': '.$sql);
    $count 0;
    $links = array();

    while(
    $row mysql_fetch_assoc($result)) 
    {
        
    $links[$count++] =  "<a href='view.php?category={$row['category']}'>{$row['category']}</a>";
    }

    echo 
    implode(' | '$links); 

  12. #12
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that whole count variable is unnecessary. keep in my mind this method is a waste of memory if you dont plan on ever using the array.

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That would have been a copy/paste error

  14. #14
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks pmw57

    galen which would you recommend for efficiency?

  15. #15
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd probably do this

    Code PHP:
    $sql = "SELECT DISTINCT category FROM script ORDER BY category ASC";
    $result = mysql_query($sql) or die(mysql_error().': '.$sql);
     
    while($row = mysql_fetch_assoc($result)) {
         $links[] =  "<a href='view.php?category={$row['category']}'>{$row['category']}</a>";
    }
     
    echo implode(' | ', $links);

    You could keep the links in a string variable and then rtrim at the end, but i figured keeping a string variable in memory is about the same as an array.

  16. #16
    SitePoint Guru ripcurlksm's Avatar
    Join Date
    Aug 2004
    Location
    San Clemente, CA
    Posts
    859
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    :: you rule ::


Bookmarks

Posting Permissions

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