ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Can you use an entry custom field inside PHP running in a template?

March 25, 2011 12:54pm

Subscribe [2]
  • #1 / Mar 25, 2011 12:54pm

    John St-Amand

    865 posts

    Hi all - I suspect this question is going to be a bit basic, but it’s because I’m not knowledgable when it comes to PHP.  Can you use a custom field from an entry within PHP that’s in a template?  I have some PHP running inside a template now and everything is fine except if I try to use a custom field within the PHP itself.  I’ll explain by showing the code here:

    <?php
    } else {
    
    
    //structure information for JSON Serialization
    $recipient['@recipientType'] = 'CUSTOMER';
    $recipient['firstName'] = $_POST["Fname"];
    $recipient['lastName'] = $_POST["Lname"];
    $recipient['gender'] = $_POST["gender"];
    $recipient['address1'] = $_POST["address1"];
    $recipient['address2'] = $_POST["address2"];
    $recipient['city'] = $_POST["city"];
    $recipient['province'] = $_POST["province"];
    $recipient['postalCode'] = $_POST["postalCode"];
    $recipient['language'] = 'E';
    
    $fulfillment['@programId'] = 51;
    $fulfillment['@fulfillmentType'] = "DOWNLOADABLE";
    $fulfillment['amount'] = "1";
    $fulfillment['recipient'] = $recipient;
    
    //convert to JSON
    $json = json_encode($fulfillment);
    
    require_once "http://www.stitesting.com/test/RestClient.php";
    
    //create Client object for sending the query
    $rest = new RestClient("vezco","apple23");
    $rest->setDestination("https://dev001-srv.samplingtechnologies.com/api/fulfillment/");
    $rest->setContentType('JSON');
    $rest->setAcceptHeader('JSON');
    //actual sending of query
    $rest->post($json);
    
    //we want to be sure that we get a success. You could probably do more error checking, for example if a 400 code was returned (missing data)
    if($rest->getResponseCode() != 200) {
    ?>
    We are sorry, but we are currently encountering technical difficulties. Please try again later.
    
    
    <?php
    
    //DEBUG Only. If we have an error, spit out the error details so we can debug
    echo $rest->getResponseBody();
    
    } else {
    
    //Decode the result so we can get the downloadUrl property
    $result = $rest->getResponseBody();
    $jsonResult = json_decode($result);
    
    ?>
    
    {product_registration_thanks}
    <h1>{thanks_title}</h1>
    {thanks_body}
    Click on <a >{'downloadUrl'}; ?>'>this link</a> for your electronic sample.
    {/product_registration_thanks}
    
    <?php
    }
    }
    ?>


    I have this entire segment of the page surrounded by an entries tag with a single entry ID parameter.  It includes a custom field for “programId”, which is a declared variable in the PHP in the JSON serialization for this form submission.  Right now, it’s hardcoded to “51” but I’d like for it to read the value that’s entered in the entry.  But when i try to substitute the custom field name in place of the hard-coded “51” my page goes blank.  I’m suspecting that there must be some tags or characters that must surround the custom field name in order to use it this way, is that it?  To protect it from being misidentified as a php function?

    Thanks!

  • #2 / Mar 25, 2011 2:43pm

    Kay Ashaolu

    23 posts

    Hey John,

    I believe in order to use custom field tags within php, you will need to put php parsing stage to “output” so that the php script will have that value available since the ee tags would have been processed before the php script runs. You can do this in the manage templates link.

    Also if you go to your root index.php and set $debug = 1, you should be able to see the errors you are running to (instead of a blank screen).

    Hope this helps

  • #3 / Mar 25, 2011 2:54pm

    John St-Amand

    865 posts

    Ah, thanks so much, that seems to have worked.  I thought at first i would have to have parsing turned inward but I guess there’s nothing in the PHP that has to be parsed before the EE tags.

  • #4 / Mar 25, 2011 3:06pm

    Kay Ashaolu

    23 posts

    Glad to help!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases