Search code examples
codeignitermodel-view-controllercodeigniter-2

Can we pass array and object from controller to view at same time in ci?


i have object like this $interest

    Array
(
    [0] => Array
        (
            [item_id] => 7
            [item_name] => Jogging
        )

    [1] => Array
        (
            [item_id] => 8
            [item_name] => Entertainment
        )

)

and array like this $profile

Array
(
    [username] => admin
    [email] => 
    [fullname] => 
    [profile] => http://pickaface.net/includes/themes/clean/img/slide4.png
)

i have sent it to view like following

$this->load->view('users/profile',$profile,$interest);

but when pass like this i cannot retrieve $interest object. at profile view but if i pass only $interest i can get it. the problem is when i pass both at a time


Solution

  • $this->load->view function expects three parameters : (Loading Views)

    1. view uri (which you entered correctly)

    2. associative array(key=>value) containing data units to be retrieved at views end as $key

    3. boolean value (optional) that you can use to change the behavior of the function so that it returns data as a string (if set to true) or sends it to your browser(if set to false which is default)

    To send multiple data units you will have to use an associative array. Try sending the data like this:

    $data = array(
       'interest' => $interest,
       'profile' => $profile,
    );
    
    $this->load->view('users/profile',$data);
    

    In profile.php view file you can retrieve this data as $interest and $profile.