Forum
Trying to use php var as perch_content_custom 'vaule'
I can't see why this isn't working. On a page I have this
$selected_state = perch_content("State Selection", true);
And inside that content block there is one field, a selection as below
<perch:content id="state_selection" type="select" label="State" options="Western Australia, Victoria, Queensland, New South Wales, Northern Territory, South Australia, Tasmania" allowempty="false" required="true" encode="false" />
On the page, this seems to create a var with the correct string. When printing or echoing, it comes up as selected 'Western Australia'. When I try and do this further down the page - it doesn't work.
perch_content_custom('Centre Details', array(
'page'=>'/centres/*',
'template'=>'centre-index-list.html',
'filter'=>'centre_state',
'match'=>'eq',
'value' => $selected_state,
'sort' => 'centre-name',
'sort-order' => 'ASC'
));
Nothing comes up. If I use the string 'Western Australia' it's fine. If I set 'Western Australia' as a php var and use that it works. Why is my $selected_state var not working?
I've tried using srtval to make sure it's a string but that still didn't work.
Hi Bruce,
Not sure if this is the problem, but
centre-name
might cause issues, as dashes should not be used in Perch IDs.Can you enable debug to see if it highlights any problems https://docs.grabaperch.com/perch/configuration/debug/
Have you checked for trailing whitespace? Applying
trim()
to the string might help.Hi Simon - I didn't know that but it's been running fine for years and as mentioned, if I use just a normal string or a var I've set to be a string the custom code works.
Hi Drew - I tried trim, it still didn't work and even tried switching on the trimmed var to see if it contained the string but it didn't match on anything
What does this output?
I got the below - I also tried creating a new Perch content area State Selection 2 to see if there was some naming bug
I was expecting some sort of array for the result but echoing it seemed to just output the string.
So it seems that drilling down into the object allowed me to use the string in as a perch_content_custom var.