php - Change text color based on switch value
Get the solution ↓↓↓Solution:
you can try using
switch ($row['destroydate']) {
case NULL:
$destroydate = '<span style="color:#FF0000">No Date Set</span>'; <---MAKE RED
//echo $destroyeddate;
break;
case "0000-00-00":
$destroydate = '<span style="color:#39CD0D">No Date Set</span>'; <--- MAKE GREEN
break;
default:
$destroydate = '<span style="color:#0808D8 ">'.$row["destroydate"].'</span>'; <--- MAKE BLUE
break;
}
Answer
Solution:
You should use this below. Create a function and use it with sending the Date data. The switch will return a result, always. This way you can always use your new function at any file. If there is anything needs to be added/changed, you don't need to change all the files who are using this switch, but only have to change the function. If you prefer: you could also make it a static function (because it's only returning some html).
/**
* @param $date
*
* @return string
*/
Public function getDateColor($date)
{
// Check on date and return result
switch($date):
case(empty($date)):
return '<span color="red">No Date Set</span>';
break;
case($date == 0000-00-00):
return '<span color="Green">No Date Set</span>';
break;
default:
return '<span color="blue">' . $date . '</span>';
endswitch;
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: invalid argument supplied for foreach() laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.