Jump to content

Get repeater item ID


Damienov
 Share

Recommended Posts

Is there a way to get the item id from a repeater field?
 
for example:

<? foreach($page->items as $item): ?>
	<div class="classname-<?php echo $item->id; ?>">
		<h2><?php echo $item->title; ?></h2>
		<p><?php echo $item->content; ?></p>
	</div>
<?php endforeach; ?>

with an expected output of something like this

<div class="classname-1">
	<h2>title 01</h2>
	<p>content</p>
</div>
<div class="classname-2">
	<h2>title 02</h2>
	<p>content</p>
</div>
<div class="classname-3">
	<h2>title 03</h2>
	<p>content</p>
</div>
Link to comment
Share on other sites

As Jeff pointed out, your example should work as coded. However I am thinking from your example that you don't actually want the repeater id, but an auto-incrementing number starting at 1. Is that right?

If so, simply define: $i=1 before the foreach starts and then $++ just before the loop closes. Then use the $i instead of $item->id

Does that help?

  • Like 2
Link to comment
Share on other sites

As Jeff pointed out, your example should work as coded. However I am thinking from your example that you don't actually want the repeater id, but an auto-incrementing number starting at 1. Is that right?

If so, simply define: $i=1 before the foreach starts and then $++ just before the loop closes. Then use the $i instead of $item->id

Does that help?

yep this works thanks :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...