Forum for discussing general topics related to Couch.
11 posts Page 1 of 2
Is it possible to access the variables for a page in PHP?
I needed very specifically customized comments so I decided to write them myself, but in order to display them I need to access the comments with my own PHP, and I need to use the k_page_id variable to do so.
Is this variable possible to access without the <cms:show k_is_page /> tag?
Cheers,
Seth.
If you use the cms:php tag, then you'll have access to all Couch variables in the current context.
Code: Select all
<cms:php>echo "The page id is: <cms:show k_page_id/>";</cms:php>

http://docs.couchcms.com/tags-reference/php.html
I tried doing this, but not only did it make my code completely unreadable due to the errors it caused, but it required me to change lots of my code as it is all dependent on other things.
Is there an easier way to use the $_GET['pname'] to find the id without querying my database?
Cheers
llamadillo wrote: Is there an easier way to use the $_GET['pname'] to find the id without query my database?
Cheers


As far as I am aware, no.

Because of the way couch executes: normal PHP runs, then couch runs.
This means that there is no context until AFTER couch has ran (which is why the <cms:php> exists.)

I think you'll have to settle for accessing the database outside of the scope of couch, if you really cannot use cms:php.

Out of curiosity, what errors do you get when using the cms:php tag?
Image
I'm new to php, but this thread looks great and interesting!
Could someone explain in plain what is going on actually? Thx!
Sorry, my mistake. This isn't the page id, after all.
Code: Select all
<?php
    $page_id = intval($DB->result);
    echo $page_id;
?>
Please excuse the terseness of my earlier post. Let me offer some explanation.

@Bartonsweb is right about the order of operations between Couch and raw php. It's generally advantageous to use the cms:php tag when using php in your templates because you have access to all of the Couch variables available in the current context.

But from the very first line, <?php require_once( 'couch/cms.php' ); ?>, Couch sets the stage by initiating a ton of variables, functions, and classes. There is no such variable as k_page_id yet, but the information is there.

To discover all of the available variables I added this code to a Couch template.
Code: Select all
<?php
    $couch_vars = get_defined_vars();
    print_r($couch_vars);
?>

I dug through the resulting text to find the variable I wanted. I had to convert a string to just the page id, so I used intval to do that. The result is stored in $page_id for further use.
:oops: :oops: Well, sorry. That didn't work after all. I think it was just a coincidence that the page id of the template I was testing on happened to show up among the variables.

With additional testing, the actual page id doesn't seem to be among these initial Couch variables.
I tried doing this, but not only did it make my code completely unreadable due to the errors it caused, but it required me to change lots of my code as it is all dependent on other things.
Now that it is established that using <cms:php> is the only available way forward, let us try and remove the problems you mentioned to make things easier.

First of all, let us work on the existing PHP code that you have.
Chances are that it would all be in a global scope. If so, refactor it to move all the code to within a function and then make a call to that function e.g. if the original was -
Code: Select all
<?php
    blah..
    blahh $page_id..
    ..
    ..
    bla..
   
?>

Refactor it to make it as follows -
Code: Select all
<?php
    // move all the relevant code to within a function ..
    function my_func( $page_id ){
        blah..
        blahh $page_id..
        ..
        ..
        bla..
    }
   
    // invoke the function ..
    my_func( 120 );
   
?>

You might have to make some adjustments but those should be straightforward as we'd be dealing straight with PHP (as opposed to through Couch).

Once you have the existing PHP code working, we'll only have to port the single line function call to Couch as follows -
Code: Select all
<?php
    // move all the relevant code to within a function ..
    function my_func( $page_id ){
        blah..
        blahh $page_id..
        ..
        ..
        bla..
    }
?>


<cms:php>
    // invoke the function from within Couch ..
    my_func( <cms:show k_page_id /> );
</cms:php>

As you can see, the bulk of the code remains in PHP with only a single line invoked from Couch. This would make it very simple to debug things if they go wrong as there is not much left to debug.

Also, in case you happen to use a PHP debugger this approach would make it possible to place breakpoints in the PHP code.

Please try this method and do let us know if it helps.
This worked! Thanks.

I had to require my mysql connection file again within the <cms:php> tag.
The functions included outside of the tag were still accessible however, what's the cause of this?

Thanks for the help!
Seth.
11 posts Page 1 of 2
cron