Roger
Roger

Reputation: 238

Display Image on nth iteration php

I am developing an app where I want to show 1 image at a time every 5th tweet, I have 18 images in total. Once all images display I want to reset to image 1. Tweets saving is done, but problem is displaying image, Suppose I have 122 tweets on DB.

<?php 
$total=0;
$result = mysqli_query($con,"select count(1) FROM tweets");
$row = mysqli_fetch_array($result);
$total = $row[0];
if (($total + 1) % 5 == 0) {
  echo '<img src='img/cities/1.jpg' />';
}
?>

I am stuck on this logic.

Upvotes: 2

Views: 83

Answers (1)

davidgiga1993
davidgiga1993

Reputation: 2853

This code exaple shows an image if the row count is dividable by 5. The image index will be calculated by dividing the row count by 5 so you know which 5th group is shown. Now we take mod 18 and add 1 so we get a range from 1-18

$rowCount = $row[0];
$imageIndex = floor($rowCount / 5) % 18 +1;
if(($rowCount % 5) == 0)
{
  echo '<img src="img/cities/'.$imageIndex.'.jpg" />';       
}

Upvotes: 2

Related Questions