2

I am writing my first EE plugin and have finally hit a roadblock. Here is how it looks

        class Thank_you {

        public function __construct()
        {
            $this->EE = get_instance();

            $entries = $this->EE->db->from('channel_titles')->get();
    }
public function contacts()
    {

        $db2   = $this->EE->load->database('ext_db', TRUE);
        $query = $db2->select('FirstName, LastName, MealType')->order_by('id','DESC')->get('contact',10);

        $contacts = $query->result();

        return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $contacts);
    }

So the goal here is to pass an array of contacts to the template and iterate from there. All that I get with

{exp:thank_you:contacts}
        {FirstName}
    {/exp:thank_you:contacts}

Is an output of {FirstName}. Where am I going wrong on this.

Thanks!

  • 1
    Just a quick comment, I notice you're loading your entire channel_titles table in the constructor. If that's not being used, then you definitely want to get rid of that, since it's adding an extra query to your database. – Jeremy Gimbel Feb 6 '14 at 3:03
3

You need to loop through the query result and build an array containing your variables. Using the following code after your query should work.

if ($query->num_rows() == 0)
{
    return $this->EE->TMPL->no_results();
}

foreach ($query->result() as $row)
{
    $vars[] = array(
        'FirstName'     => $row->FirstName,
        'LastName'      => $row->LastName,
        'url_title'     => $row->MealType
    );
}

return $this->EE->TMPL->parse_variables($this->EE->TMPL->tagdata, $vars);
| improve this answer | |
  • thx! Makes complete sense now. Need to build out the array, then pass. – SuperNinja Feb 6 '14 at 1:50
  • Hey no problem! – Dylan Feb 6 '14 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.