douwen5985 2018-02-08 15:02
浏览 60
已采纳

页面加载错误codeigniter

Small codeigniter project that supplies listing of held domains. After the login page you are redirected to the list of domains from the database. Run this page (the list of doamins) and there is no problem. The only problem is when you call this page from the login. The login page is no problem (login controller below).

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;
    }
}

The call to the 'home' index seem to be the problem (code below) Home page model

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();
    }
}

The home page controller:

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

remove the login and there is no problem all pages work fine. This is the error:

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
  • 写回答

1条回答 默认 最新

  • duanji8887 2018-02-08 15:20
    关注

    The problem is when the user enters the valid credentials you will load the view home. But you are not passing any parameter to the view.

    By looking at the home controller, I assume you are rendering a list of posts on the home page(Home view). So instead of loading the view, you must redirect him to home controller.

    Try using redirect('home','refresh') instead of $this->load->view('home') after logging in.

    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');
        }
    }
    

    The redirect takes him to the home controller and renders what is needed.

    public function index()
    {
        $this ->load->model('Crudmodel');
        $records = $this->Crudmodel->getPosts();
        $this->load->view('home', array('records' => $records));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测