1

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


     }
2
  • What error are you getting? Commented Jun 14, 2013 at 15:36
  • you must start a session on each page where you actually want to access session values. Commented Jun 14, 2013 at 15:48

2 Answers 2

3

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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million guys,ts my first time posting and m grateful how helpful n fast ths thng z :-)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.