Ive managed to loop through a table and get the difference in days between 2 dates adjacent to each other in the table.
Multiple entries have the same date, i have it now that when that date changes, it displays an image however i want it to display the image as many times as the difference in date
$sql = mysql_query("SELECT * FROM Films_Info")
or die(mysql_error());
$last_value = null;
while ($row = mysql_fetch_assoc($sql)) {
if (!is_null($last_value)) {
$a = new DateTime($row['FilmRelease']);
echo "<p>".$row['FilmName']."</p>";
$interval = $a->diff(new DateTime($last_value));
//echo $interval->format('%d days');
$i = 0;
}
$howManydays = $interval->days;
for ( $i; $howManydays; $i++) {
echo "<img src=\"day.jpg\" />";
$howManydays = 0;
}
$last_value = $row['FilmRelease'];
}
for ( $i = 0; $i < $howManydays; $i++)The second is a conditional statement telling when the loop should stop.The first section in the for loop where it says
$i = 0initialized the variable$ito 0 and then tests the condition$i < $howManydays.Let’s say
$howManydaysequals 1. That means 0 < 1, so the loop will perform.At the end of the loop, the third section is called (
$i++), so$iis incremented and now equals 1. The second section is called again to test conditions$i < $howManydayswhich is asking if 1<1 which it’s not, so the loop will exit.So if
$howManydaysis greater than 0, the loop should happen the integer amount that is in$howManydays.You will want to remove
$howManydays = 0;within the for loop, if you don’t want it to only fire once.The for loop
is somewhat equivalent to the while loop:
http://php.net/manual/en/control-structures.for.php for more information
In your code above, you should also check if interval was set. You should probably just do an if rather than a while, if only need one interval.