CodeIgniter Forums
Forms displaying error if no result is found. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: Forms displaying error if no result is found. (/showthread.php?tid=64876)



Forms displaying error if no result is found. - mydiskarte - 04-06-2016

I have this codes like this in my controller. It works fine. But when there is no record found in the database, it display a lot of errors because of $customer[0]->first_name.

$data['bfname'] = array(
'name' => 'bfname',
'id' => 'bfname',
'class' => 'form-control',
'type' => 'text',
'value' => $this->form_validation->set_value('bfname', $customer[0]->first_name),
);

I'm thinking of solving it by:

if ($customer) {
$bfname = $customer[0]->first_name;
}
else {
$bfname = '';
}

Then I'll put the $bfname in the value. But is there another way of solving this in CI?


RE: Forms displaying error if no result is found. - PaulD - 04-06-2016

Hi,

Not that I know of, there is not auto way of dealing with that.

If there is no record found, I would normally show an error of some description, after all, if they are requesting a non-existant record something has gone wrong somewhere.

If it is a case of the difference between add or edit, so when adding, clearly no record exists. In that circumstance I just fill the array with blank values. Usually I also have a hidden field to indicate if we are adding or editing too, to decide if we are updating an existing field or adding a new one in the controller on successful form validation. Although often I have different forms for adding or editing, so if editing and no record is found an error screen is shown.

Best wishes,

Paul.

PS You always have to check, if there is a possibility of a query not returning a result, if there is a result or not. (Which is almost always the case, in fact I cannot think of one query where a record is guaranteed to exist.) I also always do the check in the view as well.


RE: Forms displaying error if no result is found. - cartalot - 04-07-2016

you could do it in two passes -- assemble the form element
$data['bfname'] = array(
'name' => 'bfname',
'id' => 'bfname',
'class' => 'form-control',
'type' => 'text',
);

and then depending on the condition in a separate method add the relevant value to the array
$data['bfname']['value'] = $some value

so then you can call different methods to add the field value depending on if you are getting something back from the database, etc
which is not quite as cool as what you are doing - you are doing it in one pass - but i like having it be very obvious whether the value is coming from the database or not.


RE: Forms displaying error if no result is found. - spjonez - 04-09-2016

Couple options;

1. Validate the record before you try to access it. We do that like this;

Code:
$result = $this->db->query( $sql, $params )->row_array( );

if ( empty( $result ) === true ) {
    // set flash data saying row not found
    // redirect to another page
}

2. If you're using a combined create/edit page ensure the array always has keys by merging it with a set of defaults.

Code:
$defaults = array(
    'id' => 0,
    'name' => '',
);

$result = $this->db->query( $sql, $params )->row_array( );

$result = array_merge( $defaults, $result );



RE: Forms displaying error if no result is found. - mydiskarte - 04-11-2016

Thank you for your replies.