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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀