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

$this->EE->localize->now causes php error in plugin

Development and Programming

chrcollins's avatar
chrcollins
77 posts
15 years ago
chrcollins's avatar chrcollins

Added $this->EE->localize->now to a previously working plugin and now it throws a php error ….

Severity: Notice Message: Undefined offset: 0 Filename: libraries/Template.php Line Number: 3837

Code looks like this ….

function Exhibit()
    {
        // Make a local reference of the ExpressionEngine super object
        $this->EE =& get_instance();
     
        // get time 
        $utc_gmt = $this->EE->localize->now;

I see examples in other code but does not work for me. Any help would be appreciated.

EE Build: 20101020

[Mod Edit: Moved to the Development and Programming forum]

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
15 years ago
Mark Bowen's avatar Mark Bowen

Hiya,

Is that the only code that you have in the function inside your plugin?

If so then you are missing the closing } on your function. If not then have you tried a completely stripped down version of a plugin just to ensure that everything works as it should do? I don’t see anywhere that you are actually outputting the date variable so it may well be something else in your plugin which is causing the errors here.

Something like this just to test :

<?php


class Localize_plugin
    {

        public $return_data = '';


function Localize_plugin()
    {
        $this->EE =& get_instance();
        $date = $this->EE->localize->now;
        $this->return_data = $date;
    }


}
?>

If I run that in a template with {exp:localize_plugin} then I get the date sent back to the template without any errors. I’m just wondering if in your case perhaps there is something else going on within your code perhaps?

Best wishes,

Mark

       
chrcollins's avatar
chrcollins
77 posts
15 years ago
chrcollins's avatar chrcollins

Thanks for your reply. I did as you said and started cutting out all the other code. As I said this was a working plugin until I added this code to check the time. Anyway I reduced the code until I could focus in on the problem …

$opening_param = $this->EE->TMPL->fetch_param('opening');
if (($opening_param == 'current' && $this->EE->localize->now >= $row['start_date'])
      || ($opening_param == 'scheduled' &&  $this->EE->localize->now < $row['start_date'])
      ||  $opening_param == '')
    {
            
     }

If I changed the >= sign in the IF statement to anything other than >= the error went away. So I tried a number of things. One thing I did was the following ….

$opening_param = $this->EE->TMPL->fetch_param('opening');
$foo = $this->EE->localize->now;
$bar= $row['start_date'];
if (($opening_param == 'current' && $foo >= $bar)
      || ($opening_param == 'scheduled' &&  $this->EE->localize->now < $row['start_date'])
      ||  $opening_param == '')
    {
           
     }

… and that worked. So then I re-substitued for $bar and there was no error. I re-substituted for $foo and it worked too. The problem has now gone away.

$opening_param = $this->EE->TMPL->fetch_param('opening');

if (($opening_param == 'current' && $this->EE->localize->now >= $row['start_date'])
      || ($opening_param == 'scheduled' &&  $this->EE->localize->now < $row['start_date'])
      ||  $opening_param == '')
    {
           
     }

Maybe moving the fetch_param away from the IF did something? Maybe some hidden character on the line?

Anyway thanks for the suggestions!!

       
chrcollins's avatar
chrcollins
77 posts
15 years ago
chrcollins's avatar chrcollins

I added some test cases for the plugin and the problem came back. For anyone left reading this post I see where the bug comes.

Here is the code that is screwing up ….

$query_exhibit = $this->EE->db->query($sql);
        
        $results = array();
        $result_count = 0;
        foreach($query_exhibit->result_array() as $row_key => $row)
        {

            if ($opening == '' ||
                ($opening == 'current' && $this->EE->localize->now >= $row['start_date']) ||
                ($opening == 'scheduled' && $this->EE->localize->now < $row['start_date'])    )
            {
                $result_count++;
                $results[$row_key]['title'] = $row['title'];
                $results[$row_key]['url_title'] = $row['url_title'];
                $results[$row_key]['artist_rel'] = $row['artist_rel'];

                ....etc ....
             }

Inside the foreach, if for what ever reason the IF statement fails a row gets skipped and it causes the Template parser to cough up the error I described. This is bug in my code. I cut and pasted the code that sets the variables from another plugin I did. With that plugin there was no conditional that owuld prevent a row from being added to the tagdata so it does not have this issue.

I should be pushing the result row onto the results array. That fixes the php error.

This thread can be closed.

       

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.