Fetching data from children's subpage

Hello I am trying to fetch some data from a children’s subpage.
I tried a lot of ways. My last try was this :

<?php foreach(page('project')->children()->find("projectsub") as $projectsub): ?>
    <?= $projectsub->projecttext()->kt() ?>
<?php endforeach ?>

But all I get is this error message:

"Invalid argument supplied for foreach()"

And I am not sure what I am doing wrong here.

A foreach loop doesn’t work for a single page, only for page collections:

if ($projectsub = page('project')->children()->find("projectsub")) {
  echo $projectsub->projecttext()->kt();
}

But maybe you want something else?

1 Like

I understand! Thanks for this, that was exactly I was looking for.

I got the same error “invalid argument supplied for foreach()” with the following code:

<?php if ($page = page('community')): ?>

  <?php foreach ($lblogs as $article): ?>
    <?= $article->heading()->html() ?>
 <?php endforeach ?>

<?php endif ?>

How is is $lbogs defined? Have you defined the variable somewhere? It needs to be an iterable expression.

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable (PHP: foreach - Manual)

Please post code blocks within three backticks on a separate line before and after the code block to format your code and make it more readable. Thanks.

$lblogs = $page

  ->children()

  ->listed()

  ->sortBy('date', 'asc')

  ->when($filterBy, function($filterBy){

    return $this->filterBy('tags', 'podcast', ',');

   })

  ->flip()

  ->offset(3)

  ->pagenate(3);

There is a typo, pagenate must be paginate, with an ì`.

Also, your when method doesn’t make sense, because you are not filtering by the variable $filterBy, but by a harcoded value. Is the $filterBy variable defined?

Again: Please post code blocks within three backticks on a separate line before and after the code block to format your code and make it more readable. Thanks.

filterBy is defined

Thanks, but it doesn’t fetch any article, no more error but no article is fetched

But how many articles are there that meet the criteria?

5 articles

I am fetching to another page

<?php if ($page = page('community')): ?>

<?php endif ?>

Maybe you can post the complete context

Looks like you first define $lbogs, and then redefine $page

<?php $firstblogs = $page->children()->listed()->sortBy('date', 'asc')->filterBy('tags', 'podcast', ',')->flip()->limit(3); $lblogs = $page->children()->listed()->sortBy('date', 'asc')->filterBy('tags', 'podcast', ',')->flip()->offset(3)->paginate(3); ?> <?php if ($page = page('community')): ?> <?php foreach ($firstblogs as $article) ..... <?php foreach ?> <?php endif ?> <?php if ($page = page('community')): ?>
<?php foreach ($lblogs as $article): ?>
<?php foreach ?> <?php endif ?>

Sorry, but this code is unreadable. I’ve asked you two times to format your code properly. This is how you do it:

Screenshot 2020-12-19 at 12.00.54

Preformatted text<?php $firstblogs = $page->children()->listed()->sortBy(‘date’, ‘asc’)->filterBy(‘tags’, ‘podcast’, ‘,’)->flip()->limit(3);

Preformatted text$lblogs = $page->children()->listed()->sortBy(‘date’, ‘asc’)->filterBy(‘tags’, ‘podcast’, ‘,’)->flip()->offset(3)->paginate(3); ?>

<?php if ($page = page('community')): ?>

Preformatted text<?php foreach ($firstblogs as $article) ?>

Preformatted text<?php foreach ?>
Preformatted text<?php endif ?>

<?php if ($page = page('community')): ?>
<?php foreach ($lblogs as $article): ?>
<?php foreach ?>

Preformatted text<?php endif ?>

I posted a screenshot above how to format your code.

I assume you want to fetch the children of the community page? Then your order of code is wrong, because you are trying to fetch the children of the current page, not of the community page.

Also, because $page is this special variable that refers to the current page, it makes sense to use a different variable if you want to store another page.

<?php

if ( $p = page('community') ) {
  $lblogs = $p
               ->children()
               ->listed()
                ->sortBy(‘date’, ‘asc’)
                ->filterBy(‘tags’, ‘podcast’, ‘,’)
                ->flip()
                ->offset(3)
                ->paginate(3); 
  foreach ( $lblogs as $article) {
    // do stuff
  }
}

correction please, I do not understand you

Thanks, I will try it out now

I still encounter error,
syntax error, unexpected ‘<’

and that’s my html code