Jump to content

Why is $pages undefined?


watertower
 Share

Recommended Posts

Building my first PW site,

So this code is in my template page:


<?php
// LIST PARTICIPANTS CHILDREN OF THIS PAGE
$overviewchildren = '';
function findPartnerTitle($partnerID){
    $partnerTitle = '';
    $partnerTitle = $pages->find("parent=1004,partnerID=$partnerID")->title;
    return $partnerTitle;
}
foreach($page->children("id!=$page") as $participant){
    $partnerID = $participant->partnerID;
    $overviewchildren .=
    '<div class="row">' .
        '<div class="col-sm-2">' .
        '</div>' .
        '<div class="col-sm-10">' .
            '<strong><a href="' . $participant->url . '">' . $participant->p_first_name . ' ';
            if($participant->p_middle_name) { $overviewchildren .= $participant->p_middle_name . ' '; }
            $overviewchildren .= $participant->p_last_name . '</a></strong><br />' .
            $participant->p_job_title . '<br />' .
            $participant->p_department . '<br />' .
            findPartnerTitle($partnerID) . '<br />' .
        '</div>' .
    '</div>';
}
?>

I do not understand why $pages is "undefined":

Quote

 

Notice: Undefined variable: pages in /home/isi-wiki/processwire/site/templates/participants_list.php on line 37

Fatal error: Call to a member function find() on a non-object in /home/isi-wiki/processwire/site/templates/participants_list.php on line 37

Error: Call to a member function find() on a non-object (line 37 of /home/isi-wiki/processwire/site/templates/participants_list.php) 

This error message was shown because site is in debug mode ($config->debug = true; in /site/config.php). Error has been logged.

 

 

 

The larger problem I'm trying to solve is to get the title of Academic Partner (id=1004)

The previous site was a relational database so I did $participant->partnerID equals the partnerID field under the "Academic Partners" page (1004) ... maybe there's an easier way to find the title of that page? ... or easier way to associate Academic Partner title with the participant?

Edited by watertower
Error message was in white text; could not be seen. Changed to black.
Link to comment
Share on other sites

1 hour ago, Ivan Gretsky said:

This is because of a variable scope. Use wire('pages')->find(...)... instead. See Ryan's explanation here.

Many thanks to you and @adrian for answering this question and so quickly! This did indeed eliminate the error.

If I may trouble the community once more for hint, I've still got the problem of trying to get the title of a page; I have found it's id but I lack the knowledge on how to grab the title. 

This function does not return the title of the page as I think it should (it returns nothing that I can see on the page):

<?php
function findPartnerTitle($partnerID){
    $pageID = wire('pages')->find("template=partner-add-edit,partnerID=$partnerID");
    return wire('pages')->find("id=$pageID")->title;
}
?>

 

  • Like 1
Link to comment
Share on other sites

$partnerTitle = $pages->find("parent=1004,partnerID=$partnerID")->title;

Will not work cause find() returns a page array and not a single page.

Rather use

$partnerTitle = $pages->get("parent=1004,partnerID=$partnerID")->title;

Or even

$partnerPage = $pages->get("parent=1004,partnerID=$partnerID");
$partnerTitle = $partnerPage->id ? $partnerPage->title : "PartnerPageNotFound";

But why you need a function for that is beyond me. And also why there's obviously an ID you enter manually to then get a page? Why not use a page field to select the partner page?

 

Link to comment
Share on other sites

I do not see a need for function here at all

<?php
// LIST PARTICIPANTS CHILDREN OF THIS PAGE
$overviewchildren = '';

foreach($page->children as $participant){
    $partnerID = $participant->partnerID;
    $overviewchildren .=
    '<div class="row">' .
        '<div class="col-sm-2">' .
        '</div>' .
        '<div class="col-sm-10">' .
            '<strong><a href="' . $participant->url . '">' . $participant->p_first_name . ' ';
            if($participant->p_middle_name) { $overviewchildren .= $participant->p_middle_name . ' '; }
            $overviewchildren .= $participant->p_last_name . '</a></strong><br />' .
            $participant->p_job_title . '<br />' .
            $participant->p_department . '<br />' .
            $pages->get("parent=1004, partnerID=$partnerID")->title . '<br />'
        '</div>' .
    '</div>';
}
?>

But actually $participant->partnerID should be a Page Field so you can just write

$participant->partnerID->title

P.S. Note the use of get instead of find.
 

  • Like 1
Link to comment
Share on other sites

@Soma and @Ivan Gretsky you're right, the function was not necessary.

@Ivan Gretsky I do have a partnerID that is not page field but it is a numeric id. I will change to Page Field type.

 Anywho, I am in business again, and moving forward until I get stuck again! Thank you for your help!

Edited by watertower
Added "not". I said I have a page field but I did *not* have a page field.
Link to comment
Share on other sites

1 hour ago, Ivan Gretsky said:

$participant->partnerID->title

Update: for the example above to work, I discovered I needed to have the Page Fields' Dereference in API (under the fields Details tab) set to "Single page (Page) or empty page (NullPage) when none selected"

  • Like 1
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

  • Recently Browsing   0 members

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