Jump to content

Incremental count to each image


Peter Knight
 Share

Recommended Posts

I am trying to assign an incremental number to each image starting from 0 

<li uk-slideshow-item="0"><img src="images/photo.jpg"></li>
<li uk-slideshow-item="1"><img src="images/dark.jpg"></li>
<li uk-slideshow-item="2"><img src="images/light.jpg"></li>

The API docs mention I can use the following

Quote

$a->eq($n)

But when I apply it below, I get the following error

 Method Pageimage::eq does not exist or is not callable in this context 

Here's my usage

foreach($item->images as $photo){
echo "
<li uk-slideshow-item='{$photo->eq($n)}'><a href='#'><img src='{$photo->size(150,100)->url}'  alt=''></a></li>
";}

Thanks again

P

Link to comment
Share on other sites

$c = 0;
foreach($item->images as $photo){
  echo "<li uk-slideshow-item='$c'><a href='#'><img src='{$photo->size(150,100)->url}' alt=''></a></li>";
  $c++;
}

just use a good old counter var instead ?

  • Like 2
Link to comment
Share on other sites

From what I read, it actually returns the nth item from the array.

Returns the single item at the given zero-based index, or NULL if it doesn't exist.

Couldn't you just do:

$count = -1;
foreach($page->images as $photo) {
	echo "<li uk-slideshow-item='{$count}'><a href='#'><img src='{$photo->size(150,100)->url}'  alt=''></a></li>";
	$count++;
}

=======

Dragan beat me too it.

Edited by louisstephens
Dragan be me too it.
Link to comment
Share on other sites

15 hours ago, Peter Knight said:

The API docs mention I can use the following

Quote


$a->eq($n)

https://processwire.com/api/ref/wire-array/eq/

It says: "Returns the item at the given index..." if you do not know the index, you cannot get it this way. In other words, you already have the reference to the object, you need an index, so you are facing some sort of opposite scenario (sure, the page ID and your index are not the same, btw).

Otherwise just do what @dragan has suggested ? 

14 hours ago, louisstephens said:

$count = -1; foreach($page->images as $photo) { echo "<li uk-slideshow-item='{$count}'><a href='#'><img src='{$photo->size(150,100)->url}' alt=''></a></li>"; $count++; }

I do not recommend starting form -1 if you increment it at the last line in the loop as in this case the first index will be -1 and Peter needs it to be 0.

Edited by szabesz
typos
  • Like 1
Link to comment
Share on other sites

Thanks guys.

I have it working now and see the relevant php docs

Quote
$a++ Post-increment Returns $a, then increments $a by one.

That's cool. So we put $count++; at the end of the echo because when it loops back around, it uses this new value as the starting number. Good to know.

 

 

 

 

  • Like 1
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...