Welcome Guest, Not a member yet? Register   Sign In
Variables to the view.
#1

[eluser]Unknown[/eluser]
Hello to everyone.
I'm having a problem with passing variables to my view.
I have a template in my site, and I call it with this code:
Code:
$this->data = array(
   'main_content' => 'template_group/template',
   'header_variable' => array(
        'title' => 'Title',
        'js_load' => 'tinymce'
      ),
   'footer_variable' => '',
);
In the view where I'm working, I need a list of categories. I get the list with the category model: here's the model code.
Code:
function list_categories() {
    $query = $this->db->get('categories');
    if ($query->num_rows != 0) {

        foreach ($query->result_array() as $row) {
            $data['categories'][$row['id_categories']]['id'] = $row['id_categories'];
            $data['categories'][$row['id_categories']]['category_name'] = $row['category_name'];
            $data['categories'][$row['id_categories']]['id_parent'] = $row['id_parent'];
        }
        return $data;
    } else {
        return FALSE;
    }
}
On the controller I merge the returning value from the category_model and existing $this->data with an array_push function:
Code:
$this->load->model('category_model');
$this->cat_list = $this->category_model->list_categories();
array_push($this->data, $this->cat_list);
Finally this is the resulting array (obtained with print_r function):
Code:
Array
(
    [main_content] => officina/attivita
    [header_variable] => Array
        (
            [title] => Title
            [js_load] => tinymce
        )

    [footer_variable] =>
    [0] => Array
        (
            [categories] => Array
                (
                    [20] => Array
                        (
                            [id] => 20
                            [category_name] => Category name
                            [id_parent] => 0
                        )

                    [21] => Array
                        (
                            [id] => 21
                            [category_name] => Another category
                            [id_parent] => 0
                        )

                )

        )

)

But I don't know how to get categories in my view. What is wrong in my procedure?

Thanks


Messages In This Thread
Variables to the view. - by El Forum - 09-14-2011, 02:49 AM



Theme © iAndrew 2016 - Forum software by © MyBB