Björn C
Björn C

Reputation: 4008

why is for loop true on equal

My for loop is true when variables are equal.
Why is that when i'm only using "smaller than"?

I have a while loop that prints dates from mysql.
$today_nr is a day nr stored in db. ex: 02

Now i´d like a for loop to print out dates that is missing.
ex: 01, 02, 03. If first reccord in mysql is 04.

So i use this for loop. But it will echo both "01" from mysql($today_nr) and "1" from $i

PHP

$today_nr = $date->format('d');
$i = 1;

for(;$i < $today_nr; $i++){
  echo $i;
}

FULL SCRIPT

https://eval.in/367692
Line 532

The Print for loop is printing 1 after while loop printed 01.

01 Ons 04:41 13:10 0.50 7.97 -0.03 1.32
1
02 Tor 04:40 13:18 0.50 8.13 0.13 1.33
2
3
4
5
6
07 Tis 04:41 12:58 0.50 7.77 -0.23 1.32
7
08 Ons 04:43 13:08 0.50 7.92 -0.08 1.28

Upvotes: 0

Views: 96

Answers (2)

Bj&#246;rn C
Bj&#246;rn C

Reputation: 4008

I solved it like this:

PHP

//Loop out dates missing in db
//If $i is bigger than today nr
if($i > $today_nr){
   $i = $today_nr;
}

//Loop out missing days
for(;$today_nr != $i;){

  //Set data about day
  $iDate = $year."-".$selected_month."-".$i;
  $iDate = new DateTime($iDate);
  $iDay = $iDate->format('D');

  //Translate iDay. Eng - Swe
  include('../../../include/translate_days_iDay.php');

  //Echo out table
  echo "<tr>";
  echo "<td class='print_table_text'>".$iDate->format('W')."</td>";
  echo "<td class='print_table_text'>".$iDate->format('d')." ".$iDay."</td>";
  echo "</tr>";

  //Add to i
  $i++;
  //if i is bigger than a month
  if($i > $days_in_month){
    break;
  }
}
//If today and i is equal
if($today_nr == $i){
  $i++;
}

Upvotes: 0

MNR
MNR

Reputation: 757

It isn't like what you are saying in my case the code below works perfect with not an issue as you provided and It works as you expects:

<?php
$today_date = date('d', time());
// $today_nr = $date->format('d');
$i = 1;

for(;$i < $today_date; $i++){
  echo $i; // result: 12345678910111213141516171819
}

Upvotes: 1

Related Questions