title: Codeigniter: Get userdata from another table using session
body: <p>I'm trying to print userdata from another table with session. The table with the session is "userlogin" while the table with the data is "userinfo"</p>
  
  <p>Here's what I've done so far</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function index()
      {
          if ( $this-&gt;session-&gt;userdata('logged_in') )
          {
  
              $session_data = $this-&gt;session-&gt;userdata('userinfo');
              $id = $session_data['id'];
              $this-&gt;load-&gt;model('Userdata_model');
              $result = $this-&gt;Userdata_model-&gt;get_user_data3($id);
  
  
              if($result==0)
              {
                  echo 'No user Found';
              }
              else
              {
                  $data['id']=$result;
  
                  $this-&gt;load-&gt;view("userdata_view", $data);
              }
          }
      }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>public function get_user_data3($id)
      {
      if (isset($this-&gt;session-&gt;userdata['logged_in'])) {
  
          $userid = ($this-&gt;session-&gt;userdata['logged_in']['id']);
      } else {
          header("location: http://localhost/test");
      }
  
          $query = $this-&gt;db-&gt;query("SELECT * FROM userinfo WHERE id='$id'");
  
          foreach ($query-&gt;result_array() as $row)
          {
              echo $row['fName'];
              echo $row['phone'];
          }
  
      }
  </code></pre>
  
  <p>View</p>
  
  <pre><code> &lt;?php foreach ( $query-&gt;result_array as $new_user )
              {
                  ?&gt;
                  &lt;h4&gt;Your name: &lt;?php echo $new_user['fName'] ?&gt; &lt;/h4&gt;
              &lt;?php
              }
              ?&gt;
  </code></pre>