-1

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

4
  • 1
    You are resetting it to 0 in each loop.... ( $i = 0;) Commented Apr 9, 2016 at 15:10
  • Delete $i = 0; in the while loop. Just above while ($row...), put $i = 0; Try another option. Instead of incrementing and then finding $i % 2, just do $i = ($i === 0) ? 1 : 0; That way, each time you go in the loop, $i will change. Commented Apr 9, 2016 at 15:12
  • "Ive even tried declaring $i outside of the while loop and still nothing..... any help would be much appreciated" sorry guys i tried it outside the while loop and still nothing.... Commented Apr 9, 2016 at 15:17
  • try also to print the condition in your while loop it might help you... Commented Apr 9, 2016 at 15:52

5 Answers 5

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++;        
        }
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Ive even tried declaring $i outside of the while loop and still nothing..... any help would be much appreciated" sorry guys i tried it outside the while loop and still nothing....
try to print $row to see if there is actual result. I would print $i, to know if this loop iterates over a data. Try to put print_r($row); inside the loop
0
$i  = 0;
while($i<5) {
    $alternateVal   = $i%2==0 ? "type1" : "type2";
    echo $alternateVal;
    $i++;
}

Above is an example to overview alternate values.

Comments

0

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

Comments

0

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.