1

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>
Noobster
  • 93
  • 11
  • 3
    **WARNING**: If you're just learning PHP, please, do not use the [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) explains best practices. Your user parameters are **not** [properly escaped](http://bobby-tables.com/php) and there are [SQL injection bugs](http://bobby-tables.com/) that can be exploited. – tadman Jun 20 '16 at 18:21
  • Im having the error of "Unexpected else" – Noobster Jun 20 '16 at 18:26
  • @tadman thank you for the advice – Noobster Jun 20 '16 at 18:26
  • Where'd you put it? I've added an answer below that should show how it should look. – chris85 Jun 20 '16 at 18:26
  • I've missplace a semicolon from the first try i did it. THanks – Noobster Jun 20 '16 at 18:30

1 Answers1

1

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.

chris85
  • 23,846
  • 7
  • 34
  • 51