PHP counter increment in a while loop

前端 未结 5 1964
一生所求
一生所求 2021-01-29 08:33

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 wo

5条回答
  •  一个人的身影
    2021-01-29 09:24

    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 '
    '; $img = ( $i % 2 == 0 ) ? $img_link : $img_link_alt; echo $i; //'?' . date("h:i:sa").' echo ''; echo '

    Screenshot

    Updated Screenshot of the Botting session: ' . $row['script_name'] .' '; echo '
    '; $i++; }

提交回复
热议问题