PHP计数器在while循环中递增 [英] PHP counter increment in a while loop

查看:68
本文介绍了PHP计数器在while循环中递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个while循环中增加一个计数器时遇到问题,基本上我只想在数据库中获取的两个图像链接之间交替,但是我的计数器不会增加,而且我不确定为什么有人可以帮助吗?

Im having a problem incrementing a counter in one of my while loops basically i just want to alternate between two image links that were fetched in my database but my counter wont increase and im not sure why can anyone help?

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $img_link = $row['Image'];
      $img_link_alt = $row['Image_alt'];
      $i = 0;

       echo '<div class="col-xs-6 col-sm-3 placeholder">'; 
       $img = ( $i % 2 == 0 ) ?  $img_link : $img_link_alt; 

            echo $i;
            //'?' . date("h:i:sa").'
            echo '<img style="height:200px; border-radius:0%; width:300px;" src="screenshots/'. $img . '">';          
            echo '<h4>Screenshot</h4><span class="text-muted">Updated Screenshot of the Botting session: <b>' . $row['script_name'] .' </b></span>'; 
            echo '</div>';        
            $i++;        
        }

我什至尝试在while循环之外声明$ i,但仍然一无所获.....任何帮助将不胜感激

Ive even tried declaring $i outside of the while loop and still nothing..... any help would be much appreciated

推荐答案

在循环外初始化$ i.

Initialize $i outside the loop.

$i = 0;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
      $img_link = $row['Image'];
      $img_link_alt = $row['Image_alt'];

       echo '<div class="col-xs-6 col-sm-3 placeholder">'; 
       $img = ( $i % 2 == 0 ) ?  $img_link : $img_link_alt; 

            echo $i;
            //'?' . date("h:i:sa").'
            echo '<img style="height:200px; border-radius:0%; width:300px;" src="screenshots/'. $img . '">';          
            echo '<h4>Screenshot</h4><span class="text-muted">Updated Screenshot of the Botting session: <b>' . $row['script_name'] .' </b></span>'; 
            echo '</div>';        
            $i++;        
        }

这篇关于PHP计数器在while循环中递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆