Php Codeigniter,验证空值

时间:2016-04-25 16:54:51

标签: php mysql codeigniter

我的项目中有这个代码:

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

                    }
        }

    }

我将这些变量传递给我的观点:

    $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'];}

变量数据['odometer']获取我的SQL数据库中的最新记录(里程表)与数据['date'](日期)相同。

所以问题是如果'$ data ['lastodometer']'为空且'$ data ['lastdate']'为空(我的数据库中没有先前的记录)我收到错误{{1} }和Undefined index: odometerreading。如果我的日期记录中没有记录,我怎样才能将我的lastodometer设置为0并将其设置为datedoday?

1 个答案:

答案 0 :(得分:1)

使用empty()检查变量是否为空(当使用empty或isset时,如果变量不存在,则不会发出警告)。如果是这样,请将它们设置为0.

例如:

if (empty($data['lastodomoter']){
    $data['Lastodometer'] = 0;
}