简体   繁体   English

Codeigniter:在视图中访问数组

[英]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 我想在我的视图中迭代$allcalldetails_array ,请告诉我这样做的方法

i tried iterating logged but could not get anything . 我尝试重复logged但什么也没得到。

If i am printing the array in my views like print_r($allcalldetails_array); 如果我在像print_r($allcalldetails_array);这样的视图中打印数组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 : 关于CodeIgniter的文档

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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