Sorry… I will stop asking questions soon… Can anyone explain why this doesn’t work and what I should be doing instead? The first part returns a list of all pages across the site with a specific value, I then want to pull in data from structured fields from those pages… the logic seems right but I’m obviously missing something.
Thanks in advance (again)
<!-- ALL COURSES -->
<?php $listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10); ?>
//pagination code
<hr />
<?php foreach($listings->courses()->toStructure() as $listing): ?>
<p>
<strong><?php echo $listing->course_specific_title()->html() ?></strong>, <?php echo $listing->town()->html() ?> – <a href="<?php echo url($listing->parent()) ?>">Read full course description</a>
</p>
<hr />
<?php endforeach ?>
<!-- END ALL COURSES -->
What sort of error do you get? Or just not the expected result?
What is $listing->parent()
?
I don’t get any error messages on screen, just a truncated page, forgive me, not hugely experienced in the finer points of PHP and error reporting. The url($listing->parent() simply returns the introduction/overview page, the course details are stored in a child page (identified in the first part of the code) within the structured fields.
Could you pls. turn on debugging in your config.php
c::set('debug', true);
Then any errors should be output on screen or in the server error logs.
The problem here is probably that you are trying to get the parent of a field instead of the parent of a page. To get the page of a field, you can use $field->page()
, so maybe instead of
<?php echo url($listing->parent()) ?>
you could try
<?php echo url($listing->page()->parent()) ?>
Not tested, though.
Fatal error: Call to a member function toStructure() on a non-object in /Applications/MAMP/htdocs/tbab/site/snippets/course-listing.php on line 14
The first line of code is targeted at the child page and the url($listing->parent()) was actually working, was only when I tried to call in the data from the structured field I started experiencing the problem…
<?php foreach($listings->courses()->toStructure() as $listing): ?>
when it was
<?php foreach($listings as $listing): ?>
and pulling in data from standard fields all was working as expected
Fatal error: Call to a member function toStructure() on a non-object in /Applications/MAMP/htdocs/tbab/site/snippets/course-listing.php on line 16
16.<?php foreach($listings->courses()->toStructure() as $listing): ?>
$listings
is a collection of pages where a field called “addtocalendar” has the value “include”, right?
And courses is the structure field in those pages?
Then you need an additional foreach loop:
<?php
$listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10);
//loop through the collection of pages
foreach($listings as $listing) {
// fetch the structure field called courses
$courses = $listing->courses()->toStructure();
//loop through the structure field collection
foreach($courses as $course) {
//do stuff;
}
}
?>
Watch out for the missing parens after courses
on line 6:
$courses = $listing->courses()->toStructure();
![:wink: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=5)
Oh, thanks a lot, @AugustMiller ![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=5)
If I have understood you correctly I now have:
<?php
$listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10);
//loop through the collection of pages
foreach($listings as $listing) {
// fetch the structure field called courses
$courses = $listing->courses()->toStructure();
//loop through the structure field collection
foreach($courses as $course) {
<?php if($listings->pagination()->hasNextPage()): ?>
<p><a class="next" href="<?php echo $listings->pagination()->nextPageURL() ?>">‹ older posts</a></p>
<?php endif ?>
<?php if($listings->pagination()->hasPrevPage()): ?>
<p><a class="prev" href="<?php echo $listings->pagination()->prevPageURL() ?>">newer posts ›</a></p>
<?php endif ?>
<hr />
<p><strong><?php echo $courses->course_specific_title()->html() ?></strong>, <?php echo $listing->town()->html() ?> – <a href="<?php echo url($listing->page()->parent()) ?>">Read full course description</a></p>
}
}
?>
But this gives me the following:
Parse error: syntax error, unexpected ‘<’ in /Applications/MAMP/htdocs/tbab/site/snippets/course-listing.php on line 11
I feel I’m missing something very obvious and I appreciate your patience
Here you go (hopefully …):
<?php
$listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10);
//loop through the collection of pages
foreach($listings as $listing):
// fetch the structure field called courses
$courses = $listing->courses()->toStructure();
//loop through the structure field collection
foreach($courses as $course): ?>
<?php if($listings->pagination()->hasNextPage()): ?>
<p><a class="next" href="<?php echo $listings->pagination()->nextPageURL() ?>">‹ older posts</a></p>
<?php endif ?>
<?php if($listings->pagination()->hasPrevPage()): ?>
<p><a class="prev" href="<?php echo $listings->pagination()->prevPageURL() ?>">newer posts ›</a></p>
<?php endif ?>
<hr />
<p><strong><?php echo $courses->course_specific_title()->html() ?></strong>, <?php echo $listing->town()->html() ?> – <a href="<?php echo url($listing->page()->parent()) ?>">Read full course description</a></p>
<?php endforeach ?>
<?php endforeach ?>
<?php
$listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10);
//loop through the collection of pages
foreach($listings as $listing):
// fetch the structure field called courses
$courses = $listing->courses()->toStructure();
//loop through the structure field collection
foreach($courses as $course): ?>
<?php if($listings->pagination()->hasNextPage()): ?>
<p><a class="next" href="<?php echo $listings->pagination()->nextPageURL() ?>">‹ older posts</a></p>
<?php endif ?>
<?php if($listings->pagination()->hasPrevPage()): ?>
<p><a class="prev" href="<?php echo $listings->pagination()->prevPageURL() ?>">newer posts ›</a></p>
<?php endif ?>
<hr />
<p><strong><?php echo $courses->course_specific_title()->html() ?></strong>, <?php echo $listing->town()->html() ?> – <a href="<?php echo url($listing->page()->parent()) ?>">Read full course description</a></p>
<?php endforeach ?>
<?php endforeach ?>
@texnixe was faster. ![:wink: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=5)
@lukasbestle: Yeah, sometimes you win, sometimes you loose in this little competition ![:wink: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=5)
1 Like
Maybe we should install a Discourse plugin that tells us if the other one has seen a post already. ![:wink: :wink:](https://emoji.discourse-cdn.com/twitter/wink.png?v=5)
Well, I think we are a good team and two answers are better than none for the TOs.
2 Likes
The dynamic duo - really appreciate your help and patience, have you spotted the deliberate error tho?
I now have the pagination in the loop, DOH!
Can I buy you both a virtual beer?
You are welcome, and cheers
![:slight_smile: :slight_smile:](https://emoji.discourse-cdn.com/twitter/slight_smile.png?v=5)
Ok, let’s get this right for other people:
<?php
$listings = $site->index()->filterBy('addtocalendar', 'include')->paginate(10);
//loop through the collection of pages
foreach($listings as $listing):
// fetch the structure field called courses
$courses = $listing->courses()->toStructure();
//loop through the structure field collection
foreach($courses as $course): ?>
<p><strong><?php echo $courses->course_specific_title()->html() ?></strong>, <?php echo $listing->town()->html() ?> – <a href="<?php echo url($listing->page()->parent()) ?>">Read full course description</a></p>
<?php endforeach ?>
<?php endforeach ?>
<?php if($listings->pagination()->hasNextPage()): ?>
<p><a class="next" href="<?php echo $listings->pagination()->nextPageURL() ?>">‹ older posts</a></p>
<?php endif ?>
<?php if($listings->pagination()->hasPrevPage()): ?>
<p><a class="prev" href="<?php echo $listings->pagination()->prevPageURL() ?>">newer posts ›</a></p>
<?php endif ?>
Thanks guys - much appreciated
I have just one further question, is it possible to gather the structured field data as individual ‘data packets’ across all the $listings pages? At present it gathers them from the $listings page and groups them together, so you can only sort, for example, by date, within that group. Then it starts the sorting process over for the next group from the next $listings page.
Just curious, it would be useful to know, so don’t bust a gut on this one.