matching dates in loop

Hi Guys!

I have a table with 2 rows.

1st row shows dates starting from today up to 20 days ahead. Each date in separate cell of the row. That works.

2nd row should take the date from the mysql database and check if it corresponds with the date in the cell above (from the first row of the table). If the dates match then echo ‘option 1’, if there is no match then echo ‘option 2’. But the echoing should take the place exactly under the matching date from the first row above.

I have tried various options but so far without success…

P.S. Connection with mysql database works fine and I can get the ‘date’ from it. In mysql table the date is formatted as DATE format: YYYY-MM-DD

Any suggestions?

Thanks, Raivis
[php]
// *** first row of the table ***
echo ‘

’;
date_default_timezone_set('UTC');					// Set timezone
$date = date('Y-m-d');								// Start date - to be displayed in the first table cell
$end_date = date('Y-m-d', strtotime('+20 days'));	// End date - to be displayed in the last table cell

while (strtotime($date) <= strtotime($end_date)) {			
	$table_date = date('d',strtotime($date)) ;
	echo '<td bgcolor="#999999" style="padding:2px;" >';
	echo $table_date;
	echo "</td>";
	$date = date ('Y-m-d', strtotime('+1 day', strtotime($date)));
}
	echo "</tr><tr>";

// *** second row of the table ***

include_once “…/scripts/connect_to_mysql.php”;
$sqlCommand = “SELECT date FROM janis ORDER BY date ASC LIMIT 21”;
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error($myConnection));

while($row = mysqli_fetch_array ($query)){
	$datematch = $row['date'];
	echo '<td bgcolor="#999999" style="padding:2px;" >';
		if($datematch==$table_date){	// this was one of my versions to put the IF statement in - to check if date from first row matches the date from database
		echo 'option 1';
		} else {
			echo 'option 2';
			}
		}				
	echo "</td>";

echo “

”;
mysqli_free_result($query); [/php]

You are setting the variable $table_date to have a format of ‘d’ which is just the day (i.e a number between 01 and 31). Are the dates in the database the same format?

Secondly, you are setting the $table_date variable inside the while loop. So when you get to the database query this variable is set to the value of the last iteration of that loop. So your second while loop is always just using that last one value.

Sponsor our Newsletter | Privacy Policy | Terms of Service