Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将登录凭据连接到codeigniter中检索到的数据_Php_Codeigniter_Parameter Passing - Fatal编程技术网

Php 将登录凭据连接到codeigniter中检索到的数据

Php 将登录凭据连接到codeigniter中检索到的数据,php,codeigniter,parameter-passing,Php,Codeigniter,Parameter Passing,我一直遇到从不同的视图传递变量的问题。我想要的是,每当用户登录他的id时,它都会自动从数据库检索连接到该id的数据 显然,我有3个控制器用于登录(c_home、c_login和c_verifylogin),1个模型(m_login)和1个视图(v_home) 谁能告诉我我错过了什么 控制器: c_登录 居家 function index() { if($this->session->userdata('logged_in')) {

我一直遇到从不同的视图传递变量的问题。我想要的是,每当用户登录他的id时,它都会自动从数据库检索连接到该id的数据

显然,我有3个控制器用于登录(c_home、c_login和c_verifylogin),1个模型(m_login)和1个视图(v_home)

谁能告诉我我错过了什么

控制器: c_登录

居家

   function index() {
        if($this->session->userdata('logged_in'))
        {
            $session_data = $this->session->userdata('logged_in');
            $data['studentid'] = $session_data['studentid'];
            $this->load->view('v_display', $data);
        } else {
        //If no session, redirect to login page
            redirect('c_login', 'refresh');
        }
    }

    function getGrades() {
       $data['query'] = $this->m_login->result_getGrades(); 
       $this->load->view('v_display', $data);
    }


    function logout() {
         //remove all session data
         $this->session->unset_userdata('logged_in');
         $this->session->sess_destroy();
         redirect('c_login', 'refresh');
     }
c_验证登录

function index() {

    $this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('v_login');
        } else {
            //Go to private area
            redirect('c_home', 'refresh');
        }       
 }


 function check_database() {
     //Field validation succeeded.  Validate against database
     $studentid = $this->input->post('studentid');
     $password = $this->input->post('password');
     //query the database
     $result = $this->login->login($studentid, $password);
     if($result) {
         $sess_array = array();
         foreach($result as $row) {
             //create the session
             $sess_array = array('studentid' => $row->studentid);
             //set session with value from database
             $this->session->set_userdata('logged_in', $sess_array);
             }
      return TRUE;
      } else {
          //if form validate false
          $this->form_validation->set_message('check_database', 'Invalid username or password');
          return FALSE;
      }
  }
m_登录

function login($studentid, $password) 
    {
        //create query to connect user login database
        $this->db->select('studentid, password');
        $this->db->from('users');
        $this->db->where('studentid', $studentid);
        $this->db->where('password', md5($password));
        $this->db->limit(1);

        //get query and processing
        $query = $this->db->get();
        if($query->num_rows() == 1) { 
            return $query->result(); //if data is true
        } else {
            return false; //if data is wrong
        }
    }
         function result_getGrades()
    {
          $this->db->select('grades.blockcode,subjectblocking.subjectcode,subjects.description,grades.final');
          $this->db->from('grades');
          $this->db->join('subjectblocking','grades.blockcode=subjectblocking.blockcode');
          $this->db->join('subjects','subjectblocking.subjectcode=subjects.subjectcode');
          $this->db->where('studentid', '2013-F0218');
          $this->db->where('sem', '1');
          $this->db->where('sy','2013-2014');
          $query=$this->db->get();
          return $query->result();

    }
视图:v_显示

<!DOCTYPE html>
 <head>
   <title>Simple Login with CodeIgniter - Private Area</title>
 </head>
    <body>
      <h1>Home</h1>
   <h2>Welcome <?php echo $studentid; ?>!</h2>
   <a href="c_home/logout">Logout</a>

            <table class="table">

                <thead>
                    <th>Subject Code</th>
                    <th>Description</th>
                    <th>Grade</th>
                </thead>
        <?php foreach ($query as $row){ ?>
                    <tr>
                        <td><?php echo $row->subjectcode;?><br></td>
                        <td><?php echo $row->description;?><br></td>
                        <td><?php echo $row->final;?><br></td>
                    </tr>

        <?php } ?>  
            </table>

    </body>
</html>

使用CodeIgniter简单登录-私人区域
家
欢迎
主题代码
描述
等级



我遇到的错误是

消息:未定义变量:查询

消息:为foreach()提供的参数无效


我看到的第一件事是,您没有在查询中命名表:

更改:

$query = $this->db->get();
为此:

$query = $this->db->get("your_table_name_here);

当我读你的代码时,我感到头痛。 实际上,您只需将验证登录名放入
c\u登录名
,而不必创建另一个
c\u验证
控制器

有道理 试着重构你的代码,就像连接是这样

c_home = private page that can only be access if the user is login
c_login = verify if the input of user passed and check the data from database. 
总结

c_登录
将由以下功能组成:

  • 验证用户输入
  • 通过m_登录检查数据库
  • 注意:您的注销应该放在核心中,以便所有控制器都可以使用它

    在您的
    c_主页中
    ,您只需创建一个模型,从数据库中获取数据并将其传递给您的

    $data['grades']=$your\u model->get\u grades

    现在将使用$data传递变量等级以查看

    注意:您不需要创建另一个函数来获取数据。您只需要模型,因为这是模型的目的,只需将它传递到控制器中的变量中即可

    总结:

    从模型->控制器->查看从模型获取数据并传入 控制器在视图中显示它


    可以但是即使我没有定义表名,它仍然可以工作。您能给出发现此错误的url吗。。因为在您的代码中,我找不到正在加载v_display的登录函数的调用
    c_home = private page that can only be access if the user is login
    c_login = verify if the input of user passed and check the data from database.