John Davidson

php - ternary operator not working for multiple code

0 comments
Message:


So I am trying to color code if a value is either AWOL, Active, or On Leave
I have been looking into ternary since my code is inside an echo already, since the first part checks if there are any users in that slot.
Here is the code that works:


<strong> Status: '. ( ($co['status'] = 'AWOL') ? '<j style="color:#CC000A">'. $co['status'] : $co['status']) .'</strong>

When I try to add another ? : part is where I run into issues. So something like:


<strong> Status: '. ( ($co['status'] == 'AWOL') ? '<j style="color:#CC000A">'. $co['status'] : ($co['status'] == 'Active') ? '<j style="color:#00A808">'. $co['status'] : $co['status']) .'</strong>

I end up with errors or everything is green (second color (#00A808)). Also been reading the other not error but problem I am getting, and it says Nested ternary expressions (without parentheses) looking that up doesn't really show me or lead me to what I am doing wrong?



My Answer:
echo ($users_in_slot > 0) ? ($status == 'AWOL' ? 'AWOL' : ($status == 'Active' ? 'Active' : 'On Leave')) : 'No users in this slot';

Rate this post

4 of 5 based on 6905 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us