13
$sql = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

<p>$i. <?php echo $row['comment'] ?></p>

<div class="border"></div>

$i++;
}

How could I do to not output <div class="border"></div> under the last comment?

6 Answers 6

33
$sql = mysql_query("SELECT * FROM comments WHERE user = 1");
$number = mysql_num_rows($sql);
$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

   echo '<p>' . $i . $row['comment'] . '</p>';

   if ($i < $number)
   {
       echo '<div class="border"></div>';
   }

   $i ++;
}

Using WebDevHobo's suggestion.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the $i. Just use if( --$number===0 ).
With PDO : $number = $req->rowCount(); while (...) { if (--$number != 0) { ... } }
8
$sql = mysql_query("SELECT * FROM comments WHERE user = 1");
$output = array ();
while ($row = mysql_fetch_assoc($sql)) {
  $output[] = $row['comment'];
}
echo join('<div class="border"></div>', $output);

Comments

4
$number = mysql_num_rows($sql);

This will tell you how many rows will be returned. Based on that, you can make sure that the last on does not have the DIV.

Comments

1
$sql = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
while ($row = mysql_fetch_assoc($sql)) {

$out_data[] = "<p>$i {$row['comment']} </p>";

$i++;
}

$divider = '<div class="border"></div>';
$output = implode ($divider, $out_data);

echo $output;

Comments

0
$rslt = mysql_query("SELECT * FROM comments WHERE user = 1");

$i = 1;
if ($row = mysql_fetch_assoc($rslt)) {
  echo '<p>'. $i . ' '. $row['comment'] . '</p>';
  $i++;
  while ($row = mysql_fetch_assoc($rslt)){
    echo '<div class="border"></div>';
    echo '<p>'. $i . ' ' . $row['comment'] . '</p>';
    $i++;
  } // end while
} // end if

Avoids needing to know the number of rows. Executes the if statement only once instead of each loop. The HTML and php were kind of messy and inconsistent, so I just assumed the whole block was within php tags. Obviously, open and close the php tag as you see fit.

This is largely a style issue, but I decided that the variable name $sql was a bit misleading, as it is often and commonly used to hold the string of the sql statement to be executed, I therefore changed the variable name to $rslt.

Comments

0

A general answer to this type of problem, and using Javascript because it's easy to throw in a console and play with:

var count = 0;
var foo = 3;
while( count < 3 ) {
    console.log("Header - " + count);
    console.log("Body - "+ count);
    console.log("Footer - " + count);
    count++;
}

This will print:

Header - 0
Body - 0
Footer - 0
Header - 1
Body - 1
Footer - 1
Header - 2
Body - 2
Footer - 2

The case being requested is basically saying "Print a footer on all but the last element."

If you consider the second loop iteration as simply a continuation of the first you can see how to do this without needing to find the total number of records - e.g. no need to do a second query for count. More succinctly: when you're stuck trying to figure out how to do something using a loop (or recursion) you should try actually writing out what your loop does, but without actually looping - e.g. copy and paste the loop block at least three times.

Rather than do that here, I'm just going to finish with the answer, and leave the derivation to the reader :~)

var count = 0;
var foo = 3;
while( count < 3 ) {
    if( count > 0 ) {
        console.log("Footer - " + (count - 1));
    }
    console.log("Header - " + count);
    console.log("Body - "+ count);
    count++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.