在浏览器中显示记录的用户信息,PHP PHP代码点火器


Show logged user info in browser, PHP php codeigniter

我想在登录后显示数据库中的当前用户信息。这是我的密码。

控制器1(欢迎)

public function getValues() {
    $this->load->model('display_model');
    $result=$this->display_model->authenticateUser();
    if($result){
        $this->session->set_userdata('id', $result['id']);
        redirect('/member/home/');
    }
    else
        redirect('/welcome/');

控制器2(成员)

public function __construct()
{
        // Call the CI_Model constructor
        parent::__construct();
        if($this->session->userdata('id')==NULL)
        {
            redirect('/welcome/');
        }
}
public function home() {
    $this->load->view('member_home');
}

型号

public function authenticateUser(){
$name =$_POST['name'];
$password=$_POST['password'];
$where=array('name'=>$name,
             'password'=>$password);
return $this->db->select('name, password, id')
                ->from('member')
                ->where($where)
                ->get()->row_array();

视图1(login_View)

<h1>Member Login Form</h1>
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome/getValues'); ?>
    Username: <br/>
<input type="text" name="name" /> <br>
Password:  <br>
<input type="password" name="password" /> <br>
<input type="submit" value="Login" name="submit"/>

</form>

视图2:登录后视图(member_home)

<?php   
         foreach($result as $row){
            ?>
            <li><?php echo $row['first_name'].' '.$row['last_name'];?>
                <br/><strong>Email: </strong><?php echo $row['email'];?>
            </li>

现在显示错误消息:未定义的变量:结果

如何解决需要帮助。。提前感谢

您没有将$result值传递到主视图,请按如下方式更新代码:

控制器1:

   public function getValues() {
        $this->load->model('display_model');
        $result=$this->display_model->authenticateUser();
        if($result){
            $this->session->set_userdata('id', $result['id']);
            redirect(member/home/$result['id']);
        }
        else
            redirect('/welcome/');

控制器2:

  public function __construct()
    {
            // Call the CI_Model constructor
            parent::__construct();
            if($this->session->userdata('id')==NULL)
            {
                redirect('/welcome/');
            }
    }
    public function home($id = '') {
        $data['id'] = $id;
        $this->load->view('member_home',$data);
    }

注意:

在视图上使用foreach时,必须将查询结果从row_array()更改为result_array