1

foreach ループを使用してデータベースから通知を受け取るコードイグナイターのモデルがあります。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);
        }


     }
4

2 に答える 2

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);
    }
于 2013-06-14T16:35:17.677 に答える
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);
}
于 2013-06-14T16:35:27.377 に答える