我在codeigniter flashdata中保存一个数组
//Show success message
$data = array(
'message' => 'My message',
'message2' => 'New Message'
);
$this->session->set_flashdata($data);
也尝试过
$this->session->set_flashdata('myData', $data);
重定向页面后,我试图检索为
echo $this->session->flashdata('message');
这甚至不适合我
print_r($this->session->flashdata('myData'));
不管用
如何将flashdata设置为数组?
将flashdata保存在数组中,如:
$data = array(
'message' => 'My message',
'message2' => 'New Message'
);
$this->session->set_flashdata("someone",$data);
redirect("home","refresh");
And in view:
print_r($this->session->flashdata('someone'));
示例:来自另一篇文章。
样品1
if($insert_status){
$notification = "Record Inserted";
} else {
$notification = "Insertion Failed";
}
$this->session->set_flashdata('notification', $notification);
redirect('controller/method','refresh');
样本2
$notification = array(
'message' => 'test',
'message_one' => 'text'
);
$this->session->set_flashdata('notification', $notification);
redirect('controller/method','refresh');
How to use codeigniter flash variable?
http://www.codeigniter.com/user_guide/libraries/sessions.html
您必须将回显的代码放在您重定向到的控制器的视图中。
<?php echo $this->session->flashdata('notification');?>
引导
<?php if ($this->session->flashdata('notification')) { ?>
<div class="alert alert-success">
<i class="fa fa-check-circle"></i> <?php echo $this->session->flashdata('notification'); ?>
</div>
<?php } ?>
对于你的情况,当你使用$this->session->set_flashdata('myData', $data);
试试这个
$myData = $this->session->flashdata('myData');
echo "Message: ".$myData['message'].PHP_EOL;
echo "Message2: ".$myData['message2'];
会解决你的问题。
在控制器
$this->session->set_flashdata('message_name', 'This is my message');
redirect(base_url(),$data);
在视图页面
<?php echo $this->session->flashdata('message_name'); ?>