如果条件不起作用则嵌套

时间:2018-08-18 05:52:24

标签: php jquery json codeigniter-3

我正在使用CodeIgniter。我在数据库中有一列,例如

id | list_id |venue_id |name | start_date | end_date |start_time | end_time 

现在我有一个表单,其中的字段是

<select name="venue_id[]">// this can be multiple section
<option value="1">qwer</option>
<option value="2">poiu</option>
<option value="3">tgred</option>            
</select
<input type="text" name="name" class="form-control" placeholder="Name">
<input type="text" name="start_date" placeholder="Start Date" class="datepicker">
<input type="text" name="end_date" placeholder="End Date" class="datepicker">
<input type="text" name="start_time" placeholder="start time" class="datepicker">
<input type="text" name="end_time" placeholder="End time" class="datepicker">

现在我正在做的是,如果数据库中没有数据,那么用户输入所有信息,然后单击“提交”,则数据将插入数据库中。

现在让我们假设数据用户是第一次输入

 id | list_id |venue_id |name | start_date | end_date |start_time | end_time 
 1  |1        | 2       |xyz  |2018-08-18  |2018-08-30|5:00AM     |8:00AM

现在,如果用户要再次插入,则应首先在数据库中检查

 if(venue_id is same or not)// if not same then insert the data. if the same than checks date
 if(check start_date and end_date)
  // in case both are same or between then it will check the start_time and end_time should not be same or between or else insert data.

 if (time is matching or between the from 5:00 am to 8:00 am then display the error message)

  else(time not matching then insert a new record)

我正在尝试使用上述逻辑来解决我的问题,但是它不起作用。任何人都知道更好的逻辑,请让我知道。

控制器代码

         $id                         = $this->input->post('venue_id');
            $venue_id                   = implode(',',$id);     
            $activity_list_id           = $this->input->post('activity_name');
            $new_batch_start_date       = date('Y-m-d',strtotime($this->input->post('start_date')));    
            $new_batch_end_date         = date('Y-m-d',strtotime($this->input->post('end_date')));
            $new_batch_start_time       = $this->input->post('start_time');
            $new_batch_end_time         = $this->input->post('end_time');

        if($new_batch_start_date>= $new_batch_end_date)
            {
              $response['error'] = false;
              $response['msg']   = "End Date Should be Greater than Start Date";
              echo json_encode($response);
              return false;
            }

        $data=array(//insert data);
        $get_batch_details = $this->Batch_model->fetchBatches(); // 
        if(!empty($get_batch_details))
            {
              foreach ($get_batch_details as $rows)
              {
                $exist_batch_start_date =  $rows->start_date;
                $exist_batch_end_date =  $rows->end_date;
                $batch_time1 =  strtotime($rows->start_time);
                $batch_time2 =  strtotime($rows->end_time);
                $batch_venue_id = explode(',',$rows->batch_venue_id);
                $common_venue_id = array_intersect($id,$batch_venue_id);
               if($common_venue_id)
                {
             if($exist_batch_start_date <= $new_batch_start_date && $exist_batch_end_date >= $new_batch_start_date )
    {
     if($batch_time1 <= $new_batch_start_time && $batch_time2 > $new_batch_start_time){

          $msg = "Please Change Time Slot or Start And End Date. It's already booked"; 
          $response['error'] = false;
          $response['msg']   = $msg;
         break;
        }
        }
        }
else 
    {
      $result = $this->Batch_model->createBatch($data);
       if ($result) {
         $response['error'] = true;
         $response['msg']   = "Batch Created";
      }
        }
        }
        else 
            {
              $result = $this->Batch_model->createBatch($data);
              if ($result) {
                 $response['error'] = true;
                 $response['msg']   = "Batch Created";
              }
              echo json_encode($response);

Ajax

success: function(response)
            {
             var data = JSON.parse(response);
             if (data.error == true)
             { //some code here} 
             else {//some code here}
            }

根据Gaurav Genius建议的答案。我尝试过

  $get_batch_details = $this->Batch_model->fetchBatches(); 
    if(!empty($get_batch_details))
    {
      foreach ($get_batch_details as $rows)
      {
        $exist_batch_start_date =  $rows->start_date;
        $exist_batch_end_date =  $rows->end_date;
        echo $batch_time1 =  strtotime($rows->start_time);
        echo $batch_time2 =  strtotime($rows->end_time);
        $batch_venue_id = explode(',',$rows->batch_venue_id);
        $common_venue_id = array_intersect($id,$batch_venue_id);

if($common_venue_id == $venue_id && ($new_batch_start_date < $new_batch_end_date && $new_batch_start_date >= $exist_batch_start_date && $new_batch_end_date <= $exist_batch_end_date) && 
($new_batch_start_time < $new_batch_end_time && $new_batch_start_time >= $batch_time1 && $new_batch_end_time >= $batch_time2))
  {
  $msg = "Please Change Time Slot or Start And End Date"; 
  $response['error'] = false;
   $response['msg']   = $msg;
   print_r($response);
}else{
      $result = $this->Batch_model->createBatch($data);
      if ($result) {
         $response['error'] = true;
         $response['msg']   = "Batch Created";
      }
}
echo json_encode($response);
      }
    }
else 
    {
      $result = $this->Batch_model->createBatch($data);
      if ($result) {
         $response['error'] = true;
         $response['msg']   = "Batch Created";
      }
  }
echo json_encode($response);

1 个答案:

答案 0 :(得分:0)

您可以构建这样的逻辑...条件可以根据需要进行修改...

if(database.venue_id == form.venue_id && (form.start_date < form.end_date && form.start_date >= database.start_date && form.end_date <= database.end_date) && 
(form.start_time < form.end_time && form.start_time ......)){
    //show error;
}else{
    //insert record;
}

如果您想为每次验证提供自定义错误消息...

使用嵌套的if..elseif ... elseif ... elseif .... else