Jump to content
hellomoto

Can't loop through my menu PageArray? (can't convert class Page to type int)

Recommended Posts

I have a page field on my home template with output set to multiple pages (PageArray). I can echo its value, 1111|2222, or foreach $nav as $p echo $p, 11112222. But when I do this that I need to, it doesn't work:

					foreach ($mainnav as $p) {
						if($page->rootParent->id == $p->id) $current = 'style="background: #fff;"'; else $current = '';
						echo "<a href='{$pages->get($p)->url}' $current class='{$pages->get($p)->name}'>" . strToUpper($pages->get($p)->name) . "</a>";
					}

I get:

Notice: Object of class Page could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/processwire/wire/core/Pages.php on line 1459

 
Notice: Object of class Page could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/processwire/wire/core/Pages.php on line 1459
 
Notice: Object of class Page could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/processwire/wire/core/Pages.php on line 1459
 
Notice: Object of class Page could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/processwire/wire/core/Pages.php on line 1459
 
Notice: Object of class Page could not be converted to int in /Applications/XAMPP/xamppfiles/htdocs/processwire/wire/core/Pages.php on line 1459
 
Notice: Object of class Page 
 
I've tried the conditional in the code with one and three = signs.

Share this post


Link to post
Share on other sites

Never mind, wasn't the conditional anyway. This did it:

foreach ($mainnav as $p) {
						$p = $p->id;
						if($page->rootParent->id == $p) $current = 'style="background: #fff;"'; else $current = '';
						echo "<a href='{$pages->get($p)->url}' $current class='{$pages->get($p)->name}'>" . strToUpper($pages->get($p)->name) . "</a>";
					}
  • Like 1

Share this post


Link to post
Share on other sites

But then you already got the page ($p) so why get it again?

  • Like 1

Share this post


Link to post
Share on other sites

I don't know if this helps, but why not call those fields directly from the page object? For the if statement a single = would be wrong as you'd assign ids and not compare them. == and === are here the same, as id's are always of the same type. 

foreach($mainnav as $p) {
  if($page->rootParent->id == $p->id) $current = 'style="background: #fff;"';
  else $current = '';
  echo "<a href='{$p->url}' $current class='{$p->name}'>" . strToUpper($p->name) . "</a>";
}

Edit: Soma was faster :)

  • Like 1

Share this post


Link to post
Share on other sites

I'm having a similar (same?) problem.

 

$matches = $pages->findIDs($selector);
foreach($matches as $v) {
    $useAsRef = $pages->get($v)->use_as_ref_select;
    echo "useAsRef $v: $useAsRef <br>";
    //echo gettype($useAsRef) . "<br>"; // object
    //echo "<pre>";
    //print_r($useAsRef); // PW page
    //echo "</pre>";
    if( ($pages->get($useAsRef->id)) === 10132 ) {
        $removeArray[] = $v; // remove those
        $whichOneInstead = $pages->findIDs($v)->sammelref_rel; // add these instead
        echo "whichOneInstead : $whichOneInstead <br>";
        $addArray[] = $whichOneInstead;
    } else {
        echo "else: " . $pages->get($useAsRef->id) . " <br>";
    }
}

Now, my debug output is:

useAsRef 9609: 10132 
else: 10132 
useAsRef 9615: 10132 
else: 10132 
useAsRef 9611: 10132 
else: 10132 
useAsRef 9607: 10132 
else: 10132 
useAsRef 9589: 10129 
else: 10129 
useAsRef 9619: 10129 
else: 10129 
useAsRef 9599: 10129 
else: 10129 
useAsRef 9623: 10132 
else: 10132 

Why is it outputting the wanted page id in my else() several times, but ignoring my if statement? I tried hacks like (int) or using == instead of ===, but to no avail.

use_as_ref_select is a pagefield, btw.

value type: "single page Page or boolean false when none selected"

input: single select, with a default value and required

Any ideas?

Share this post


Link to post
Share on other sites

You compare === and page object to a int.

if( ($pages->get($useAsRef->id)) === 10132 ) {

Why do you get() the page you already have? Strange. It's not neccessary to get the page again as you have it already as $useAsRef.

if($useAsRef->id == 10132) {
  • Like 1

Share this post


Link to post
Share on other sites

I tried that as well, but I always got an error. I switched now from findIDs to findMany, and my first line after foreach() is now

$useAsRef = $m->use_as_ref_select->id;

Later on, I use $matches->remove() and $matches->add() to get the desired new Page array.

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By EyeDentify
      Hello dear PW gurus.

      I have stumbled over a strange error that i all of sudden got when trying to upload an image to a images field on a page.
      There where images allready stored in the field that i wanted to keep, but during the upload the error apear and after that all images are gone from the field and i can´t upload any, i just get the error every time.
      I am running ProcessWire 3.0.153 dev.
      Update:
      After looking in the assets folder i find the folder for the page and the image files seems to be there including the ones i tried to upload when the error occured.
      But they don´t show up in the images field in the page editor.
       
      The error reported:
       
      SQLSTATE[01000]: Warning: 1265 Data truncated for column 'ratio' at row 1 And here is a screenshot of the event:

      The TracyDebugger Error reporting:

      I hope you fine folks could point me in a direction.
      But it seems our old pal set_time_limit() is back.

      Regards, EyeDentify
    • By Spyros
      Hello
      I'm having a strange issue with the $page->find(), for some reason I'm missing some of the pages from the results. I found then that I was missing all the pages with the same "PAGE NAME". Is it a bug or am I missing something?
      PS 
      If I change the "PAGE NAME" of one of the missing ones then I'm retrieving the page without any problem.
      Thank you
    • By benbyf
      Hi, Looking to create form elements on a page–some input with a colection of form inputs and the appropriate labels and variables for that input. I've used ProForms in the past and rolled out my own when creating simply one off forms, but I wonder if anyone has found a good way of allowing form creation on page editing so that clients can adhocly make and edit forms?
      Thanks
    • By EyeDentify
      Hello Dear PW Gurus.

      Hope you fair well in these Corona Crisis times.

      Anyhow, i have a problem with a Cryptic error message that shows up when i am trying to Delete images out of a Images field.
      The Error message does say a little but it does not make sense to me why i can´t delete the images because of it.

      Is it some permission issue perhaps?

      I will attach screenshots of it and what i did prior to it.
      I am from sweden so ignore the funny words here and there.

      Hope you all can send me on the right track.
      i am running PW 3.0.139 on this install.

      Step1:

      Step 2: Select the images with the trashcan symbol

       
      Step 3: Cryptic Error message

      I am scratching my head on this one, have not seen this before.

      Thankfull for all help.
      /EyeDentify
    • By ngrmm
      i have a event-page with a table.
      first column in this table is page-reference-field
      how can i find out if a urlSegment matches one of the rows having the same page-reference-field (page-id)?
      // all guests $allguests = new pageArray(); foreach($page->event_guests_table as $event_guests_table_row) { $allguests->prepend($event_table_row->guest); } // echo allguests would output: 1101|1102|1103|… // domain.tld/event/1101/ // show content if guest is in the table or redirect if($input->urlSegment1 ~= $allguests ) { // show content } else { // redirect }  
      which selector operator do i have to use? the one above does not work
×
×
  • Create New...