We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Creating PHP utility methods

Development and Programming

PA Webmaster's avatar
PA Webmaster
4 posts
14 years ago
PA Webmaster's avatar PA Webmaster

I have a fairly significant amount of PHP code that can’t be made into an add-on (it has to be accessible as PHP). Right now, I have everything organized using the EE Snippets system, but I’m wondering if there’s a better place to store this code so it’s accessible from a template.

Secondly, I’m having trouble accessing the db object from within a function. Inside the template at the top level, I have $db = $this->EE->db, which I’m passing as a parameter to my functions. This causes the page to fail to load entirely. Is there a standard way for doing this? Thanks!

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Hi Kenneth,

I have a fairly significant amount of PHP code that can’t be made into an add-on (it has to be accessible as PHP)

I’m curious what you mean by this. Why can’t it be made into an add-on? What exactly needs to be accessible “as PHP”?

I have a fairly significant amount of PHP code…

This is one main reason I’d recommend reconsidering if an add-on would work. Parsing PHP at the EE template level will be more resource intensive than if it were an add-on.

Secondly, I’m having trouble accessing the db object from within a function. Inside the template at the top level, I have $db = $this->EE->db, which I’m passing as a parameter to my functions. This causes the page to fail to load entirely. Is there a standard way for doing this?

Without seeing the code it’s hard to say. I did test the following code (basically from the docs) in an EE template with PHP enabled and it worked as expected:

<?php

$query = $this->EE->db->query("SELECT channel_name FROM exp_channels");
echo $query->num_rows();

?>
       
PA Webmaster's avatar
PA Webmaster
4 posts
14 years ago
PA Webmaster's avatar PA Webmaster

Related thread: http://ellislab.com/forums/viewthread/189220/

The short story is that I’m using PHP to generate start_on/stop_before dates that get passed to the channel tag. So far as I’m aware, you can’t use an exp tag as a parameter for those values. Therefore the dates need to be generated as PHP, and not as an EE tag. Of course, I’m not thoroughly experienced in EE, so advice here would be much appreciated.

DB issue example:

<?php

function get_newest_post($db) {
$db->from("channel_titles")->where("channel_id", 1)->order_by("entry_date", "desc")->limit(1);
$query = $db->get();
return $query->row();
}
?>
       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
14 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
Related thread: http://ellislab.com/forums/viewthread/189220/ The short story is that I’m using PHP to generate start_on/stop_before dates that get passed to the channel tag. So far as I’m aware, you can’t use an exp tag as a parameter for those values. Therefore the dates need to be generated as PHP, and not as an EE tag. Of course, I’m not thoroughly experienced in EE, so advice here would be much appreciated. </code></pre>

I see. Using the example code from your previous thread, what about adding inward parsing with your custom plugin. Consider this:

{exp:channel:entries start_on=”{exp:date_util:start_date}” stop_before=”{exp:date_util:stop_date}” parse="inward"}

Note the extra “parse” parameter at the end. That tells EE to parse inner EE tags before completing that tag. I created a simple test locally and it seems to work just fine.

DB issue example:
<?php

function get_newest_post($db) {
$db->from("channel_titles")->where("channel_id", 1)->order_by("entry_date", "desc")->limit(1);
$query = $db->get();
return $query->row();
}
?>

I’d recommend opening your site’s index.php file and switching the $debug variable to 1 so you can see error messages rather than a white screen. I tested the following code in a template with PHP enabled and it worked as expected:

<?php

function get_newest_post($db)
{
    $db->from("channel_titles")->where("channel_id", 1)->order_by("entry_date", "desc")->limit(1);
    $query = $db->get();
    return $query->row();
}

print_r(get_newest_post($this->EE->db));

?>
       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.