Search code examples
phpcodeignitercodeigniter-3codeigniter-query-builder

Displaying data which was passed from Controller to View in codeigniter


I am having a problem with displaying array which was retrieved from database table This is how my array looks.

Note i selected from my database table

"WHERE pro_cat_id = 36" ;

Array
(

 Array
  (
    [0] => Array
    (
        [pro_cat_id] => 1
        [cat_name] => men
    )

[1] => Array
    (
        [pro_cat_id] => 2
        [cat_name] => women
    )

[2] => Array
    (
        [pro_cat_id] => 3
        [cat_name] => Gilamlar
    )

[3] => Array
    (
        [pro_cat_id] => 4
        [cat_name] => kids
    )

[4] => Array
    (
        [pro_cat_id] => 5
        [cat_name] => others
    )

[5] => Array
    (
        [pro_cat_id] => 27
        [cat_name] => game
    )

)

This is My Controller's index code

    public function index()
    {
        $res = $this->Categories_model->getCategory();
        //  ;
        // print_r($data);
        $data = $res;

            echo "<br><br><br><pre>";
            print_r($data);
            echo "</pre>";
        $this->load->view('categories',$data);
    }

where getCategory is a model function to retrive all row from database table

I tried echo it as ($pro_cat_id and $cat_name )

How can i use this data in view?

How to pass Multidimensional array to view and use it there?


Solution

  • Hope this will help you :

    Your index method should be like this :

    public function index()
    {
        $data['categories'] = $this->Categories_model->getCategory();
        // print_r($data);
        $this->load->view('categories',$data);
    }
    

    In your categories view page use foreach loop like this :

    <?php 
      if (! empty($categories))
      {
        foreach ($categories as $category)
        { 
          echo $category['pro_cat_id'];
          echo $category['cat_name'];
        }
      }
    
    ?>