0

我是代码 igniter 和 MVC PHP 的新手,我已经创建了一个带有在线资源的基本登录系统并且它可以工作,我现在正在尝试改进它以学习......

我的模型:

function validate(){


            $this->db->where('Email', $this->input->post('email'));
            $this->db->where('Password', sha1($this->input->post('password')));
            $query = $this->db->get('admin_users');

        if($query->num_rows == 1){

            return true;
        }

    }

    function get_admininfo(){


        $this->db->where('Email', $this->session->userdata('email'));
        $query = $this->db->get('admin_users');

        return $query->result();
    }

一位用户已经验证我想提取他们的一些信息并将其存储在会话中,用于通知等,化身。

我的控制器看起来像这样:

 function validate_credentials(){

        //load the adminship model and then the function validate within it.
        $this->load->model('adminship_model');
        $query = $this->adminship_model->validate();

        // If the admin user successfully validated.
        if($query){

            $admininfo = $this ->adminship_model->get_admininfo();

            $data = array(

                'email' => $this->input->post('email'),
                'username' => $this->$admininfo['username'],
                'is_logged_in' => true
            );

            $this->session->set_userdata($data);
            redirect('dashboard_controller/dashboard');


        }else{

           $this->index();

        }

    }

在我设置的仪表板控制器中:

        $data['email'] = $this->session->userdata('email');
        $data['email'] = $this->session->userdata('username');

并将其传递给视图,当我回显 $email 时,我收到了电子邮件,但用户名没有拉我收到此错误:

消息:未定义索引:用户名

这一行:

'username' => $this->$admininfo['username'],

我知道我以错误的方式从模型中提取信息,请有人指出我正确的方向。

4

2 回答 2

0

功能

function get_admininfo(){


    $this->db->where('Email', $this->session->userdata('email'));
    $query = $this->db->get('admin_users');

    return $query->result();
}

正在尝试将查询作为对象返回

      $admininfo = $this ->adminship_model->get_admininfo();

        $data = array(

            'email' => $this->input->post('email'),
            'username' => $this->$admininfo['username'],
            'is_logged_in' => true
        );

正在尝试将 $data['username'] 设置为来自 $this->admininfo 的预期数组值,但它返回了一个对象

尝试改变:

return $query->result();

return $query->row_array();
于 2013-02-25T01:24:47.940 回答
0

尝试将您的模型功能更改为:

 function get_admininfo(){
    $this->db->where('Email', $this->session->userdata('email'));
    $query = $this->db->get('admin_users');

    if($query->num_rows() == 1){
       foreach($query->result() as $row){
            $data = $row;
       }
       return $data;

    } else {
    return FALSE;
    }
 }

然后尝试以这种方式获取数据:

   $admininfo = $this ->adminship_model->get_admininfo();
        $data = array(

            'email' => $this->input->post('email'),
            'username' => $admininfo->username,
            'is_logged_in' => true
        );

编辑

尝试 go get all_userdata(),因为您要使用它:

$data['userdata'] = $this->session->all_userdata(); // returns an associative array

然后将其传递给视图并使用 将其从视图中取出$userdata['username'],这通常对我来说很好。

选项

如果上面的代码不起作用,可能最简单的方法是指定列的名称,例如:

function get_admininfo(){
    $this->db->where('Email', $this->session->userdata('email'));
    $query = $this->db->get('admin_users');

    if($query->num_rows() == 1){
        $row = $query->row(); 
        return $row->username; //assuming this is the name of the column
    } else {
    return FALSE;
    }
 }

将控制器代码更改为:

        $admininfo = $this ->adminship_model->get_admininfo();
        $data = array(

            'email' => $this->input->post('email'),
            'username' => $admininfo,
            'is_logged_in' => true
        );
于 2013-02-24T22:50:49.437 回答