试图让我的数据显示在view-codeigniter上

时间:2016-06-09 06:23:39

标签: codeigniter view

从我的控制器中,我能够将会话日期显示在我的视图上,但是当我从表中显示数据时,我在语法中遗漏了一些东西。即$ data ['modelData']。我怎么做? 控制器:

function validation()
    {

        $this->load->library('form_validation');

        $this->form_validation->set_rules('email', 'Email', 'required|trim');
        $this->form_validation->set_rules('password', 'Password', 'required|md5');


        if ($this->form_validation->run()  == FALSE )
        {
            $this->load->view('login_form');
            //return false; 
        }
        else{

           $this->load->model('login_model');

            $email = $this->input->post('email');
            $password = $this->input->post('password');
             //echo $email;
            // echo $password;
            $result = $this->login_model->match_login($email, $password);

            if ($result ==false) {
                echo "Invalid Cardinals";
             } 
             else 
             {
                $session = array(
                  'id'=>$result[0]['id'], 
                  'email'=>$this->input->post('email'),
                  'is_logged_in'=> 1
                );
                if (!$this->session->set_userdata($session)) {

                    $data['modelData'] = $result;
                    $data['sessionData'] = $session;

                    $this->load->view('dashboard_view', $data);
                }
                 else {
                    echo "Error in session";
                }                          
            }   
        }            
    }

模型

 class Login_model extends CI_Model{
 //client..model

    public function match_login($email, $password){

    $this->db->where('email', $email);
    $this->db->where('password', $password);
    $query = $this->db->get('user');

    $result = $query->result_array();

    if (empty($result)) 
    {
        return false;
    } 
    else 
    {
        return $result;
    }

 }
}

2 个答案:

答案 0 :(得分:0)

替换:

if (!$this->session->set_userdata($session)) {
    $data['modelData'] = $result;
    $data['sessionData'] = $session;
    $this->load->view('dashboard_view', $data);
}

if ($this->session->set_userdata($session)) {
    $data['modelData'] = $result;
    $data['sessionData'] = $session;
    $this->load->view('dashboard_view', $data);
}

Explantion:

由于set_userdata函数返回true,因此无法从表中获取数据,因此如果阻塞,控制语句将忽略语句。

答案 1 :(得分:0)

试试这个

在控制器中

if (!$this->session->set_userdata($session)) { # Changed

    echo "Error in session";
}
else {

    $data['modelData'] = $result;
    $data['sessionData'] = $session;

    $this->load->view('dashboard_view', $data);
}

在视图中

// retrieving modelData
foreach ($modelData as $modelItem) {
    echo "Model ID is ".$modelItem['id'];
}

// retrieving sessionData
foreach ($sessionData as $sessionitem) {
    echo "Session ID is ".$sessionitem['id'];
    echo "Session Email is ".$sessionitem['email'];
}