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:

        $content .= <<<EOF
            <ul>
EOF;
        $result1 = @mysql_query("SELECT * FROM `navigation` WHERE level = '1'");
        while ($row = mysql_fetch_array($result1, MYSQL_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:

<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

    just out of curiosity, why are you using heredoc statements for such short snippets of html?

      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

        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.

          Of course, you can always increment a counter variable:

          <?php
          $num_rows = mysql_num_rows($result1);
          $i = 0;
          while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)) { 
          	$i++;
          	/*
          	your code
          	 */
          	if ( $i == ( $num_rows - 1 ) )
          		//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.

            bretticus wrote:

            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.

              Write a Reply...