我们如何处理while循环迭代?

I am having block of code which is working fine but the problem is that...

  • in one loop it print 'one' qoute.
  • in 2nd both 'one' and 'two'
  • in third all three

...so on till the loop execute.

I want to print only one in first time and second qoute in second time and so on till loop executes. here is my block of code

<div class="records round">
<?php
//show records
$query = mysqli_query($con,"SELECT table2.col2 AS a,table1.col2 AS b, table1.col1 AS c, table1.q_url AS d 
                            FROM {$statement} 
                            LIMIT {$startpoint} , {$limit}");
$output='';
$Authorname='';
$count=1;
while ($row = mysqli_fetch_array($query)) {
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
          $url=explode('/',$url);
    ?>
        <div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
         echo $output .=$row['b'].'</a>';?></div>
     <?php 
          $count++; 

        }
        ?>
</div>

What you are doing here appending string to $output variable so at each iteration its keeping appending. Your $output variable must be empty before performing new iteration.

Thats the reason you are getting such output.

So to avoid this you have to reinitialize $output variable inside the loop. Check below code:

while ($row = mysqli_fetch_array($query)) { 
$output='';
$Authorname =$row['a'];
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
          $url=explode('/',$url);
    ?>
        <div class="record round"><?php echo $count; $output .='<a href="http://localhost/quotes/'.$url[5].'/'.$row['d'].'.html">';
         echo $output .=$row['b'].'</a>';?></div>
     <?php 
          $count++; 

        }