Active class inside a for statement

4 posts by 2 authors in: Forums > CMS Builder
Last Post: October 1, 2014   (RSS)

By Jesus - September 29, 2014 - edited: September 29, 2014

Hi, I'm wondering if there's an option where I can get a class for an active link for a statement like this.

<?php foreach (getListOptions("preguntas_frecuentes", "categoria") as $value => $label):?>
    <li> <a data-toggle="tab" href="#tab-<?php echo $value;?>"> <?php echo $label;?> </a> </li>
<?php endforeach ?>

I need to have an active tab inside my for statement... that means just one of my categories will have the active class, like this:

<li class="active"> <a data-toggle="tab" href="#tab-<?php echo $value;?>"> <?php echo $label;?> </a> </li>

The final html code should look like this:

<li> <a data-toggle="tab" href="#tab-Generales"> Generales </a> </li>
<li class="active"> <a data-toggle="tab" href="#tab-Servicios"> Servicios </a> </li>
<li> <a data-toggle="tab" href="#tab-Nutrición"> Nutrición </a> </li>

Thanks!

By Jesus - September 30, 2014

Hi Claire thanks for your answer.

I think I'm close to understand this but....

A few things....

I might have unlimited categories and something active needs to be a variable as well, because the system will have just one option as active and all others regular.

So based on this, how can or how should I set the something active value in order to work with any of my categories?

Thanks!

By claire - October 1, 2014

I can't say without knowing how you want to determine the active category. In general, the easiest thing to do would be to make the first one active and ignore the rest.

Try something like this:

<?php $count = 0; ?>
<?php foreach (getListOptions("preguntas_frecuentes", "categoria") as $value => $label):?>
    <li<?php if($count == 0) : ?> class="active"<?php endif; ?>> <a data-toggle="tab" href="#tab-<?php echo $value;?>"> <?php echo $label;?> </a> </li>
<?php $count++; ?>
<?php endforeach ?> 

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/