• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forms displaying error if no result is found.

#1
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?
Reply

#2
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.
Reply

#3
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.
Reply

#4
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 );
Reply

#5
Thank you for your replies.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.