开发者

Codeigniter - accessing variables from an array passed into a page

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,


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

print $product;
print $product_no;
print $main_content;


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜