I have a menu where I sort the categories. I use foreach
to list and everything is working fine. My code is this:
<?php
$consulta_sub_cat = $pdo->query("SELECT * FROM ws_categoriasp");
$sub_cat = $consulta_sub_cat->fetchAll(PDO::FETCH_OBJ);
//$sub_cat = str_replace("-"," ",$lista_sub_cat);
?>
<?php
foreach ($sub_cat as $listar_cat) {
?>
<li class="lv1"><a href="<?= BASE; ?>categorias/audio/<?= $listar_cat->sub_categoria; ?>"><?= $listar_cat->sub_categoria; ?></a></li>
<?php
}
?>
For me to change the 1 class of the 1% item from foreach
I did so
<?php
$consulta_sub_cat = $pdo->query("SELECT * FROM ws_categoriasp");
$sub_cat = $consulta_sub_cat->fetchAll(PDO::FETCH_OBJ);
//$sub_cat = str_replace("-"," ",$lista_sub_cat);
?>
<?php
$list = 1;
foreach ($sub_cat as $listar_cat) {?>
<li class="lv<?= $list; ?>"><a href="<?= BASE; ?>categorias/audio/<?= $listar_cat->sub_categoria; ?>"><?= $listar_cat->sub_categoria; ?></a></li>
<?php $list = 2;
}
?>
So, when foreach
lists 2, it changes the class from lv1
to lv2
, but how do I change, for example, the 4th item of foreach
or 6 and so on ? That way I can only change the 1 item.