0
I have a menu where I list the categories. I use the foreach
to list and everything is working right. 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 of foreach
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 the foreach
list the 2, it changes the class of lv1
for lv2
, But how do I change, for example, the 4 foreach
or the 6 and so on? This way I can only change the 1 item.
Brigado I managed to resolve using the index as you mentioned
– diogo Dsa