2013-01-07 47 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

相關問題