-1

当我在控制器文件夹中编写以下代码时,命名文件LoginController.php

<?php
class LoginController extends CI_Controller
{

    public function index()
    {
        $this->load->view('login');
    }
    public function check()
    {
        $this->form_validation->set_rules('username','Username','required|valid_email');
        $this->form_validation->set_rules('password','Password','required');
    }
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('login');
    }
    else{}

}
?>

发出以下解析警告:

-Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION) in    C:\xampp\htdocs\CodeIgniter\application\controllers\LoginController.php on line 14
4

3 回答 3

3

您已经在检查功能之外编写了 if 子句,它应该是这样的

  <?php      
 public function check() 
 {
    $this->form_validation->set_rules('username','Username','required|valid_email');
    $this->form_validation->set_rules('password','Password','required');
    if($this->form_validation->run() == FALSE)
    {
      $this->load->view('login');
    }
    else{} 
  }
  ?>
于 2013-08-17T19:07:07.453 回答
1

您的 If Else 不包含在任何函数中...以这种方式将其放在函数大括号中

public function check()
{
    $this->form_validation->set_rules('username','Username','required|valid_email');
    $this->form_validation->set_rules('password','Password','required');
    if ($this->form_validation->run() == FALSE){
    $this->load->view('login');
        }
    else{
    }
}
于 2013-08-17T19:23:36.213 回答
0

您已经在函数外部定义了 if-else,将其添加到函数内部

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

     if ($this->form_validation->run() == FALSE)
     {
        $this->load->view('login');
     }
     else
     {

     }
  }
于 2013-08-18T02:43:34.203 回答