Codeigniter:使用session从另一个表中获取userdata

时间:2017-10-25 11:29:37

标签: php mysql codeigniter session

我正在尝试使用会话从另一个表中打印userdata。会话的表是“userlogin”,而带有数据的表是“userinfo”

这是我到目前为止所做的事情

控制器

public function index()
    {
        if ( $this->session->userdata('logged_in') )
        {

            $session_data = $this->session->userdata('userinfo');
            $id = $session_data['id'];
            $this->load->model('Userdata_model');
            $result = $this->Userdata_model->get_user_data3($id);


            if($result==0)
            {
                echo 'No user Found';
            }
            else
            {
                $data['id']=$result;

                $this->load->view("userdata_view", $data);
            }
        }
    }

模型

public function get_user_data3($id)
    {
    if (isset($this->session->userdata['logged_in'])) {

        $userid = ($this->session->userdata['logged_in']['id']);
    } else {
        header("location: http://localhost/test");
    }

        $query = $this->db->query("SELECT * FROM userinfo WHERE id='$id'");

        foreach ($query->result_array() as $row)
        {
            echo $row['fName'];
            echo $row['phone'];
        }

    }

查看

 <?php foreach ( $query->result_array as $new_user )
            {
                ?>
                <h4>Your name: <?php echo $new_user['fName'] ?> </h4>
            <?php
            }
            ?>

1 个答案:

答案 0 :(得分:0)

更改控制器中的功能:

public function index()
{
    if ( $this->session->userdata('logged_in') )
    {

        $session_data = $this->session->userdata('userinfo');
        $id = $session_data['id'];
        $this->load->model('Userdata_model');
        $data['result'] = $this->Userdata_model->get_user_data3($id);

        $this->load->view("userdata_view", $data);

    }
}

更改模型功能,如:

public function get_user_data3($id)
{

    $query = $this->db->query("SELECT * FROM userinfo WHERE id='$id'");

    return $query;

}

并查看:

<?php if($result->num_rows() == 0){
         echo 'No user found';
      }
      else {
        foreach ( $result->result_array() as $new_user ){ ?>
           <h4>Your name: <?php echo $new_user['fName'] ?> </h4>
<?php   } 
      }
?>