Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a model in codeigniter that gets notification from the database using foreach loop.i want to pass the values to session using set_userdata but unfortunately i cant pass multiple values in to my session,please help.below is my model

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);
        }


     }
share|improve this question
 
What error are you getting? –  James Jenkins Jun 14 at 15:36
 
you must start a session on each page where you actually want to access session values. –  Ghazanfar Mir Jun 14 at 15:48

2 Answers

add all the notifications to an array you instantiate prior to the foreach then add the array to session.

Also note if your using the standard codeigniter session there is a 4k limit on its size so this may not be the best approach.

Also, this is working on the assumption that your session class has been properly initialized...

I did my best to clean up your code.

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);
    }
share|improve this answer
 
Thanks a million guys,ts my first time posting and m grateful how helpful n fast ths thng z :-) –  bobin56 Jun 14 at 17:13

Try this,

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);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.