开发者

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

开发者 https://www.devze.com 2023-04-01 03:32 出处:网络
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 t开发者_如何学Crue (and data) or fals

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 t开发者_如何学Crue (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
}


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号