Codeigniter - accessing variables from an array passed into a page.

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-04-07T11:41:38Z Indexed on 2010/04/07 11:53 UTC
Read the original article Hit count: 241

Filed under:
|
|
|

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,

© Stack Overflow or respective owner

Related posts about codeigniter

Related posts about variables