Loop php in an HTML class

Asked

Viewed 144 times

0

Let’s assume that I have a div inside a foreach and inside that div I have another div with an x class, but I would like that x class to be numbered with each repetition, there is this possibility?

Code example:

<?php
                    foreach ($list_menu as $key => $menu):
                    ?>
                            <?php
                            if(count($menu->subMenu)):
                            ?>
                                <li class="dropdown">
                                    <a href="<?= base_url('categoria/' . $menu->txtUrlMenuSite); ?>"><?= strtolower($menu->txtMenu); ?></a>
                                    <div class="dropdown-content">
                                        <div class="col-xs-12">
                                            <ul class="sub-nav">
                                                <?php
                                                foreach ($menu->subMenu as $key => $sub):
                                                ?>
                                                    <li><a href="<?= base_url('categoria/' . $sub->txtUrlMenuSite); ?>"><?= $sub->txtMenu; ?></a></li>
                                                <?php
                                                endforeach;
                                                ?>
                                            </ul>
                                        </div>
                                    </div>
                                </li>
                            <?php
                            else:
                            ?>
                                <li><a href="<?= base_url('categoria/' . $menu->txtUrlMenuSite); ?>"><?= strtolower($menu->txtMenu); ?></a></li>
                            <?php
                            endif;
                            ?>
                        </li>
                    <?php
                    endforeach;
                    ?>
  • You can put a piece of code to make the question clearer, friend?

  • 1

    puts a Count $Count = 0; foreach(sue foreachaqui) ģ echo "<div class='x$Count'></div>"; $Count++;}

  • Woton Sampaio, sorry friend I just edited the post, thank you very much!

  • 1

    Marcos Brinner, thank you very much, I’ll try now!

1 answer

1


Gustavo,

you can do:

$a = "";
foreach ($arr as $key => $value)
   $a .= "<div><div class='x_".$key."'></div></div>";

 echo $a;

or tbm put a counter:

$count = 0;
$a = "";
foreach ($arr as $value)
    $a .= "<div><div class='x_".$count++."'></div></div>";

echo $a;
  • 1

    Thank you Gabriel Carvalho, with your answer and that of other friends I managed to solve the problem!!

  • @Gustavosiqueira, avoid thanking in the comments. The thanks is already an Upvote, and confirmation of the answer. :]

Browser other questions tagged

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