dongyuan8469 2015-08-01 13:28
浏览 61
已采纳

我们如何处理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>
  • 写回答

1条回答 默认 最新

  • dre75230 2015-08-01 13:52
    关注

    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++; 
    
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?