lightning_missile
lightning_missile

Reputation: 2992

Proper handling of errors stored in a class in code igniter

I am doing a project using code igniter and I have to pass all error messages to ajax. So I'm thinking of putting them in an array. How can I go about this?

The first approach I've thought is to create a class that store all error messages and displays them, like this.

class errors {
    public $errorList = array();
    public function set_error($key, $message)
        $errorList[$key] = $message;
    }

    public function display_errors()
    {
        return $errorLis;
    }
}

I would use it like this:

public function upload_file()
{
    // if file not uploaded,
    set_error("file upload", $this->upload->display_errors());
}

Is this approach fine? What is wrong with it? What's a better way?

Upvotes: 0

Views: 37

Answers (1)

cgds
cgds

Reputation: 46

You can use flashdata controller:

class Welcome extends CI_Controller {

 public function add_user() {  

     //insert or update code
     $this->session->set_flashdata('message', 'Successfully Added.');
     $this->load->view('index'); 
     }   
 }

view:

<?php if($this->session->flashdata('message')){?> 
    <div class="alert alert-success">  
    <?php echo $this->session->flashdata('message')?> 
    </div>
 <?php } ?>

Upvotes: 1

Related Questions