简体   繁体   中英

PHP While loop adding blank row to repeating region

I have what is, to me, a confusing situation. The following code is producing a blank row on the web page. I'm using the same code on different pages and they are working fine. The row count is set to display 5 rows but is showing 6 with the first one being devoid of any dynamic data. I'm sure I've missed or overlooked something, can anyone shed any light on why this might be happening? Cheers

    <?php
$counter = 0;
do {
$color = ($counter & 1)? "#FFF" : "#DEDEDE";
$counter++
?>
      <tr style="background: <?php print $color; ?>">
        <td ><a href="edit_workorder.php?jobID=<?php echo $row['jobID']; ?>" ><?php echo $row['wo_date']; ?></a></td>
        <td />
        <td ><?php echo $row['wcust_firstname']; ?> <?php echo $row['wcust_surname']; ?></td>
        <td ><?php echo $row['wcust_address']; ?> - <?php echo $row['wcust_suburb']; ?></td>
        <td ><?php echo $row['wo_equip']; ?></td>
        <td />
        <td ><?php echo $row['wo_problem']; ?></td>
        <td />
        <td ><?php echo $row['wtech_userlogin']; ?></td>
      </tr>
      <?php
} while($row = mysql_fetch_array($sql2))
 // close while loop
?>

$row is not populated until you go through the loop the second time.

instead of

do{
 ...
}while($row = mysql_fetch_array($sql2))

do:

while($row = mysql_fetch_array($sql2)){
...
}

The problem is that with a do-while the expression in the while-clause is not executed in the first run. The consequence of this is that the $row array is not set and will thus not contain any values to print.

The code should be modified to this:

while($row = mysql_fetch_array($sql2)){

}

Here's an example to illustrate how do-while works:

$i = 0;
do {
   echo $i;
}while($i++ < 3);

Outputs:

0
1
2
3

Whereas a standard while loop would output:

1
2
3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM