PHP counter increment in a while loop

你离开我真会死。 提交于 2021-02-05 12:19:17

问题


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

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


回答1:


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



回答2:


You have to reset $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++;        
}



回答3:


$i  = 0;
while($i<5) {
    $alternateVal   = $i%2==0 ? "type1" : "type2";
    echo $alternateVal;
    $i++;
}

Above is an example to overview alternate values.




回答4:


move the

$i 

initialization:

 $i=0

outside of the while loop and it worked. you every time renizialize the

$i 

variable in your while loop,and your counter don't increase, fix it. An alternative is use the rows counter, like that example:

$count = $db->query("SELECT COUNT(id) FROM users")->fetchColumn();

echo $count; //Returns number of rows

like in that answer




回答5:


Put the $i variable outside the while block, like this:

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


来源:https://stackoverflow.com/questions/36518614/php-counter-increment-in-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!