Jump to content

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


hellomoto
 Share

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.
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

  • 2 years later...

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?

Link to comment
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
Link to comment
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.

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

×
×
  • Create New...