Thomas Clowes
Thomas Clowes

Reputation: 4609

Custom errors e.g more than 1 record when using Codeigniter method

I have a codeigniter application.

My controller calls a function of a method. I am checking if the row it is trying to select actually exists, and then returning true (and data) or false (and error message) respectively to my controller.

Within my controller there are multiple calls to this function.

I only want to load the 'success' view if all of these method calls return true. Otherwise i want to display an error..

Given that i want to show the error within my layout, i could simply create an error view and pass an error message to it.. if there are multiple errors i want to display them all..

Is the correct/most efficient way to do this simply:

if($resultone['result'] == FALSE or $resulttwo['result'] == FALSE)
{
    $data['error']['0']=$resultone['error'];
    $data['error']['1']=$resulttwo['error'];

    $this->load->view('custom_error',$data);
} else {
    //load success view
}

Upvotes: 0

Views: 54

Answers (1)

LeleDumbo
LeleDumbo

Reputation: 9340

I'd rather do this:

$data = array(
  'data'   => array(),
  'errors' => array()
);
...
$result = call_that_function($arg1);
if (isset($result['error'])) {
  $data['errors'][] = $result['error'];
} else {
  $data['data'][] = $result['data'];
}
...
$result = call_that_function($arg2);
if (isset($result['error'])) {
  $data['errors'][] = $result['error'];
} else {
  $data['data'][] = $result['data'];
}
...
$result = call_that_function($arg3);
if (isset($result['error'])) {
  $data['errors'][] = $result['error'];
} else {
  $data['data'][] = $result['data'];
}
...
if (count($errors['errors']) > 0) {
  $this->load->view('custom_error',$data);
} else {
  $this->load->view('success',$data);
}

Upvotes: 1

Related Questions