Jump to content

Newbie here confused about shift() and pop()


Tom.
 Share

Recommended Posts

So I'm outputting an image field array, I believe this is known as WireArray (bare with me, I only started developing for ProcessWire Yesterday). 

The cheatsheet has to be one of the best things ever! Not only does it show how simple ProcessWire is and how well crafted the API is, kudos to whoever came up with the cheatsheet. One thing I would like to see is example usages when you click more because at the moment, more doesn't really tell you anything more. I can tell it's inspired by jQuery's API documentation so I have no doubt this will be coming.

So, moving on to the problem I'm having, looking at the cheatsheet $a->shift() will remove the first item for the array and then return it. So I have 

$gallery = $page->gallery;   // A multi-upload image field

$featured = $gallery->first()->size(800,550)->url; // Works fine

$slider = $gallery->shift() // Doesn't work

$slider = $gallery->pop() // Doesn't work

$footer = $gallery->last()->size(800,1100)->url; // Works fine 

So my page layout goes like this: 
                                                 ___________
                                                |__________ | <--  $featured // First Image 

                                                |        |        |

$slider // Image Gallery --> |        |        | <--  $page->content // Body Content Field
                                                |_____|____ |
                                                |__________| <-- $footer // Footer Image

I have no idea if that example helps, but for usability that's how I want the gallery to work, so the user can just drag all their images in at once, move the featured to the front and the footer to the back. 

However $gallery->shift() returns nothing and $gallery->pop() returns nothing. $gallery->slice(1) get's rid of the first one in that array but I want to get rid of the first and last. 

Thanks,

Tom

Link to comment
Share on other sites

Welcome to the forums Tom!

All kudos to Soma for the cheatsheet. Best thing since sliced bread :) More detailed examples are a little slower in happening but some of the items do have examples, not all.

Shift and Pop are like their PHP array equivalents - shift gets the first item from the array, pop the last, and essentially removes it from the array for use right now.

So your example would be something like:

// Shift the first item off the beginning of the array to use right now. It's no longer in the array for this instance of the page
$featured = $page->gallery->shift();
echo $featured->size(800,550)->url;
 
// Get the footer item now for use later on - we don't want to iterate over it when displaying the other images just below:
$footer = $page->gallery->pop();
 
// Now do some funky gallery stuff - this instance of the gallery array no longer contains the first and last items it did earlier
foreach ($page->gallery as $item) {
    echo $item->size(400,400)->url;
}
 
// And now we can echo the footer
echo $footer->size(800,550)->url;

That should work, though typed into the browser and untested - let us know how you get on :)


P.S. It is also perfectly acceptable to create two new single-image fields for featured and footer - might keep things neater in the admin for you - but this was an interesting request to explain some less-used functionality I think so I went with it :D

  • Like 8
Link to comment
Share on other sites

That is so much cleaner than I expected it to be! I guess I misunderstood shift() and pop() and it works so much better than I thought it did originally! 

Ah ProcessWire just keeps getting better and better! Thank you very much! 

  • 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

×
×
  • Create New...