0

I'm trying to achieve a red background color for a table cell if the query yields no as an answer and red background otherwise.I'm a beginner in php and dont know how to switch between html and php code.

<?php  if($row1['attended']==1){ ?>
     < td style="background-color:#99ff99"><?php echo "Yes";}?>
     </td>
 <?php  else{ ?>
    <td style="background-color:#ffb3b3"><?php echo "No";}?>
    </td>

the error I'm getting :

syntax error, unexpected 'else' (T_ELSE)

chris85
  • 23,846
  • 7
  • 34
  • 51

2 Answers2

2

You are basically there. One thing to know is that conditionals have alternative syntax that make it look cleaner when mixing your PHP and HTML.

<?php if ($row1['attended'] == 1) : ?>
    <td style="background-color:#99ff99">Yes</td>
<?php  else : ?>
    <td style="background-color:#ffb3b3">No</td>
<?php endif; ?>

In modern PHP development, it is good to rely on templating such as Twig or things built into frameworks like Laravel's Blade.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
1

You should

  <?php  
      if($row1['attended']==1){  
        echo ' <td style="background-color: #99ff99"> Yes </td>'
       }
       else{ 
        echo '<td style="background-color:#ffb3b3">No</td>';
       }
  ?>
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107