我在codeigniter中有一个模型,它使用foreach loop从数据库获取通知。我想使用set_userdata将值传递给会话但不幸的是我无法将多个值传递给我的会话,请帮助。我是我的模型
function get_user_notifications($userID){
$this->db->select()->from('messages')->where('receiverID',$userID);
$query = $this->db->get();
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$notification=$rows->notification;
$messageID=$rows->messageID;
// $rows->messageID =>,
$this->session->set_userdata('mynotifications',$notification);
}
}
答案 0 :(得分:3)
将所有通知添加到您在foreach之前实例化的数组,然后将该数组添加到会话中。
另请注意,如果您使用标准codeigniter会话,其大小限制为4k,因此这可能不是最佳方法。
此外,这是假设您的会话类已正确初始化...
我尽力清理你的代码。
function get_user_notifications($userID){
$this->db->select()->from('messages')->where('receiverID',$userID);
$query = $this->db->get();
$all_notifications = array();
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$all_notification[]=$rows->notification;
$messageID=$rows->messageID;
}
$this->session->set_userdata('mynotifications',$all_notification);
}
答案 1 :(得分:0)
试试这个,
function get_user_notifications($userID){
$this->db->select()->from('messages')->where('receiverID',$userID);
$query = $this->db->get();
$notification='';
if($query->num_rows()>0)
{
foreach($query->result() as $rows)
{
//add all data to session
$notification.=$rows->notification.',';
$messageID=$rows->messageID;
// $rows->messageID =>,
}
}
$this->session->set_userdata('mynotifications',$notification);
}