php - 页面加载错误codeigniter

标签 php mysql function codeigniter

提供持有域列表的小型 codeigniter 项目。 在登录页面之后,您将被重定向到数据库中的域列表。 运行这个页面(域列表)没有问题。唯一的问题是当您从登录名调用此页面时。 登录页面没有问题(下面是登录 Controller )。

public function index(){
    $this->load->view('login');
}
public function checklogin()
{
    $this->form_validation->set_rules('username','Username','required');
    $this->form_validation->set_rules('password','Password','required|callback_verifyUser');

    if($this->form_validation->run() == false){
        $this->load->view('login');
    }else{
        $this->load->view('home');
    }
}
public function verifyUser(){
    $User = $this->input->post('username');
    $Pwd = $this->input->post('password');

    $this->load->model('Loginmodel');
    if($this->Loginmodel->login($User,$Pwd)){
            return true;
    }else{
         $this->form_validation->set_message('verifyUser','Incorrect Login Details');   
         return false;
    }
}

对“home”索引的调用似乎是问题所在(下面的代码) 首页模型

public function getPosts()
{
    $result = array();
    $this->db->select('*');
    $this->db->from('domains');
    $this->db->where('Active','Y');
    $this->db->order_by('ExpDate','ASC');
    //$query= $this->db->query("select * from baldwin_domains where Active ='Y' order by ExpDate ASC");
    $query = $this->db->get();
    if($query->num_rows()>0){
        return $query->result();
    }
}

主页 Controller :

public function index()
{
    $this ->load->model('Crudmodel');
    $records = $this->Crudmodel->getPosts();
    $this->load->view('home', ['records' => $records]);
}

删除登录名,没有问题,所有页面都可以正常工作。这是错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: records

Filename: views/home.php
Line Number: 17
Backtrace:
File: C:\xampp\htdocs\domainsys\application\views\home.php
Line: 17
Function: _error_handler
File: C:\xampp\htdocs\domainsys\application\controllers\Login.php
Line: 15
Function: view
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/home.php
Line Number: 17
Backtrace:
File: C:\xampp\htdocs\domainsys\application\views\home.php
Line: 17
Function: _error_handler
File: C:\xampp\htdocs\domainsys\application\controllers\Login.php
Line: 15
Function: view

最佳答案

问题是当用户输入有效凭据时,您将加载 View home。但是您没有将任何参数传递给 View 。

通过查看主 Controller ,我假设您正在主页(Home View )上呈现一个帖子列表。因此,您必须将他重定向到家庭 Controller ,而不是加载 View 。

尝试使用 登录后 redirect('home','refresh') 而不是 $this->load->view('home')

public function checklogin()
{
    $this->form_validation->set_rules('username', 'Username','required');
    $this->form_validation->set_rules ('password','Password','required|callback_verifyUser');

    if($this->form_validation->run() == false){
        $this->load->view('login');
    }else{
        redirect('home','refresh');
    }
}

重定向将他带到家庭 Controller 并呈现所需内容。

public function index()
{
    $this ->load->model('Crudmodel');
    $records = $this->Crudmodel->getPosts();
    $this->load->view('home', array('records' => $records));
}

关于php - 页面加载错误codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688528/

相关文章:

c++ - 在C++中传递函数类型的参数是什么意思?

PHP 解析 XML 时出错(simpleXML)

php - 在网页上输入数学字符

php - 如何在不为 Java Bridge 添加 tomcat 服务器的情况下将 Java 后端添加到现有 PHP 站点?

php - 如何构造 MySQL 数据库来存储多个复选框表单数据并稍后在 php 中进行统计?

mysql - 对于所有列都使用外键的表进行重复删除插入的解决方法

php - mysql:向 php 发送 sqlstate 信号

javascript - 如何获取表 <tr> 的唯一行并使用 jquery 对它们进行计数

c - 当我传入函数指针时出现错误

c - 返回数组的函数