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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line