问题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篇文章,网址显示班级名称。
新闻控制器:
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错误日志中没有显示错误消息!
答案 0 :(得分: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
函数应检查登录ID和密码组合是否正常。
然后在signup
视图页面中,将某些内容放在顶部,如
if (isset($error)){
echo "<div class='error'>$error</div>";
}