开发者

Why isn't this model returning any values?

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);

    $da开发者_Python百科ta['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.


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.


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();


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');

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜