-2

im trying to add more ternary operator inside an echo but i dont know how. below is my current codes

   echo "<td onclick='window.location = 'edit_visa.php?id=$visa_id'><input type='text' class='form-control' size='8' value='$visa_status' ". (($date_today >= $expired) ? "style='background-color : #e60000; font-weight: bold; color: white;'" : "") (($date_today != $expired) ? "style=\"background-color : #e60000; font-weight: bold; color: white;\"" : ""). "></td>";

this is what i tried but no luck

adrian pascua
  • 3
  • 1
  • 1
  • 5
  • 4
    Looks a lot like your ghost question account http://stackoverflow.com/users/7012338/adrian-pascua I see http://stackoverflow.com/q/40015708/1415724 - I thought this code looked familiar. – Funk Forty Niner Oct 17 '16 at 01:49
  • you have chosen to do it the most difficult way. –  Oct 17 '16 at 01:52
  • 1
    PHP's ternary binds backwards, making it act non-intuitively. The [documentation even recommends against doing it](https://secure.php.net/manual/en/language.operators.comparison.php): "It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious". While you _can_ do this with the right parentheses, I strongly urge you to find a different solution. – ChrisGPT was on strike Oct 17 '16 at 02:22

1 Answers1

0

Don't make your code too complicated: Try this:

$style = '';
if($date_today >= $expired)
{

   $style= "style='background-color : #e60000; font-weight: bold; color: white;'";

}
else if($date_today != $expired)
{
   $style= "style='background-color : #e60000; font-weight: bold; color: white;'";
}
echo "<td onclick='window.location = 'edit_visa.php?id=$visa_id'><input type='text' class='form-control' size='8' value='$visa_status' ". $style ."></td>";

I did not find any difference in your css for both the condition. Please review.

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14