1

问题 1)我已经设置了一个注册/登录区域并验证了所有内容(包括空白字段),如果用户名和/或密码不正确,则在显示错误消息时碰壁。目前,如果字段留空,但在提交页面时,我会收到一条错误消息,显示正确或不正确的用户详细信息。这是我当前的代码:

问题 1(已解决):

控制器:

//This method will have the credentials validation
    public function credentials_validation() {
        $data['error'] ="Invalid Username or Password";
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_password_check');

          if($this->form_validation->run() == FALSE)
           {
             //Field validation failed.  User redirected to login page
             $this->index();

             }else{
                 $this->load->model('register_model');

                if($query = $this->register_model->validate()) {
                $this->load->model('news_model');

                $data = array('news' => $this->news_model->getArticle());
                $data['title'] = 'Admin | Home';
                $data['heading'] = 'News Articles';
                $data['main_content'] = 'admin/news';
                $this->load->view('include/template', $data);

                }else {
                $this->load->view('login',$data);
                }    

            }
    }

问题 2) 新闻文章页面仅加载 1 篇文章,并且 url 显示类名。

新闻总监:

class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('admin/news_model');
    }

    public function index() {
        $data = array('news' => $this->news_model->getArticle());
        $data['title'] = 'Admin | News Home';
        $data['heading'] = 'News Articles';
        $this->load->view('admin/news', $data); 
}

}

新闻模型只是拉取数据库表,全选并按 id 排序并生成数组。任何人都知道为什么这仅显示 1 个结果?我认为这与 foreach 方法有关吗?

PS,没有从 CI 或 php 错误日志中显示错误消息!

4

1 回答 1

5

最好的方法是将错误消息存储在一个数组中,例如

$data['error'] ="Invalid Login";

例如在控制器中

if($query = $this->register_model->check_membership()) {
    $data['main_content'] = 'signup_successful';
    $this->load->view('include/template', $data);

}else {
    $data["error"]="Invalid User Id and Password combination";
    $this->load->view('signup',$data);
}

check_membership功能应检查登录名和密码组合是否正常。

然后在signup视图页面中,在顶部放一些东西

if (isset($error)){
    echo "<div class='error'>$error</div>";
}
于 2012-08-31T12:47:53.000 回答