views:

170

answers:

2

Hello, I have a controller with an index function as follows:

function index() 
{ 
    $this->load->model('products_model');
    $data['product'] = $this->products_model->get(3); // 3 = product id
    $data['product_no'] = 3;
    $data['main_content'] = 'product_view';
    //print_r($data['products']);
    $this->load->view('includes/template', $data);
}

This is the get function in the products_model file

function get($id)
{
    $results = $this->db->get_where('products', array('id' => $id))->result();
    //get the first item
    $result = $results[0];
    return $result; 
}

The products table contains fields such as name, price etc. Please can you tell me how to output variables from $data['product'] after it is passed into the view? I have tried so many things but nothing is working, even though the print_r (commented out) shows the data - it is not being passed into the view. I thought it may have been because the view calls a template file which references the main_content variable:

Template file contents:

<?php $this->load->view('includes/header'); ?>

<?php $this->load->view($main_content); ?>

<?php $this->load->view('includes/footer'); ?>

but i tried creating a flat view file and still couldn't access the variables.

Many thanks,

A: 

your $data array is "ripped" into single variables within the view.

print $product;
print $product_no;
print $main_content;
Tim
Yes but they are also listed into $this->load->_ci_cached_vars for use within child views that have no data array of their own.
Phil Sturgeon
Thanks for your help. I have removed that template file - now the view is a flat html file. I have also changed the controller index function to function index() { $this->load->model('products_model'); $data['product'] = $this->products_model->get(3); // 3 = product id $this->load->view('test', $data);} however, I cant access the array variables in the view. What would the correct code be? I have tried echo $this->$price, echo $data['price'] - I'm sure this is dead simple, what am i doing wrong?
Matt
A: 

Handling headers & footers this way sucks and get's unmanageable pretty quickly.

Try using my Template library, your data will be passed through to the product_view no problem.

Phil Sturgeon
Thanks, I'll take a look at this Phil.
Matt