0

I have 4 data points that can exist for a particular item. Existing is defined as having a value of "Y." Here are the 4 things:

INST_UNIV
INST_4YEAR
INST_2YEAR
INST_VOTECH

What I'd like to do is loop through them and if each has a value of "Y," display the associated blurb of text. Finally, if the number is greater than 1, I'd like them comma separated with "or" before the last item. Here's the mess of code I've been playing with. It works, but because the array is hardcoded, even if one of the variables returns an empty value, the comma still displays. Any help is appreciated.

<li>
Must attend: 
  @php
    $univ = ( $instance->applicationCharacteristics->first()->INST_UNIV == 'Y')? "a university" : "";
    $fourYear = ( $instance->applicationCharacteristics->first()->INST_4YEAR == 'Y')? "a four-year college" : "";
    $twoYear = ( $instance->applicationCharacteristics->first()->INST_2YEAR == 'Y')? " two-year college" : "";
    $voTech = ( $instance->applicationCharacteristics->first()->INST_VOTECH == 'Y')? "a vocational-tech school" : "";
    $items = array("$univ", "$fourYear", "$twoYear", "$voTech");
    $items[count($items) - 1] = "or " . $items[count($items) - 1];
    $items_string = implode(", ", $items );
    echo $items_string;
  @endphp
</li>

3 Answers 3

1

Try something like this instead. It only pushes values into the $items array if they need to be in there. Then it implodes the first part of the array with comma separators and adds " or " before the last element.

$items = array();
if ( $instance->applicationCharacteristics->first()->INST_UNIV == 'Y') $items[] = "a university";
if ( $instance->applicationCharacteristics->first()->INST_4YEAR == 'Y') $items[] = "a four-year college";
if ( $instance->applicationCharacteristics->first()->INST_2YEAR == 'Y') $items[] = "two-year college";
if ( $instance->applicationCharacteristics->first()->INST_VOTECH == 'Y') $items[] = "a vocational-tech school";
if (count($items) > 1)
    $items_string = implode(', ', array_slice($items, 0, -1)) . " or " . $items[count($items)-1];
else
    $items_string = $items[0];
echo $items_string;  
Sign up to request clarification or add additional context in comments.

Comments

0

You could process the arrays a little and combine it using conditional statement :

$univ = ( $instance->applicationCharacteristics->first()->INST_UNIV == 'Y')? "a university" : "";
$fourYear = ( $instance->applicationCharacteristics->first()->INST_4YEAR == 'Y')? "a four-year college" : "";
$twoYear = ( $instance->applicationCharacteristics->first()->INST_2YEAR == 'Y')? " two-year college" : "";
$voTech = ( $instance->applicationCharacteristics->first()->INST_VOTECH == 'Y')? "a vocational-tech school" : "";
$items = array("$univ", "$fourYear", "$twoYear", "$voTech");
$items = array_filter($items); // remove empty arrays
$or = count($items) > 1 ? ' or ' : ''; // only add 'or' if items is more than one
$last_item = array_pop($items); // get the last items
$items_string = implode(", ", $items ) . $or . $last_item;
echo $items_string;

Comments

0

Just use the substr function to remove the last ", " removing the two last chars:

$items_string = substr(implode(", ", $items ),0,-2);

1 Comment

Thanks for your response. This still shows the comma if one of the four variables is empty. It's just trimming the last 2 characters of the entire string now. So if $fouryear, for example, is empty, I get "Must attend a university, , two-year college, or a vocational-tech scho"

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.