Noobster Noobster - 5 months ago 12
HTML Question

php,mysql,html,css - Hide specific text when it has a database data

Halo I have here my table

sched_id sched_time sched_day emp_id
1 7:30-9:00 Monday 1


Primary key, varchar(20), varchar(20) and a foreign key respectively.

I want to show the text green when that specific data in the database is inserted and just a normal plain color when it is not yet inserted. Here is my work so far

<?php
$sel_admin = " SELECT * from ehr_schedule where emp_id=".$_SESSION['emp_id']." ";
$rs_admin = mysql_query($sel_admin);
if($row = mysql_fetch_array($rs_admin))
{
?>
<td><?php if ($row['sched_stime']=="7:30-9:00")
{ ?>
<span style="color:green">7:30-9:00</span> //I have already the specific requirement and data in my database, and this display nicely
<?php } ?>
7:30-9:00 //Although, I want this to be hidden when the requirement above is meeted
<?php } ?> </td>

Answer

You need to use and else for the inverse occurrence of an if. So:

<?php if ($row['sched_stime']=="7:30-9:00") 
          { ?>
    <span style="color:green">7:30-9:00</span> //I have already the specific requirement and data in my database, and this display nicely
    <?php }  else {?>
        7:30-9:00
    <?php } ?>

You can read more about else here, http://php.net/manual/en/control-structures.else.php.

So it pretty much is:

if (..) {
    condition was met, this block executes;
} else {
    condition was not met, this block executes;
}

or you can use else if, if you want to check multiple things.

Comments