Take the 2-minute tour ×
ExpressionEngine® Answers is a question and answer site for administrators, end users, developers and designers for ExpressionEngine® CMS. It's 100% free, no registration required.

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!

share|improve this question
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 at 3:03

1 Answer 1

up vote 3 down vote accepted

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);
share|improve this answer
    
thx! Makes complete sense now. Need to build out the array, then pass. –  Bungdaddy Feb 6 at 1:50
    
Hey no problem! –  Dylan Feb 6 at 2:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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