Jump to content

Recommended Posts

Posted

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

Posted
$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
Posted (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 by louisstephens
Dragan be me too it.
Posted (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 by szabesz
typos
  • Like 1
Posted

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

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
  • Recently Browsing   0 members

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