Jump to content

Getting Started Question


casey
 Share

Recommended Posts

Hi, I'm just getting started with ProcessWire and am attempting to make the conversion from ExpressionEngine. My PHP skills are a little rusty, so please bear with me...

So I'm trying to make a simple "Related Entries" field. I went with a Pages field type and am accessing the values through the PageArray/WireArray method. Before I get too deep into this, I was wondering if someone could look at this and tell me if I'm going about this in the right way in terms of syntax and native functionality.

$a=$page->related;

$array = explode("|", $a);

if ($a->count()) {
	echo '<h2>'.$a->getTotal().' Related Entries</h2>';
	echo '<ul>';
	foreach ($array as $key => $value) {
		$link	= $pages->get($value)->httpUrl;
		$title	= $pages->get($value)->title;
		$out = '<li><a href="'.$link.'">'.$title.'</a></li>';
		echo $out;
	}
	echo '</ul>';
}
Link to comment
Share on other sites

Hello there - and no worries, all questions are welcome :)

In terms of logic and doing it "ProcessWire way" I think you've pretty much nailed it. Page fields are exactly what you'd want to use for these kinds of situations.

Regarding your code sample there are two things I'd like to point out:

  • PageArray acts just like regular PHP array in many ways, so you don't have to convert it to array with explode etc. before count or foreach.
  • When you're looping through PageArray, you can access each pages content directly, so that way there's no need for new $pages->get() call each time

This is probably how I'd rewrite that code of yours:

if ($page->related->count()) {
	echo '<h2>'.$page->related->getTotal().' Related Entries</h2>';
	echo '<ul>';
	foreach ($page->related as $related_page) {
		echo '<li><a href="'.$related_page->httpUrl.'">'.$related_page->title.'</a></li>';
	}
	echo '</ul>';
}

Hope this helps!

  • Like 4
Link to comment
Share on other sites

I can see why you exploded the relatedPages as when you echo $page->relatedPages it outputs a string of page ID's with a | character separating them, so what you did seems logical if you're not familiar with the API, but as teppo says you don't need to do that.

It is basically a PageArray of it's own and that | separated list of IDs is useful if you want to use that in a selector - something like $pages->find("id=$page->relatedPages, images.count>0"); <-- So you could do something like that to only return related pages with 1 or more images if you see what I mean.

The other thing to note is that since it's a PageArray, you can loop through as teppo says, but don't think that it's adding extra overhead. The act of grabbing fields as you need them in the foreach loop is pretty efficient in terms of speed - it doesn't grab all the fields for each page, just the ones you need as you need them (so I understand). I think ryan said it best elsewhere that there might be a few extra queries this way, but overall you're making savings in terms of storing data in memory and since each field is a table and well-indexed it's arguable as to whether there is any speed difference at all from other systems where all of a specific template's fields are stored in a single table.

Not sure why I felt the need to try and explain some of the mechanics, but there we go. I'm sure someone could explain it better than I did as well :)

  • Like 3
Link to comment
Share on other sites

  • 4 weeks later...

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