0

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?

5
  • 1
    To be honest you're better off avoiding ternaries and using regular if statements. Commented May 12, 2022 at 22:49
  • This code is hard to read I agree with @JacobMulquin Commented May 12, 2022 at 22:51
  • Yeah, was trying to avoid that since I am checking if there are any users in the database first to just show the header and having it blank if no one member was a part of a rank. I will prob revert to if statement. Commented May 12, 2022 at 22:53
  • Simple ternaries are OK, but nested ternaries are overly confusing. PHP 8.0 even requires that you use extra parentheses to make sure that the grouping is what you intend. It looks like you parenthesized it correctly, so I'm not sure why you're getting incorrect results. Commented May 12, 2022 at 22:54
  • Wrap the last tertiary in parenthesis: (('firstcheck' == $firstcheck)? 'firsttrue': (('secondcheck' == $secondcheck)? 'secondtrue' :'secondfalse')) Commented May 12, 2022 at 23:29

1 Answer 1

4

I would approach this using an array that maps Status => color. This gives you the ability to change the colours easily down the track. Even better would be to have CSS classes instead of inline styles.

<?php

$statuses = ['AWOL', 'Active', 'On Leave'];

$co = ['status' => $statuses[rand(0,count($statuses)-1)]];

$color_map = [
    'AWOL' => '#CC000A',
    'Active' => '#00A808',
    'On Leave' => '#000000'
];

echo '<strong> Status: <j style="color:'.$color_map[$co['status']].'">' . $co['status'] . '</j></strong>';
Sign up to request clarification or add additional context in comments.

3 Comments

This is WAY more readable than the nested ternaries
Is there any way to avoid the [rand]? Since that throws any color on either one of those even if they are active, it will put 'Active' as red or when I refresh will go yellow etc. @JacobMulquin
@Self_taught_codes The rand() is only there for demonstration purposes, I was assuming that in your implementation, you already have the $co variable set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.