2016-04-25 41 views
0

我有這樣的代碼在我的項目:腓笨,確認空值

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

的變量數據[「里程錶」]得到最新的記錄(里程錶)在我的SQL數據庫相同的數據['日期'](日期)。

所以問題是,如果「$數據[」 lastodometer「]」爲空和「$數據[」 lastdate「]」爲空(以前沒有記錄但在我的DB)我收到的Undefined index: odometerreadingMessage: Undefined index: date錯誤。如果我的數據庫中沒有記錄,我可以如何設置我的最後一個天氣計爲0,最後更新爲datetoday?

回答

1

使用empty()檢查變量是否爲空(當使用空或isset時,如果變量不存在,則不會發出警告)。如果這是真的,將它們設置爲0。

例如:

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

空()不測試對於變量存在,所以這仍然會產生不確定的索引警告。 –

+0

引自[PHP手冊](http://php.net/manual/en/function.empty.php):「如果變量不存在,則不會生成警告,這意味着empty()實質上是簡潔的等價to!isset($ var)|| $ var == false。「@MarcB您的評論因此是錯誤的。 – manniL

+1

啊是的。道歉...編輯您的答案,以便我可以刪除downvote。但請注意,empty()仍然不可靠。如果被測試的值自然是「假的」,那麼你會得到誤報,例如,即使字符串的長度不爲零,empty('0')也爲TRUE。 –