我无法将控制器中的信息传递给视图

时间:2015-01-14 15:46:49

标签: php codeigniter templates scope

我尝试var_dump我的foreach循环howerver它返回NULL。我的数据库不是空的,我一直在声明正确的变量,但它仍然给我一个错误,说

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/v_home.php

Line Number: 16

我有3个控制器(c_home.php,c_verifylogin.php和c_login.php)

- > c_login.php加载我登录的视图

- > c_verifylogin.php是验证和身份验证过程完成的地方,并尝试匹配数据库中的数据。

- > c_home是加载主页视图的控制器,这也是我为studentinfo创建一个显示其全名的方法。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('m_login','',TRUE);
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
    }

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

    function studentinfo() {
       $this->data['query'] = $this->m_login->getStudentInfo(); 
       $this->load->view('v_home', $data);
    }

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

}

这是c_home.php将传递信息的视图

<!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>

 <h4>Display Records From Database Using Codeigniter</h4>
    <table>
     <tr>
      <td><strong>First Name</strong></td>
      <td><strong>Last Name</strong></td>
    </tr>
    <?php if (var_dump($query)){?>

     <?php foreach($query as $row){?>
     <tr>
         <td><?php echo $row->firstname;?></td>
         <td><?php echo $row->lastname;?></td>
      </tr>    
     <?php }?>  
     <?php }?>
   </table>


 </body>
</html>

然后这是我完成所有查询的模型:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class M_login extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    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 getStudentInfo()
{
      $this->db->select('firstname,middlename,lastname');
      $this->db->from('studentinfo');
      $query = $this->db->get();
      return $query->result_array();
}

}

我很困惑为什么它不会从我的观看中显示任何检索到的数据。我已经声明了$ query,这是因为控制器太多了吗?请帮忙! PHP和CodeIgniter的新手。

2 个答案:

答案 0 :(得分:0)

我认为您需要在C_home中更改某些内容,在studentinfo()函数中,当您返回视图时,需要将$data更改为$this->data。< / p>

答案 1 :(得分:0)

你的索引和studentinfo功能调用相同的视图。但我认为这不是你的目的。你应该有两个不同的观点。问题是

http://localhost/codeigniter/index.php/c_home/ //this link will produce that error
http://localhost/codeigniter/index.php/c_home/studentinfo  //this link will not produce the same error.  

这是解决方案,你可以删除该错误,但我不确定你真正想要的是什么。

function index() {
    if($this->session->userdata('logged_in'))
    {
        $session_data = $this->session->userdata('logged_in');
        $data['studentid'] = $session_data['studentid'];
        $data['query']=array();
        //add this line there.your error will gone.
        //rememeber it will not display current student's first name and last name

        //or you can use this 
        //$this->data['query'] = $this->m_login->getStudentInfo(); 
        //remember your model returns all students info not current student.
        //so it will show all students info

        //if you want only current student's info u need
        //$data['query'] =something that will return only current loged user's info


        $this->load->view('v_home', this->$data);
        //here you calling v_homve view but not passing $query;
    } else {
    //If no session, redirect to login page
        redirect('c_login', 'refresh');
    }
}

主要问题在于Index功能,您没有在您的视野中传递$query 你应该知道如何从控制器传递变量并在视图中接受它