在if语句中只回显一次文本

时间:2017-01-14 18:12:11

标签: javascript php jquery html css

我试图回应赢得特定奖牌的几周。我从数据库中获取数据,结果存储在“$ toprow55”中。

以下是我用来回显查询结果的代码: -

<div class="modal-body" style="white-space: pre-line ;color:black;background:#ffc266; font-size:17px ">
                <?php while( $toprow55 = sqlsrv_fetch_array( $stmt55) ){
                 if($toprow55 !== NULL){?>
              <?php  echo "Gold medals won  : "."\n". "".$toprow55['WeekNumber']."" ."\n";
                }
              else {
                {
                  echo "No  Gold medals won";
                } }}?>

                </div>

问题:我收到的文字是:“金牌赢了:”结果多次被回应。我明白我需要在这里使用“sqlsrv_fetch_array($ stmt55)”。请告诉我如何显示“获得金牌:” onlyonce

pic显示当前情况:

img

3 个答案:

答案 0 :(得分:1)

在你的情况下最简单的方法是添加一个计数器并询问,如果计数器为零或其他。

<?php
$i = 0;
while( $toprow55 = sqlsrv_fetch_array( $stmt55) ){
    if ($i == 0) {
        echo echo "Gold medals won  : \n";
    }

    if($toprow55 !== NULL){
        echo $toprow55['WeekNumber'] . "\n";
    } else {
        echo "No  Gold medals won";
    }
    $i++;
}
?>

答案 1 :(得分:1)

它应该工作。你可以展示你的标签&#34;赢得金牌&#34;一次布尔设置为true,然后在if条件结束时为false。

&#13;
&#13;
<div class="modal-body" style="white-space: pre-line ;color:black;background:#ffc266; font-size:17px ">
<?php
  $i = true;
  while( $toprow55 = sqlsrv_fetch_array( $stmt55) ){
    if($toprow55 !== NULL){
       if($i) echo "Gold medals won  :\n";        
       echo $toprow55['WeekNumber']."\n";
       $i = false;
     }
  }
  if($i){
    echo "No  Gold medals won";
  } 
?>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

你需要在达到结果后打破while循环,否则只要$toprow55 = sqlsrv_fetch_array($stmt55)为真,就会重复无穷大。

<div class="modal-body" style="white-space: pre-line ;color:black;background:#ffc266; font-size:17px ">
  <?php
    while($toprow55 = sqlsrv_fetch_array($stmt55)){
      if($toprow55 !== NULL){
        echo "Gold medals won  : "."\n". "".$toprow55['WeekNumber']."" ."\n";
      } else {
        echo "No  Gold medals won";
      };
      break;
    };
  ?>
</div>

PHP While Documentation