Peter Knight Posted March 5, 2019 Share Posted March 5, 2019 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 More sharing options...
dragan Posted March 5, 2019 Share Posted March 5, 2019 $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 ? 2 Link to comment Share on other sites More sharing options...
louisstephens Posted March 5, 2019 Share Posted March 5, 2019 (edited) 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 March 5, 2019 by louisstephens Dragan be me too it. Link to comment Share on other sites More sharing options...
szabesz Posted March 6, 2019 Share Posted March 6, 2019 (edited) 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 March 6, 2019 by szabesz typos 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted March 6, 2019 Author Share Posted March 6, 2019 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now