【问题标题】:how can we handle while loop iteration?我们如何处理while循环迭代?
【发布时间】:2015-08-01 13:28:16
【问题描述】:

我的代码块运行良好,但问题是......

  • 在一个循环中它会打印 'one' qoute。
  • 在第二个“一”和“二”中
  • 在第三所有三个

...以此类推,直到循环执行。

我想在第一次只打印一个,在第二次打印第二个 qoute,依此类推,直到循环执行。 这是我的代码块

<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>

【问题讨论】:

    标签: php mysqli while-loop


    【解决方案1】:

    您在这里所做的是将字符串附加到 $output 变量,以便在每次迭代时保持附加。 在执行新的迭代之前,您的 $output 变量必须为空。

    这就是你得到这样的输出的原因。

    因此,为避免这种情况,您必须在循环内重新初始化 $output 变量。 检查以下代码:

    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++; 
    
            }
    

    【讨论】:

      猜你喜欢
      • 2021-09-06
      • 2021-02-18
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 1970-01-01
      相关资源
      最近更新 更多