2013-01-07 46 views
0

嗨,大家好,我只是为了显示错误消息而只是简单地显示了代码,但现在仍在显示。 - 你可以在开始做这个在codeigniter中显示带有错误消息的值页面

首先发起的空属性,所以你知道它的存在:

public function check_profile_ifexist($id) 
    { 

    if($this->input->post('edit')){ 

    $this->form_validation->set_rules('email','Email','trim|valid_email|is_unique[user_details.email]'); 
    $this->form_validation->set_rules('username','Username','trim|min_length[6]|max_length[20]|is_unique[user_details.username]|xss_clean');    $this->form_validation->set_message('is_unique',"That %s already exists!"); 
    $this->form_validation->set_message('max_length', 'the maximum characters for %s is 20'); 
    $this->form_validation->set_message('min_length', 'the minimum characters for %s is 6'); 

    if ($this->form_validation->run()) 
    { 
     $this->load->model('model_user_manage'); 
     $em=$this->model_user_manage->update_all($id); 
    } 
    else 
    { 

    $this->view_profile($id); 
    } 

} 

}

+0

你好谢赫你能帮助我吗? – webstudent

回答

0

相反呼应了所有的错误,只是他们都存储到财产check_profile_ifexist()方法:

$this->profile_errors = ''; 

然后你check_profile_ifexist()方法中只需添加错误的属性,当您去,而不是附和他们如

if (MD5($username)==$pass){ 
    echo "username cannot be the same as password"; 
} 

变为:

if (MD5($username)==$pass){ 
    $this->profile_errors .= "username cannot be the same as password\n"; 
} 

这个属性届时将提供给所有的方法,所以你可以把它添加到view_profile(数据阵列),像这样:

if(isset($this->profile_errors)){ 
    $data['errors'] = $this->profile_errors; 
}else{ 
    $data['errors'] = ''; 
} 

和错误在您的视图中可用作$错误,因此您可以用echo $errors;打印出所有错误。

注意显然你必须确保该属性总是存在或检查其存在,以避免你自己的错误。我还在错误中添加了换行符,以便在多个打印出来时可以看起来更整齐一些。

第二个注意你似乎有很多东西我会放在这个代码模型,我认为是一个控制器。你应该把所有的数据库模型保存在模型中,否则MVC警察会为你而来。

+0

嗨webweaver非常感谢您的想法,但你能提出一个好的方法,因为即使我对代码很困惑,虽然它的作品很难阅读。我正在做一个更新配置文件,如果用户输入的密码和旧用户名相同,用户无法更新用户名,并且在视图中提供了文本框上的值的错误消息 – webstudent

+0

您可以讨论关于属性的更多信息我把这个抱歉,我是新的codeiginiter – webstudent

+0

属性就像一个变量,可用于您的班级中的所有方法。您可以在check_profile_ifexist()方法的开头声明空属性。如果你不直接调用view_profile,这将工作正常。无论如何,最好在使用它之前检查它是否已经设置好。我会更新我的答案,以反映这 – WebweaverD

相关问题