2

I am getting this very strange error "Cannot use a scalar value as an array" neither google god nor stuffs on SOF helped me this what i am trying to do is i have already created user session[with session data username and password] now after user login on my_profile page i am appending some session data like user_id then i start getting this error??? Any help or Hints

//controller

<?php 


      class My_profile extends CI_Controller {

      public function __construct()
         {
              parent::__construct();
              $this->load->library('session');
              $this->load->helper('form');
              $this->load->helper('url');
              $this->load->helper('html');
              $this->load->database();
              $this->load->library('form_validation');
        //    $this->is_login();
         }

      public function index () {

      if($this->session->userdata('is_login')) {

       $session_data = $this->session->userdata('is_login');
       $session_data['user_id'] = $this->session->userdata('user_id');
       $this->session->set_userdata("is_login", $session_data);

      $this->load->model('My_profilee');
      $data['query']=$this->My_profilee->view_data();
      $this->load->helper('url');
      $this->load->view('header2');
      $this->load->view('my_profile');
     // $this->load->view('footer');
     }else {
        echo "you don't have permission to access this page <a href=../Homecontroller/index/>Login</a>";
        die();

     }
      }



}

      ?>

//model

<?php 


      class My_profilee extends CI_Model {

       function __construct()
     {
          // Call the Model constructor
          parent::__construct();
          $this->load->database(); 
     }


      public function view_data() {


        //echo $this->session->userdata('user_id');
        $query=$this->db->get('tbl_usrs');
        return $query->result();

      }
}

      ?>

what i am really trying to do is to add user id [from databse] of user after login into session data and then display user information according to user id??

1 Answers1

0

This is pretty straight forward.

You have an assignment wherein a single value, be it an integer or string, is assigned to a variable:

$session_data = $this->session->userdata('is_login');

This is now a scalar value. it is unmutable unless redefined in context. You cannot apply an array value to an existing scalar value, you must instantiate a new array, and apply the value as an index, then add the new item as an index to the new array.

$session_array['session_data'] = $this->session->userdata('is_login');

And continue assigning variables to this array and so forth.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • you mean this line i am getting error on this line $session_data['user_id'] = $this->session->userdata('user_id'); –  Aug 04 '15 at 22:43
  • @paritosh, check this: http://stackoverflow.com/questions/6019853/php-cannot-use-a-scalar-as-an-array-warning – sinisake Aug 04 '15 at 22:44
  • @paritosh that is correct as well, I missed that one. In any event, if you take an integer, say, `15`, and you try to add an index to it, you won't be able to. This is a scalar value, it can only be a string / integer, it cannot mutate into an object or an array, for instance. – Ohgodwhy Aug 04 '15 at 22:44
  • @Ohgodwhy, $data array is not problem here, here is problem: $session_data = $this->session->userdata('is_login'); $session_data['user_id'] = $this->session->userdata('user_id'); I guess that value of is_login is true/false, so it is not an array... – sinisake Aug 04 '15 at 22:46
  • @nevermind yes, I acknowledged that in my previous comment. Thank you. I'll adjust the answer to reflect this now. – Ohgodwhy Aug 04 '15 at 22:46
  • this is getting very confusing for me.... will you explain what i am doing wrong i can't able to understand what you have mentioned in answer –  Aug 04 '15 at 22:49
  • @paritosh It has been updated. Please review the answer now. – Ohgodwhy Aug 04 '15 at 22:52
  • 1
    @Ohgodwhy salute.... this is very simple concept but due to some misconception it becomes bigger... :) –  Aug 04 '15 at 22:59
  • @paritosh As this is a community driven on Q/A of a single question per thread, I would recommend that you create a new question with your concerns and another valued member of the community will see it and assist you. Best of luck! – Ohgodwhy Aug 04 '15 at 23:03
  • i have already created that but no response...... [link]http://stackoverflow.com/questions/31819266/adding-user-id-from-database-to-session-data-in-codeigniter –  Aug 04 '15 at 23:05