Jump to content

How to get data from the PageArray instead of page_ids


MarkW
 Share

Recommended Posts

A multiselect will return an array, so most likely you will want to foreach through it - something like this:

foreach($page->pagefieldname as $p) {
	echo $p->title . '<br />';
}

There are lots of other ways to grab the data so tell us more about your needs and we can help further.

  • Like 1
Link to comment
Share on other sites

Thanks, Adrian!

I've been using Ryan's skyscraper demo to try and put together something like IMdB. Here's the function I copied and modified to output the template data for my purposes.

function renderVideoData($page) {

	$searchUrl = wire('config')->urls->root . "search/";
	$na = "<span class='na'>n/a</span>";
	$videos = '';

	foreach($page->videos as $v) {
		$videos .= "\n\t<li><a href='{$v->url}'>{$v->title}</a></li>";
	}

	$out =	"\n<table id='video_data' class='table table-responsive'>" . 
		"\n\t<tbody>" . 
		"\n\t<tr><th>Title </th><td>" . ($page->title ? "<a href='$searchUrl?year={$page->title}'>{$page->title}</a>" : $na) . "</td></tr>" .
		"\n\t<tr><th>Synopsis</th><td>" . ($page->synopsis ? : $na) . "</td></tr>" . 
		"\n\t<tr><th>Release Year</th><td>" . ($page->year ? "<a href='$searchUrl?year={$page->year}'>{$page->year}</a>" : $na) . "</td></tr>" . 
		"\n\t<tr><th>Director</th><td>" . ($page->director ? "<a href='$searchUrl?director={$page->director}'>{$page->director}</a>" : $na) . "</td></tr>" .
		"\n\t<tr><th>Cast</th><td>" . ($page->cast ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Franchise</th><td>" . ($page->franchise ? "<a href='$searchUrl?franchise={$page->franchise}'>{$page->franchise}</a>" : $na) . "</td></tr>" .
		"\n\t<tr><th>Language</th><td>" . ($page->language ? "<a href='$searchUrl?language={$page->language}'>{$page->language}</a>" : $na) . "</td></tr>" .
		"\n\t<tr><th>VOD Rights</th><td>" . ($page->vod_rights ? "<a href='$searchUrl?vod_rights={$page->vod_rights}'>{$page->vod_rights}</a>" : $na) . "</td></tr>" .
		"\n\t<tr><th>Running Time</th><td>" . ($page->running_time ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Hue</th><td>" . ($page->hue->select_value ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Rating</th><td>" . ($page->rating ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Cost</th><td>" . "$" . ($page->cost ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Country</th><td>" . ($page->country ? : $na) . "</td></tr>" .
		"\n\t<tr><th>Term</th><td>" . ($page->term ? : $na) . "</td></tr>" .
		"\n\t</tbody>" . 
		"\n</table>"
		;

	return $out; 
}

I am getting decent results on my single video except that I get the page_ids. I can't figure out how to get the title of the page instead of the page_ids to show. In my example above, it would be actor's names. 

I came across Lostkobrakai's post about using two foreach's to find the multiselects but I am unclear how to implement that in my function or if I need a new function altogether. Maybe I should make a function just so I can reuse the multiselect foreach function whenever I need it?

Edited by MarkW
clarification
Link to comment
Share on other sites

I don't see anything wrong with your videos foreach, but look at the Cast line from your OP, you can't simply output $page->cast assuming "cast" is a page field. There are multiple ways, but perhaps the easiest for the way you have your code structured would be:

$page->cast->implode('<br />', 'title');

 

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