Jump to content

Recommended Posts

Posted

I've created several fields and want to output their values on my page.

Normally, the following works fine

<?=$page->field-name-here?>

For my latest fields this doesn't work and is instead outputting an integer (which I imagine is the page number).

<h4>Course Details</h4>
<strong>Level:</strong> <?=$page->course_detail_level->title?><br />
<strong>Category:</strong> <?=$page->course_detail_category?><br />
<strong>Grouping:</strong> <?=$page->course_detail_grouping?><br />

The above fields are mostly all based on a Page type and then an input field type of AsmSelect.

I imagine my issue is that I am therefore dealing with an array and need to create a foreach statement?

Posted

Making progress. Because each field must be an array, it appears I do need to loop through each.

This is working but perhaps not the way I *should* be solving it

<?php
$course_level = $page->course_detail_level;
$course_category = $page->course_detail_category;
$course_grouping = $page->course_detail_grouping;
$course_prerequisite = $page->course_detail_prerequisite;
$course_writer = $page->course_detail_writer;
$course_lessons = $page->course_detail_lessons;
			
foreach($course_level as $level){
echo "<strong>Level:</strong> {$level->title}";}
			
foreach($course_category as $category){
echo "<strong>Category:</strong> {$category->title}";}

foreach($course_grouping as $grouping){
echo "<strong>Grouping:</strong> {$grouping->title}";}
			
foreach($course_prerequisite as $prereq){
echo "<strong>Prerequisite Course:</strong> {$prereq->title}";}
			
foreach($course_writer as $writer){
echo "<strong>Writer:</strong> {$writer->title}";}
			
foreach($course_lessons as $lesson){
echo "<strong>Lessons:</strong> {$lessons->title}";}
			
?>
  • Like 1
Posted

Hey Peter,

If there is more than one value in the page field array, then looping is likely what you want, but if the page field is set to only allow one option, then you are better off with your good friend "first":

$page->course_detail_level->first()->title
  • Like 1
Posted

Making progress. Because each field must be an array, it appears I do need to loop through each.

This is working but perhaps not the way I *should* be solving it

Why do you feel that it's not the way you should be doing it? The only case where I think there could be a cleaner way is if you only wanted to print a comma separated list of the titles of the items inside the Page-fields. Then you could do

<h4>Course Details</h4>
<strong>Level:</strong> <?= $page->course_detail_level->implode(", ", "title"); ?><br />
<strong>Category:</strong> <?= $page->course_detail_category->implode(", ", "title"); ?><br />
<strong>Grouping:</strong> <?=  $page->course_detail_grouping->implode(", ", "title"); ?><br />

Otherwise, just stick with the loops :)

  • Like 4
Posted

Why do you feel that it's not the way you should be doing it? 

More a reflection on my PHP noob-ness. Somtimes you discover *a* way to do something, stick with it and don't realise it may not best practice.

's all :)

  • Like 1

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...