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!

Hi there - sure, you can add an active class, it just depends on how the active state is determined.

You can add something like this:

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

I'm not sure what your check should be, however. 

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

Claire Ryan
interactivetools.com

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

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/