0

I use Kirby CMS as backend. I want following structure for my html output:

<ul>

    <li class="link-1"><a href="/#">Link</a></li>

    <li class="link-2"><a href="/#">Link</a></li>

    <li class="link-3"><a href="/#">Link</a></li>

    <li class="link-4"><a href="/#">Link</a></li>

</ul>

I have following code:

<?php foreach($pages->visible() AS $p): ?>

<?php $nbr = $pages->countVisible()?>


<li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">
    <a<?php echo ($p->isOpen()) ? ' class="active"' : '' ?> href="<?php echo $p->url() ?>"><?php echo html($p->title()) ?></a></li>
<?php endforeach ?>

But instead I only get the css class

link-1234

in each of the links, so it is making the for loop, but I need only one number per foreach loop.


This code made it work:

<li class="link-<?php static $x=1; echo $x; $x++; ?>">

1 Answer 1

3
 <li class="link-<?php for ($i = 1; $i <= $nbr; $i++){echo $i;} ?>">

only loops inside that element

 <?php for ($i = 1; $i <= $nbr; $i++){ 
echo "<li class=\"link-$i\">";
echo 'the rest of the line';
 } ?> 

should loop the whole block

Sign up to request clarification or add additional context in comments.

3 Comments

Please look at my edit; why it says Undefined variable: i? And when I first define $i with 0 or 1 it won't increase the number in the foreach loop.
@projektalex The code in the question edit looks like a butchered mess trying to be a for loop. Start with user1281385's loop, then modify loop body as desired.
I made it with this line of code: <li class="link-<?php static $x=1; echo $x; $x++; ?>">. It is everything what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.