Jump to content

[SOLVED] Trouble Comparing id Property of PageArray Objects


Boost
 Share

Recommended Posts

Hei!

I'm facing an issue with comparing the id property of PageArray objects in my ProcessWire template. I've been working on a blog post template, and I'm trying to display the author's name by comparing the id of the author stored in the blog post page with a list of authors.

Here's the relevant code snippet from my template:

// Grab other pages' content
$authors = $pages->find("template=author");

<?php foreach ($authors as $item) : ?>
    <?php if ($item->id == $page->author->id) : ?>
        <?= $item->title ?>
    <?php endif ?>
<?php endforeach ?>

However, this code isn't producing any output, even though I've verified that both $authors and $page->author contain the expected id values through var_dump.

object(ProcessWire\PageArray)#232 (3) {
  ["count"]=>
  int(2)
  ["items"]=>
  array(2) {
    ["Page:0"]=>
    array(5) {
      ["id"]=>
      int(1117)
      ["name"]=>
      string(14) "boost-ai-staff"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(8) "Boost.ai"
    }
    ["Page:1"]=>
    array(5) {
      ["id"]=>
      int(1120)
      ["name"]=>
      string(12) "bill-schwaab"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(12) "Bill Schwaab"
    }
  }
  ["selectors"]=>
  string(15) "template=author"
}
object(ProcessWire\PageArray)#244 (3) {
  ["count"]=>
  int(1)
  ["items"]=>
  array(1) {
    ["Page:0"]=>
    array(5) {
      ["id"]=>
      int(1117)
      ["name"]=>
      string(14) "boost-ai-staff"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(8) "Boost.ai"
    }
  }
  ["selectors"]=>
  string(0) ""
}

object(ProcessWire\PageArray)#232 (3) {
  ["count"]=>
  int(2)
  ["items"]=>
  array(2) {
    ["Page:0"]=>
    array(5) {
      ["id"]=>
      int(1117)
      ["name"]=>
      string(14) "boost-ai-staff"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(8) "Boost.ai"
    }
    ["Page:1"]=>
    array(5) {
      ["id"]=>
      int(1120)
      ["name"]=>
      string(12) "bill-schwaab"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(12) "Bill Schwaab"
    }
  }
  ["selectors"]=>
  string(15) "template=author"
}
object(ProcessWire\PageArray)#244 (3) {
  ["count"]=>
  int(1)
  ["items"]=>
  array(1) {
    ["Page:0"]=>
    array(5) {
      ["id"]=>
      int(1117)
      ["name"]=>
      string(14) "boost-ai-staff"
      ["parent"]=>
      string(9) "/authors/"
      ["template"]=>
      string(6) "author"
      ["title"]=>
      string(8) "Boost.ai"
    }
  }
  ["selectors"]=>
  string(0) ""
}

I'd appreciate any insights or suggestions on how to fix this issue.

Thanks!

Link to comment
Share on other sites

4 hours ago, WillyC said:

you showd 4 pageArrays.but 

what is value $page->author->id ? 1117, 1120 or mebe some thing else ?

or go back .-.-.- why load.all author when u only.need 1 ?

<?= $page->author->title ?>

@Boost

Ok. Here is my entire setup:

1) I created an Authors page to hold the authors. Here is the  author template:

01.png.28406d03e70e29050d293ae9f936d0f6.png

02.thumb.png.65a6c5e5d6ed0c6b79f08f8a5b60d28a.png

2) I also created a author field that is a page reference type.

03.thumb.png.0b343c0f2de3554622e6452d6baf0fad.png

 

3) So, I have my author field in my blog-post template.

04.thumb.png.4d4bb20d5823faa620202e6cb8c1f2ba.png

4) And in my blog post page, I can add one or more authors.

 05.thumb.png.b1d833ac23b47df3b75c6f986ecf0e59.png

Link to comment
Share on other sites

You could use this instead:

if($page->author->has($item)) // you could name the field "authors" to remove ambiguity

The issue is that you were trying to compare with a (non-existing) id from a PageArray instead of a single author (since you allow multiple authors to be added in your page reference field)

  • Like 1
Link to comment
Share on other sites

3 minutes ago, monollonom said:

You could use this instead:

if($page->author->has($item)) // you could name the field "authors" to remove ambiguity

The issue is that you were trying to compare with a (non-existing) id from a PageArray instead of a single author (since you allow multiple authors to be added in your page reference field)

Sorry, I'm not following you.

Link to comment
Share on other sites

3 hours ago, Boost said:

4) And in my blog post page, I can add one or more authors.

You said your page reference field “author” can hold multiple authors, so when trying to call $page->author you will get an array of authors instead of a single author and so when trying to get the “id” property you won’t get any since it’s an array, which is why your code doesn’t work.

So in your initial code sample, I was suggesting to replace line 5 with $page->author->has($item), like so:

// Grab other pages' content
$authors = $pages->find("template=author");

<?php foreach ($authors as $item) : ?>
    <?php if ($page->author->has($item)) : ?>
        <?= $item->title ?>
    <?php endif ?>
<?php endforeach ?>

 

  • Like 1
Link to comment
Share on other sites

2 minutes ago, monollonom said:

You said your page reference field “author” can hold multiple authors, so when trying to call $page->author you will get an array of authors instead of a single author and so when trying to get the “id” property you won’t get any since it’s an array, which is why your code doesn’t work.

So in your initial code sample, I was suggesting to replace line 5 with $page->author->has($item), like so:

// Grab other pages' content
$authors = $pages->find("template=author");

<?php foreach ($authors as $item) : ?>
    <?php if ($page->author->has($item)) : ?>
        <?= $item->title ?>
    <?php endif ?>
<?php endforeach ?>

 

Thank you.

I'm not completely understand what I did, but it seems to work now. 

<?php foreach ($page->author as $item) : ?>
	<?= $item->title ?>
<?php endforeach ?>

 

Link to comment
Share on other sites

9 hours ago, Boost said:

I'm not completely understand what I did, but it seems to work now. 

What do you need to understand?

$page->author is a Page Reference field that can list multiple authors pages, so authors are stored in a PageArray, a kind of array dedicated in listing ProcessWire pages.
So in your loop, $item is a Page instance using the template "author", and the Page class has a property "title" that corresponds to the title filled in this author's page admin.

  • Like 2
Link to comment
Share on other sites

  • Boost changed the title to [SOLVED] Trouble Comparing id Property of PageArray Objects

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

  • Recently Browsing   0 members

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