views:

41

answers:

2

I have a model, view and controller not interacting correctly, and I do not know where the error lies.

First, the controller. According to the Code Igniter documentation, I'm passing variables correctly here.

function view() {
        $html_head = array( 'title' => 'Estimate Management' );

        $estimates = $this->Estimatemodel->get_estimates();

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

The model (short and sweet):

function get_estimates() {
        $query = $this->db->get('estimates')->result();
        return $query;
    }

And finally the view, just to print the data for initial development purposes:

<? print_r($estimates); ?>

Now it's undefined when I navigate to this page. However, I know that $query is defined, because it works when I run the model code directly in the view.

+2  A: 

The documentation shows that the object you pass to the view must be an associative array.

$data = array(
   'estimates' => $estimates
);

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

Docs here

seanmonstar
+2  A: 
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('estimates/view', $estimates);

You're loading the return of $this->Estimatemodel->get_estimates() as the array of view variables. In other words, all the children of $estimates (assuming it can be treated as an array) are available in your view. But not the parent element.

The key here is when loading a view the second parameter needs to be an array of values, not just a single value.

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

That should get the result you're looking for, in fact, you're already doing that for the html header view. Even though that view only has one variable, it's passed as the single element of an array:

$html_head = array( 'title' => 'Estimate Management' );
$this->load->view('html_head', $html_head);
Tim Lytle