简体   繁体   中英

Codeigniter: Accessing a array in view

I am completely new in the codigniter i just started day befire Yesterday i am having a problem Here is my snippet of my controller code

        $allcalldetails_array = array(
                        'id' => $row->id,
                        'customer_id' => $row->customer_id
        );

        $this->session->set_userdata('logged',$allcalldetails_array);

I want to iterate the $allcalldetails_array in my views please tell me the way to do this

i tried iterating logged but could not get anything .

If i am printing the array in my views like print_r($allcalldetails_array); but this is also disappointing me .Please help me to get back on track .

Thanks

To get the data from session your need to do the following:

// controller logic
$logged = $data['logged_for_view'] = $this->session->userdata('logged'); // since 'logged' is the name you set it to
// more controller logic
$this->load->view('view_name', $data);

// in view
var_dump($logged_for_view); // this is the key of the $data variable you assigned for the view

If you would like data to be send to your view as variables, you should add the corresponding data to your view load. Controller:

$logged = $this->session->userdata('logged');
$this->load->view('viewfile', $logged);

View

print "logged id:".$id;

best regards. Jonas

Its not necessary for session variables to be parsed via controller, access it on view directly:

$logged = $this->session->userdata('logged');
print_r($logged);

Regarding the CodeIgniter's documentation :

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function.

Then you could use something like :

// Controller side

$data['allcalldetails_array'] = array(
    'id' => $row->id,
    'customer_id' => $row->customer_id
);
$this->load->view('your_view', $data);


// View side

print_r($allcalldetails_array);

// Loop through your array
foreach ($allcalldetails_array as $detail){
    // Do something with your $detail.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM