how to change the class of an element according to the position of values within a foreach?

Asked

Viewed 101 times

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.

2 answers

2


If the idea is only to define the class of the element according to the position of the values in the array, just increase the value of $list, instead of assigning the doir value. Something like:

<?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 endforeach; ?>

If observed, the class of the element is class="lv<?= $list++; ?>", in which $list++ returns the current value of $list and increments it right after.

But the best solution, in my view, is to use the array:

<?php foreach($sub_cat as $i => $listar_cat): ?>

<li class="lv<?= $i; ?>"><a href="<?= BASE; ?>categorias/audio/<?= $listar_cat->sub_categoria; ?>"><?= $listar_cat->sub_categoria; ?></a></li>';

<?php endforeach; ?>

See that in the foreach there is the instruction $i => $listar_cat, in which $i shall be the numerical index of that value in array. It will be worth 0 for the first value, 1 for the second and so on. If you want the class to start at 1, just do class="lv<?= $i+1; ?>"

  • Brigado I managed to resolve using the index as you mentioned

1

Simply incrementing the variable $list before the end of the loop puts $list++;

  • if I do so will show this way will look like this <li class="lv1"></li><li class="lv2"></li><li><li class="lv3"></li><li class="lv4"></li><li class="lv5"></li

  • If ($list==3 || $list==6){ a sua class}Else {outra class}

  • @diogoDsa, you did not specify this in the question. Edit it and write EXACTLY your problem, please.

  • You’re right Anderson Diogo had not specified that nuance in the question.

  • I’ve already edited the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.