user3219940
user3219940

Reputation: 1

foreach $row within a while loop

I am trying to create a calendar:

while($row = mysql_fetch_assoc($result)){
   while ( $day_num <= $days_in_month ){ 

  //echo "<td style='background-color:red;' onclick='testing(this)'> $day_num </td>"; 

   echo "<td  onclick='testing(this)'> $day_num </td>";
   $day_num++; 
   $day_count++;
   if ($day_count > 7){
      echo "</tr><tr>";
      $day_count = 1;
   }
 }
}

The code is working fine, the issue I am having is I want to change the background of days that are in the database. As I see it, I need to put a foreach in there to check each

$row['day']

check

if($day_num == $row['day']) {

and put my commented out echo in there, but no matter where I place the foreach it breaks.

Can anyone tell me how to best go about this?

Upvotes: 0

Views: 84

Answers (1)

Patrick Moore
Patrick Moore

Reputation: 13354

This should work:

echo "<td " . ( $day_num == $row['day'] ? 'style="background-color:red;" ' : ' ' ) . "onclick='testing(this)'> $day_num </td>";

It checks whether $day_num is equal to $row['day'] and outputs the additional styling if so, and just a space if not.

Upvotes: 2

Related Questions