Jump to content

order pageArray by first image height


diogo
 Share

Recommended Posts

Ya, that's right, I really want to do that. Is there any way to do it with a selector? I tried this but it doesn't work since this info is not stored on the db...

("sort=images.height")

with this selector i do get the first image, since it is ordering alphabetically in the correct way if I remove the .height part.

Well, if not I will have to play a bit with pops and shifts and all those array things...

Link to comment
Share on other sites

You won't be able to do this at the DB level because width/height is not stored in the DB. As a result, you won't be able to query the DB based on height of an image unless you manually store it yourself in some other field. However, I think you could probably do a $page->images->sort("height"); at runtime.

Link to comment
Share on other sites

I guess I already knew the answer... and i am trying to avoid storing this info in a field.

However, I think you could probably do a $page->images->sort("height"); at runtime.

Not sure if I get it, I'm not trying to sort the images on the same page, I'm trying to sort pages by the height of the first image. I guess I will have to do it with some array manipulation.

Thanks for the answer Ryan!

Link to comment
Share on other sites

I actually did something similar on one of those villa sites, so that I could randomly pull from pages having the highest quality images. I have a Pages::save hook that saves the average width of all the photos on the page to a hidden integer field. That way I can use $pages->find() to find villas with best image quality (or at least, largest images) and sort by that, etc. In your case, you might want to do the same thing, except store the height. If all the pages have the same number of images, then you could do combined height. If they are differing numbers of images, you'd probably want to do average.

  • Like 2
Link to comment
Share on other sites

Diogo, you don't have to do it manually, just a save hook. Giving you back a simple find() on front-end it is worth considering it. It would also even work in admin sorting by the field and so on. Sure an simple page array where you store the height of the first image in a runtime property to then sort by it is also a way.

But then just add a line or two more to save the result to the page and you already have a "function" to do it automaticly.

$pa = new PageArray();
foreach($pages->find("template=xyz") as $p){
   $h = $p->images->first()->height;
   $p->img_height = $h;
   $pa->add($p);
}
foreach($pa->sort("-img_height") as $p){
   $img = $p->images->first();
   ....
}
  • Like 3
Link to comment
Share on other sites

Oh man, sometimes it's not easy to tell things in the forum :) i understood what Ryan meant, and it's actually a nice solution.

Anyway, your solution is great! I was trying to do that but somehow couldn't... i guess i needed some time away from the computer. Here is the working code, it has a small correction:

$pa = new PageArray();
foreach($pages->find("template=xyz") as $p){
       $h = $p->images->first()->height;
       $p->img_height = $h;
       $pa->add($p);// <- you had $h here, I changed it to $p
}

Thanks!

Link to comment
Share on other sites

Greetings,

This is really interesting to me.

I am finishing up a site now where I need to create slideshows from submitted pages, but (as often is the case) not all of the pages have high enough quality images. I solved the problem by creating a checkbox field that I (or the client) can use to manually choose whether the page is included in the slideshow (I added a note to only check the box if the image meets certain criteria).

There are advantages to the checkbox approach: the client can have a lot of flexibility to decide which pages are "featured." But it would be really neat in some cases to automatically exclude pages that don't meet the requirements for a slideshow...

Seems that every day another example of ProcessWire's flexibility comes to the surface!

Thanks,

Matthew

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