Forum

Thread tagged as: Question, Problem

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.

Bruce Reynolds

Bruce Reynolds 0 points

  • 3 years ago
Simon Clay

Simon Clay 127 points

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/

Drew McLellan

Drew McLellan 2638 points
Perch Support

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

case stristr($trimmed, 'Western Australia'):
Drew McLellan

Drew McLellan 2638 points
Perch Support

What does this output?

print_r(perch_content_custom("State Selection", [
'skip-template' => true
]));

I got the below - I also tried creating a new Perch content area State Selection 2 to see if there was some naming bug

Array
(
    [0] =&gt; Array
        (
            [_id] => 107
            [state_selection] => Western Australia
            [_page] => *
            [_pageID] =>; 103
            [_sortvalue] => 1000
        )

)
Array
(
    [0] =&gt; Array
        (
            [_id] => 108
            [state_selection] => Western Australia
            [_page] => /centres/child-care-centre-perth.php
            [_pageID] => 103
            [_sortvalue] => 1000
        )

)

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.

$state = perch_content_custom("State Selection", [
                'skip-template' => true
                ]);

$my_string = $state[0]['state_selection'];

                perch_content_custom('Centre Details', array(
                    'page'=>'/centres/*',
                    'template'=>'centre-index-list.html',
                    'filter'=>'centre_state',
                    'match'=>'eq',
                    'value' => $my_string,
                    'sort' => 'centre-name',
                    'sort-order' => 'ASC'
                    ));