Jump to content

page->get(field) where field is a GET var - wont work


Jochen Kremer
 Share

Recommended Posts

Hi there,

i realy dont understand the problem - im passing a var to a php with is a string with a correct field name.
When i try to load the data of that field .... it returns nothin. If i just set the field name in the code as string°
it works ... look at the comments in the code-snippet.

Anyone can explain me wh yythis dosent work?
Thanks && byebye Jochen 

<?php namespace ProcessWire;

    require_once '../index.php';

    $targetGrid = $_GET["targetGrid"]; // this is "desktop_grid_placed" 
    echo $targetGrid."<br>"; // echos "desktop_grid_placed"
    echo gettype($targetGrid)."<br>"; // echos string
    
    $itemsData = wire()->pages->find("template=portfolio-item");

    for ($i=0; $i < count($itemsData); $i++) 
    {
        $p              = $itemsData[$i];
        $_gridPlaced    = $p->get($targetGrid); // returns nothing
        echo $_gridPlaced." <br>"; //echos only the <br> tag
    }
    echo $p->get("desktop_grid_placed"); // returns a value - with is okay

 

Link to comment
Share on other sites

You should do another nice Debugging like you have done with your GET string, to see whats going on:

	$itemsData = wire()->pages->find("template=portfolio-item");
	echo "<pre>";
	echo $itemsData InstanceOf PageArray ? "is PageArray\n" : "NOT a PageArray\n";
	
	if($itemsData InstanceOf PageArray && count($itemsData)) {
		// we have at least one page in the collection, get the first one for testing
		$p = $itemsData->first();
		echo $p InstanceOf Page ? "is Page\n" : "NOT a Page\n";
		if($p InstanceOf Page && $p->viewable()) {
			echo $p->fields->has("$targetGrid") ? "Field $targetGrid exists\n" : "Field $targetGrid does NOT exist\n";
			if($p->fields->has("$targetGrid")) {
				var_dump($p->$targetGrid);
			}
		}
	}
	echo "\n</pre>";

 

If you not already know it, here are a nice overview: http://cheatsheet.processwire.com/

 

PS: instead of $_GET you better should use wire('input')->get->targetGrid, or when in template scope: $input->get->targetGrid. Also don't forget to sanitize input values!

$targetGrid = wire('sanitizer')->fieldName( wire('input')->get->targetGrid );

 

  • Like 4
Link to comment
Share on other sites

Hi Horst,

thanks a lot - this works - and i learned a lot new possibilities .)
I am always looking into the cheatsheet & documentation. But
iam relatively new to php ... because of that iam often not sure
how to interprete what i read there ... i  think its time for a basic
book about php in general oO

Thanks and && byebye Jochen 

PS: there was missing at semicolon

$p = $itemsData->first()
  • Like 1
Link to comment
Share on other sites

2 hours ago, Jochen Kremer said:

PS: there was missing at semicolon


$p = $itemsData->first()

Thanks for mentioning. I have updated it in the post. (It was written in the browser, so without syntax checker :) )

  • 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...