Determinig the last iteration of a while() loop
Results 1 to 6 of 6

Thread: Determinig the last iteration of a while() loop

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    17

    Determinig the last iteration of a while() loop

    Hello all,

    Is there an easy way to determine the last iteration of a while () loop? My suspicion is that there is no way until the while concludes. I was wondering because I'm using a very simple while() to create a list for use in my navigation, I need the last element in that list to have a class of "last" so that my css knows not to put a separator bar when it displays it in a horizontal list.

    The code I have is this:
    PHP Code:
            $content .= <<<EOF
                <ul>
    EOF;
            
    $result1 = @mysql_query("SELECT * FROM `navigation` WHERE level = '1'");
            while (
    $row mysql_fetch_array($result1MYSQL_ASSOC)) {
                
    $parent_id $row['id'];
                
    $parent_text $row['text'];
                
    $parent_link $row['link'];
                
    $parent_type $row['type'];
                
    $content .= <<< EOF
                    <li><a href=$page$parent_link>$parent_text</a></li>
    EOF;
            }
            
    $content .= <<<EOF
                </ul>
    EOF; 
    Which I'd like to return:
    HTML Code:
    <ul>
      <li><a href=/index.php>Home</a></li>
      <li><a href=/members.php>Members</a></li>
      <li><a href=/products.php>Merchandise</a></li>
      <li><a href=/downloads.php>Downloads</a></li>
      <li class="last"><a href=/photo/login.php>Member Login</a></li>
    </ul>
    Any thoughts?

    Any and all advice / insight will be greatly appreciated. Thanks in advance!

    ~Jordan

  2. #2
    I see what you did there
    Join Date
    Sep 2004
    Location
    Albany, NY
    Posts
    1,889
    just out of curiosity, why are you using heredoc statements for such short snippets of html?
    ***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

    Dev Server: Apache 2.4.x | PHP 5.4.x | MySQL 5.x

    PHP Manual | Learn HTML | Validate HTML

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    17
    The code that is posted is a short snippet of a bigger body of code. There would be more html proceeding and following the <ul> and </ul>. Otherwise you'd be right in that it's foolish to use heredocs.

    Good observation.

    ~Jordan

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Personally I almost never use a while() loop to iterate through a database result set. I first get the $number of rows and then use a for($i=0; $i<$number; $i++) loop. Then at every iteration I know exactly how many records there are, how many I've processed, which one I'm processing now, and how many there are left to do.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Super Geek bretticus's Avatar
    Join Date
    Apr 2002
    Location
    UT USA
    Posts
    1,576
    Of course, you can always increment a counter variable:

    PHP Code:
    <?php
    $num_rows 
    mysql_num_rows($result1);
    $i 0;
    while (
    $row mysql_fetch_array($result1MYSQL_ASSOC)) { 
        
    $i++;
        
    /*
        your code
         */
        
    if ( $i == ( $num_rows ) )
            
    //you're on last line...    
    }
    ?>
    Personally, I like to build arrays from my database results first. That way it's easy to do a for or foreach loop and I can use the database results more than once w/o an expensive additional call to the database.
    When in doubt, read the manual: PHP MySQL
    For beginners: PHP 101 PHP + MySQL 101 Google
    Forum etiquette.: See FAQs (Typically found in sticky posts: top posts)
    Avoid asking questions like: "It's not working..." (Describe how it's not working!) "Do you know of a script..." (This is a forum for PHP developers.)

  6. #6
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Quote Originally Posted by bretticus
    Of course, you can always increment a counter variable:
    Although of course that's not far off from a for() loop anyway. I usually build an array first and then use it as well: collecting the results and using them are two separate tasks, and end up being handled by two separate chunks of code.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

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
  •