0

I have this code in my project:

public function new_inspection_report() {

                $NOWdate = new Datetime('now');
        $this->check_session();
        $this->restrict_ordinary();

    $truckno = $this->uri->segment(2);

    $data['title'] = "New Inspection Report";

    $data['result'] = $this->truck->find_truckbyid($truckno);
    $data['regno'] = $data['result']['registrationno'];
    $data['odometer'] = $this->truck->validate_odemeter($truckno,$this->input->post('odometerreading'),"mypms_inspection_report");
    $data['lastodometer'] = $data['odometer']['odometerreading'];
    $data['date'] = $this->truck->validate_date($truckno,$this->input->post('datestarted'),"mypms_inspection_report","date");
    $data['lastdate'] = $data['date']['date'];


        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters("<div class='row'><div class='col-lg-12'><div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><i class='fa fa-info-circle'></i>&nbsp;", "</div></div></div>");

        $data['validateodometer'] = $this->truck->validate_odemeter($truckno,$this->input->post('odometerreading'),"mypms_inspection_report");
        $data['validatedate'] = $this->truck->validate_date($truckno,$this->input->post('datestarted'),"mypms_inspection_report","date");

        if($this->form_validation->run('new_inspectionreport') === FALSE) {

    $this->load->view('header',$data);
    $this->load->view('new_inspection_report',$data);
    $this->load->view('footer',$data);  

        }else{

            if($this->input->post('odometerreading') < $data['validateodometer']['odometerreading']) {
                            $this->session->set_flashdata('success_insert', "4");
                            redirect(base_url("new-inspection-report/".$truckno));  



                    }else{


                        if($this->input->post('datestarted') < $data['validatedate']['date']) {

                            $this->session->set_flashdata('success_insert', "5");
                            redirect(base_url("new-inspection-report/".$truckno));  

                        }elseif ($this->input->post('datestarted') > $NOWdate)
                        {


                            $this->input->post('datestarted') > $NOWdate;

                            $this->session->set_flashdata('success_insert', "6");
                            redirect(base_url("new-inspection-report/".$truckno));

                        }
                        else
                        {
                        $this->truck->insert_inspectionreport($truckno);
                        $last_id = $this->db->insert_id();
                        $count = $this->truck->count_inspection_bytruck($truckno);

                        $this->authentication->watch_dog("Successfully add inspection report with ID ".$last_id,$this->session->userdata('id'));
                        $this->truck->insert_inspectionno($count,$last_id);


                        $this->session->set_flashdata('success_insert', "1");
                        redirect(base_url('new-inspection-report/'.$truckno));
                        }

                    }
        }

    }

I am passing these variables to my view:

    $data['odometer'] = $this->truck->validate_odemeter($truckno,$this->input->post('odometerreading'),"mypms_inspection_report");
    $data['lastodometer'] = $data['odometer']['odometerreading'];
    $data['date'] = $this->truck->validate_date($truckno,$this->input->post('datestarted'),"mypms_inspection_report","date");
    $data['lastdate'] = $data['date']['date'];}

The variables data['odometer'] gets the latest record (odometer) on my SQL Database same as data['date'] (date).

So the problem is if '$data['lastodometer']' is null and '$data['lastdate']' is null (No previous record yet in my DB) I am receiving an error of Undefined index: odometerreading and Message: Undefined index: date. How I can set the my lastodometer to 0 and lastdate to datetoday if I have no record on my datebase yet?

John Paul
  • 55
  • 1
  • 8

1 Answers1

1

Check if the vars are empty by using empty() (When using empty or isset, no warning will be thrown in case the variable does not exist) . If this is true, set them to 0.

For example:

if (empty($data['lastodomoter']){
    $data['Lastodometer'] = 0;
}
manniL
  • 7,157
  • 7
  • 46
  • 72
  • empty() does not test for variable existence, so this will STILL produce undefined index warnings. – Marc B Apr 25 '16 at 17:00
  • Quoted from the [PHP Manual](http://php.net/manual/en/function.empty.php): "No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !isset($var) || $var == false. " @MarcB Your comment is therefore wrong. – manniL Apr 25 '16 at 17:02
  • 1
    Ah yes. apologies... edit your answer so I can remove the downvote. But note that empty() is still unreliable. if the value being tested is naturally "falsey", you'll get a false positive, e.g. `empty('0')` is TRUE, even though the string has non-zero length. – Marc B Apr 25 '16 at 17:06
  • @MarcB Done! Yes, this is true. I'm relying on isset most of the time thought. – manniL Apr 25 '16 at 17:08