Find out item position in conditional query result


I’m building simple gallery, Each gallery has list of photos, which is selected with specific conditions (order, where status = approved, etc) using Gallery::relations.

I’d like to find out index of current photo on the list (to show this is photo {index} out of {count}) and get the links to next / previous photo.

Is there a better way to get this data than getting the list, looping through it and comparing id of list element and current photo?

Why don’t you do it by TIMESTAMPS? or via IDs? You do not need to know the exact index position… you order by TIMESTAMP or IDS and then previous or next is just a matter of selecting a previous or next on TIMESTAMPS or IDs.

Ok, it’ll solve the problem of next/prev link, but it won’t help finding out item index.

You could consider using some jquery plugin like ad-gallery…these plugins does it automatically and are jquery based.

I’m saying that because i’ve been working with galleries since i started with Yii, in all my client’s projects I’ve put a different plugin and all of them work as you mentioned.

For example, see my portfolio at and click some client’s links, in Procelt, Fogao de Lenha, Alisson Zaia and Fotopirata there are 4 different galleries.

Hope this helps!