2

I have an unordered list that's being generated from a database. Depending on the value of one of the returned fields, the li tag's class should be set to different values. Unfortunately they're all returning "inactive". I know this must be something simple, but I've been staring at it for hours and am just not seeing it.

Here's the code:

<ul class="tabs">
   <? foreach ($tracks AS $track) {
    $active = (strtolower($track->shortname === 'dpp')) ? ' class="active"' : 'class="inactive"';
    echo "<p>".strtolower($track->shortname). " is ".$active."</p>"; ?>
    <li <?= $active; ?>><a href="#<?= strtolower($track->shortname); ?>"><?= $track->name; ?></a></li>
   <? } ?>
</ul>

Here's what is actually getting printed:

<ul class="tabs"> 
    <p>dpp is class="inactive"</p>  <li class="inactive"><a href="#dpp">Digital Path to Purchase</a></li> 
    <p>cre is class="inactive"</p>  <li class="inactive"><a href="#cre">Fueling Creativity</a></li> 
</ul> 

It's obvious that the first one is returning $track-shortname of dpp, so why is the $active variable not getting set to "class=active"?

1
  • 1
    Watch your brackets: strtolower($track->shortname === 'dpp') => strtolower($track->shortname) === 'dpp' Commented Jun 29, 2011 at 21:41

3 Answers 3

2

You've made an error in the expression where you check the shortname:

$active = (strtolower($track->shortname === 'dpp')) ? ' class="active"' : 'class="inactive"';
                     ^                           ^

You most certainly only want to put it around $track->shortname:

$active = (strtolower($track->shortname) === 'dpp') ? ' class="active"' : 'class="inactive"';
                     ^                 ^

Otherwise you tried to lowercase a boolean value which is either true or false but in your case it looks to always be false, hence the inactive CSS class.

BTW, you can spare another pair:

$active = strtolower($track->shortname) === 'dpp' ? ' class="active"' : 'class="inactive"';
         ^                                       ^
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you; I did some copy/paste work and misplaced the closing parentheses. I knew it would be something like that!\
1

The code (strtolower($track->shortname === 'dpp')) looks wrong. Your putting the strtolower around the comparison, and you probably just want it around the $track->shortname.

Comments

1

One of your close parentheses is in the wrong place.

Change:

$active = (strtolower($track->shortname === 'dpp')) ? ' class="active"' : 'class="inactive"';

To:

$active = (strtolower($track->shortname) === 'dpp') ? ' class="active"' : 'class="inactive"';

With the parenthesis in the wrong place, $track->shortname is compared to dpp before any lower-case conversion. Then the result of that comparison (boolean) is passed to strtolower. That results in strtolower simply returning a string representation of that which results in the ternary comparison evaluating to false.

1 Comment

Thanks. Same answer as hakre below; just saw that one a minute sooner.

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.