Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
So, while normally I just code JSON feeds for some of the AJAX applications on my site manually using Couch tags, I'm having a bit of an issue where using the built-in json_encode feature from PHP would make my life better.

Problem - I keep getting an error that the closing PHP tag doesn't exist. Here's the code:

Code: Select all
<cms:set jsonResult="<cms:php> 
   $jsonFeed = $array();
   <cms:pages>
      array_push($jsonFeed, ["progID"=>k_page_id,"presenterName"=>presenter_name,"presenterAddress"=>presenter_address,
"presenterPhone"=>presenter_phone,"presenterEmail"=>presenter_email,
"contactPreference"=>contact_preference,"progName"=>k_page_title,"progBranch"=>desired_location,
"targetAudience"=>target_audience,"feeNeeded"=>fee_radio,"feeAmount"=>fee_amount,
"feeNegotiable"=>fee_negotiable,"pastPresentations"=>past_presentation,
"pastLocations"=>past_when_and_where,
"profReferences"=>professional_references,"progDesc"=>program_description]);
   </cms:pages>

   echo(json_encode($jsonFeed));
   </cms:php>" />

<cms:show jsonResult />
It would probably help if I used proper PHP.

Changed $array() into array() - now it works.
Shouldn't you also be actually outputting the values from the Couch editable regions?
I believe following should work -
Code: Select all
<cms:capture into='jsonResult'>
    <cms:php>
        $jsonFeed = array();
        <cms:pages>
            array_push( $jsonFeed,
                array(
                    "progID"=>"<cms:show k_page_id />",
                    "progName"=>"<cms:show k_page_title />",
                    "progDesc"=>"<cms:show my_richtext />"
                )
            );
        </cms:pages>

        echo(json_encode($jsonFeed));
    </cms:php>
</cms:capture>

<cms:show jsonResult />

It is better to use cms:capture instead of cms:set as it saves the hassle of taking care of enclosed quotes characters.

Also if any of the Couch editable regions could contain double-quotes character (e.g. in textareas with 'no_xss_check' on), you'd want to use cms:addslashes
Code: Select all
...
"progDesc"=>"<cms:addslashes><cms:show my_text /></cms:addslashes>"
...

Hope it helps.
Thanks, KK! I didn't know about the addslashes tag, and that actually solves another problem I'd had elsewhere.
4 posts Page 1 of 1