2

I'm trying to build VERY basic model->controller->view structure with CodeIgniter. I'd rather use models (and model functions, more importantly) and controllers than executing queries within each view.

First off, here's the model:

class Leads extends Model {

    function Leads()
    {
        parent::Model();
    }

    function fetch_leads()
    {
        $query = $this->db->get('leads', 10);
        return $query->result();
    }

}

Here's the controller, only relevant function being the one in question:

function view()
{

    $this->load->model('Leads', '', TRUE);

    $data['query'] = $this->Leads->fetch_leads();

$this->load->view('html_head');
$this->load->view('leads/view', $data);
$this->load->view('html_foot');
}

I think the problem is in this line, I may not be passing the data to the view correctly:

$data['query'] = $this->Leads->fetch_leads();

Now, the view:

print_r $data;

Did I mention basic? I'm just trying to get it to pull every field and print it from the database. Yes, the database is configured correctly, and yes, there is data in the database.

EDIT: The model wasn't auto-connecting to the database, so I added that parameter. The model still isn't returning values.

2
  • Any errors or warnings you can give? Commented Jan 27, 2010 at 0:11
  • How do I turn on error logging? Commented Jan 27, 2010 at 17:42

3 Answers 3

2

it's not gonna work the way you do it.

return $query->result() does return an array of rows. In order to correctly pass this array to view you need to do it this way:

$view_data = array(
    'leads' => $leads,
);
$this->load->view('leads/view', $view_data);

What happens at the View is, all $view_data array elements are converted into local (for the view) variables. Therefore what in your controller is $view_data['leads'] becomes just $leads in your view.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, +1 :) :) (using smiles to fill the required characters)
0

It may be a problem with how you are passing the data to the view. Try to put the $leads in an array.

Ex. $data['leads'] = $this->Leads->fetch_leads();

Comments

0

You must pass an associative array to the $this->load->view() method.

Try this:

$leads = $this->Leads->fetch_leads();

$data["leads"] = $leads;

$this->load->view('html_head');

$this->load->view('leads/view', $data);

$this->load->view('html_foot');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.