John Davidson

kirby - add an incrementing number to id with php

0 comments
Message:


I am making a portfolio site with kirby cms.
my site is a single page website. I want to add an incrementing number after id by using foreach in the part shown in the code below..!
Is there easy way to do..?
regards,


<?php foreach ($pages->listed() as $p): ?>
<div class="slideshow">
<div class="slideshow-box">
<div class="slideshow-holder">
<?php foreach ($p->images() as $workimage): ?>
<div class="slide">
<?= $workimage ?><br><br>
</div>
<?php endforeach ?>
</div>
<div class="tooltip">
<p id="myText????<here>"><?= $p->worktitle() ?></p>
</div>
<div class="toolbox" id="myDIV????<here>">
<?= $p->description() ?>
</div>
</div>

<div class="actions">
<span>1/3</span>
</div>

<a href="#" class="prev">←</a>
<a href="#" class="next">→</a>
</div>
<?php endforeach ?>

<script>
var button????<here> = document.getElementById('myText????<here>');
var div????<here> = document.getElementById('myDIV????<here>');

div????<here>.style.display = 'none';

button????<here>.onclick = function () {
if (div????<here>.style.display !== 'none') {
div????<here>.style.display = 'none';
} else {
div????<here>.style.display = 'block';
}
};
</script>



My Answer:
Yes, you can easily add an incrementing number to the id by using a counter variable in your foreach loop. Here's an example of how you can achieve this:

php
$counter = 1; // Initialize counter variable

foreach ($items as $item) {
// Output the item with an incrementing number in the id
echo '
' . $item . '
';

$counter++; // Increment the counter variable
}
?>


In this code snippet, the `$counter` variable is initialized outside the foreach loop and incremented after each iteration. This way, you can easily add an incrementing number to the id of each item in your portfolio site.

Rate this post

4 of 5 based on 3857 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us