Jump to content


Photo

If first/last page in array


  • Please log in to reply
10 replies to this topic

#1 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 374 posts
  • 237

  • LocationGainesville, Florida

Posted 21 March 2012 - 09:12 PM

How can I detect (and add additional markup) to the first/last iteration using the API?

Here's an example:


$profiles = $pages->get("/meet-us/executive-profiles")->children("limit=5");

foreach ($profiles as $profile)
echo "<h2>{$profile->job_title}</h2>".
"{$profile->biography}";


With raw PHP I would add a counter to the foreach:

$i = 0;
$length = count($profiles);
foreach ($profiles as $profile) {
if ($i == 0) {
// first
} else if ($i == $length - 1) {
// last
}
$i++;
}


I'm sure there's a more elegant solution using the API.
I think I need to use $a->$key, but I'm not quite sure how to put it together.
Any guidance would be much appreciated.

:)

#2 MarcC

MarcC

    Sr. Member

  • Members
  • PipPipPipPip
  • 255 posts
  • 63

  • LocationCalifornia

Posted 21 March 2012 - 11:41 PM

What about ProcessWire's $a->first() and $a->last() ? Unless there's something I'm not seeing here.

I'm a freelance, processwire-using web designer based in california. work site | personal site | visuals


#3 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 22 March 2012 - 02:28 AM

Yup, so:

if ($profile == $profiles->first()) {
  echo "I'm first!";
}

Should work.

Edited by apeisa, 22 March 2012 - 02:54 AM.
added closing bracket


#4 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 22 March 2012 - 02:29 AM

Oops - forgot a closing bracket and can't edit on my phone.

#5 arjen

arjen

    Sr. Member

  • Members
  • PipPipPipPip
  • 340 posts
  • 125

  • LocationHoogeveen, The Netherlands

Posted 22 March 2012 - 02:48 AM

The cheat sheet helped my out on numerous occasions for getting the stuff you want. Very useful for this kind of information.
work will always be the curse of the drinking classes...

#6 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 374 posts
  • 237

  • LocationGainesville, Florida

Posted 22 March 2012 - 08:43 AM

Thanks all — things are becoming a bit clearer now.

I think it's just a matter of convincing my brain to look at things from the perspective of the API and not raw PHP.
I'll study up on the cheat sheet some more, and try not to ask these rudimentary questions. No promises though. :P

#7 arjen

arjen

    Sr. Member

  • Members
  • PipPipPipPip
  • 340 posts
  • 125

  • LocationHoogeveen, The Netherlands

Posted 22 March 2012 - 09:39 AM

That's what I had to learn too. You can look at it from a jQuery point of view. Select/find the element you want to use and do something. That's why I posted the cheat sheet. It's pretty straight-forward. I've got a feeling I've barely scratched the surface, but I do see it's potential. That why PW rocks.
work will always be the curse of the drinking classes...

#8 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 22 March 2012 - 02:29 PM

The best thing I've had fun playing with recently is chaining in PW, and it's worth knowing about. Here's a good, slightly abstract example:

On a large communal news website, an Article page has a PageField called Author - this links to the Author's page. The Author works for a Company - there might be several authors in that company writing on this website and you might be curious and want to find out various things like the following (pretend we're on the article page still):

// Let's find all articles by this author:
$author_articles = $pages->find("template=article,author=$page->author");

// Let's get the company name that this author works for - going through a few pages to get this one - from the article to the the author' page then the company page - all using the incredibly powerful PageField field type <img src='http://processwire.com/talk/public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
echo $page->author->company->title; // Ridiculously simple huh?

// Now let's grab a list of other authors from this company - not sure why you would, but you could (the authors are all PW users in this case, so the template is 'users')
$company_authors = $pages->find("template=user,company=$page->author->company");

// And finally let's get a list of all articles written by all authors in this company using $company_authors above
$company_articles = $pages->find("template=article,author=$company_authors");

Now some people might wonder how the last one works - surely $company_authors is an object or array right? Well if you run a simple echo on it, it doesn't spit it's dummy out like PHP normally would, but rather gives you a concatenated list of page IDs in the format: 123|124|1024|456 - the pipe character is used in the last $page->find to mean "OR" so it works perfectly.

And this is why I keep getting excited whenever I work with ProcessWire. I would need literally dozens of lines of code to recreate that example in any other CMS I can think of.

I pulled in the articles and authors by template, but there are various ways of doing it and I just did it with template names as that didn't assume any particular site structure, so it's not necessary to follow that exactly.

Have fun! :)

#9 renobird

renobird

    Sr. Member

  • Members
  • PipPipPipPip
  • 374 posts
  • 237

  • LocationGainesville, Florida

Posted 22 March 2012 - 02:42 PM

Pete,

Great examples. The pipe serving as the 'or' in the last example is really cool.

#10 Pete

Pete

    Administrator

  • Administrators
  • 1,756 posts
  • 658

  • LocationChester, England

Posted 22 March 2012 - 02:57 PM

The best bit I find is in this case (or in any ProcessWire code samples for that matter) is that whilst not all of those things would necessarily be that useful in that particular context, you can see how you might just grab a company's users to display on the company page, or company details on the author's page and so on - I just love the ease at which you can get at related content from whatever angle you like in one or two lines of code, just leaving you with the fun job of outputting it on the screen.

Anything that lets someone as impatient as I am output the finished result to the screen in as little time as possible is great, and from a business point of view the hours of work it saves and the legibility of the code that's produced are invaluable, especially when it comes to re-using similar code in other websites.

A fun thing I did recently was to implement a gallery page on a website and that had child pages for different gallery sections. The whole thing took about 20 minutes and about as many lines of PHP and HTML ;)

#11 alanfluff

alanfluff

    Sr. Member

  • Members
  • PipPipPipPip
  • 405 posts
  • 118

  • LocationOttawa, Canada

Posted 23 March 2012 - 05:46 AM

THANKS for those examples Pete, code snippets like this are really helping me help myself :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users