Welcome Guest, Not a member yet? Register   Sign In
display error by function
#1

hi in my model i have this function :

Code:
    function login($email,$passwrd){
       
       $this->db->where('email', $email);
       $q = $this->db->get('users')->row();
       
       if($q->num_rows()!=0){
           if ($q->password_hash==$passwrd){
               $_SESSION['user_id']=$q->id;
           } else {
               $errors[]='Password error'  ;
         
           }
       }
       else{
           $errors[]='Username error'  ;
       }
       

       
    }
     


HOw can i show error in view ?
Reply
#2

return the errors array. Then you can add it to your view like so: $this->load->view('your-view', ['errors' => $errors]);
Simpler is always better
Reply
#3

(06-15-2017, 04:59 PM)donpwinston Wrote: return the errors array. Then you can add it to your view like so: $this->load->view('your-view', ['errors' => $errors]);

This is my function on controller (simple function only to try):

Code:
public function index(){
       $this->load->library('form_validation');
       $this->form_validation->set_rules('email','email','required|valid_email', array('required' => 'Inserisci la mail','valid_email'=>'la mail deve essere valida'));
       $this->form_validation->set_rules('password','password','required|min_length[6]');
       
       if($this->form_validation->run()){
           
          $this->load->model('user_model');
         
          $res=$this->user_model
          ->login(
              $this->input->post('email'),
              $this->input->post('passwrd')
           );
         
          if($res!=false){
             $_SESSION['user_id']=$res->id;
          }
         
       }
       
       
       $this->load->view('user/login',['errors' => $errors]);
   }
   


But in my view i have :

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: errors
Filename: controllers/User.php
Reply




Theme © iAndrew 2016 - Forum software by © MyBB