Mayank Bhardwaj Mayank Bhardwaj - 1 year ago 108
PHP Question

switching between html and php

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";}?>
<?php else{ ?>
<td style="background-color:#ffb3b3"><?php echo "No";}?>

the error I'm getting :

syntax error, unexpected 'else' (T_ELSE)

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download