E
E
Egor Ogurtsov2015-11-09 18:09:05
PHP
Egor Ogurtsov, 2015-11-09 18:09:05

How to assign classes to loop elements?

I have a list that looks like this:

<?php if(count($items)): ?>
<ul>
  <?php foreach ($items as $key=>$item):	?>
  <li class="someclass myclass">some content</li>
  <?php endforeach; ?>
</ul>
<?php endif; ?>

It is certainly known that there will be no more than 3-5 elements in the cycle.
How can classes be hardcoded in this case, for example, assign "class-one" instead of "myclass" to the first element, "class-red" to the second, and class-rounded to the third?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2015-11-09
@mrdubz

$class = array('class-one', 'class-red', 'class-rounded');


<li class="someclass <?php echo $class[$key]; ?>">some content</li>

G
GreatRash, 2015-11-09
@GreatRash

:nth-child

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question