【问题标题】:How to call json from multiple nested if condition如何从多个嵌套的if条件中调用json
【发布时间】:2019-01-20 21:45:05
【问题描述】:

我正在使用 CodeIgniter。我正在做一个批处理列表的小项目。现在,如果管理员想要创建批处理列表,那么应该输入开始日期和结束日期以及开始时间和结束时间,那么它将在数据库中检查批处理是否在相同的日期和时间运行?如果是,那么它将显示消息,如果不是,它将创建一个新的批处理列表。

如果日期相同,时间应该不同。

现在我的逻辑是, 我将第一个 new_start_date 与exist_start_date 和exist_end_date 进行比较,如果在两者之间找到日期,那么它将检查时间。

到目前为止,它一直在进行比较。即使它正在检查时间,但如何从那里退出进程并调用 JSON?因为从那里我的 JSON 不起作用。

我从那里添加了"echo "time not match";" 我无法调用 JSON 我在网络选项卡上获取输出。

我得到了输出

enter 1enter 2{"error":true,"msg":"Batch Created"}time not match

你能帮我解决这个问题吗?

 $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');
    $days                       = implode(',',$this->input->post('days'));
    //print_r($days);   

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

    //convert in Time Format
      $new_batch_start_time = strtotime($new_batch_start_time);   
      $new_batch_end_time = strtotime($new_batch_end_time);

      $venue = $this->input->post('name');
      $data = array(      
          'activity_list_id' => $this->input->post('activity_name'),          
          'batch_venue_id'   => $venue_id,      
          'batch_name'       => $this->input->post('batch_name'),     
          'start_date'       => date('Y-m-d',strtotime($this->input->post('start_date'))),     
          'end_date'         => date('Y-m-d',strtotime($this->input->post('end_date'))),     
          'start_time'       => $this->input->post('start_time'),     
          'end_time'         => $this->input->post('end_time'),     
          'total_capacity'   => $this->input->post('total_capecity'),
          'batch_status'     => 1,
          'created_by'       => trim($this->session->userdata['login_data']['user_id']),
          'created_date'     => date('d-m-Y h:i:s A'),
          'batch_days'     => $days
          );


    $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);
        //print_r($common_venue_id);
        if($common_venue_id)
        {
          echo "enter 1";
        //if new batch start date  between existing batch start date 
        if($exist_batch_start_date <= $new_batch_start_date && $exist_batch_end_date >= $new_batch_start_date ){ 

          echo "enter 2";
          if($batch_time1 <= $new_batch_start_time && $batch_time2 > $new_batch_start_time){
                 $msg = "Other Batch Alredy Running On from Date $batch_start_date to $exist_batch_end_date on Time : $batch_time1 to $batch_time2.
                    Please Change Time Slot or Start And End Date"; 
                  $response['error'] = false;
                  $response['msg']   = $msg;
                  echo json_encode($response);
                  exit;
            }
            else{
              $result = $this->Batch_model->createBatch($data);
              echo "time not match";
              print_r($result);
            }
            break;
      }

     //if date is different 
 else 
    {
      $result = $this->Batch_model->createBatch($data);
    }    
}else 
    {
      $result = $this->Batch_model->createBatch($data);
    }
}
}
//first time creating batch
else 
    {
      $result = $this->Batch_model->createBatch($data);
    }

莫贝尔

function createBatch($data){
        if($this->db->insert('batch_list',$data))
            {
                $response['error'] = true;
                $response['msg']   = "Batch Created";
                echo json_encode($response);
            }
            else 
            {
                $response['error'] = true;
                $response['msg']   = "Failed to Create Batch";
                echo json_encode($response);
            }                   
    }

function fetchBatches()
    {
    $result = $this->db->where(['batch_list.batch_status'=>1,'activity_list.act_status'=>1])
                            ->from('batch_list')
                            ->join('activity_list','activity_list.activity_id = batch_list.activity_list_id')
                            ->get()
                            ->result();
        return $result;
    }

Ajax

success: function(response){
                    var data = JSON.parse(response);
                    if (data.error == true){                        
                        swal({
                             title: "Success", 
                             text: data.msg ,
                             type: "success"
                             }).then(function(){ 
                                location.reload();
                            }
                    );
                    } else {
                        swal({  
                            title: "Warning",                       
                             text: data.msg ,
                             type: "warning"
                            });
                    }
         }

你能帮我解决这个问题吗?

【问题讨论】:

  • 谁能帮我解决这个问题?
  • 发布您的 fetchBatches 模型 - 我在您的代码中看不到任何您实际从管理员那里检索开始和结束日期的内容...
  • @sintakonte,是的,我在模型部分进行了更新。请检查一下
  • @sintakonte,我得到了输出。即使最后一个其他条件也有效。即使我在第一个 else 中得到输出,但无法从第一个和第二个 else 将 JSON 发送到 ajax
  • 我不明白......我引用你的话:then should enter the start date and end date and start time and end time then it will check 管理员输入的那些日期在哪里?你从来没有真正访问过它

标签: php jquery json html codeigniter-3


【解决方案1】:

您的整个方法有点混乱,因为您发现自己处于大量冗余代码片段中,没有人能够理解您到底想要什么 - 我在这里给您一些提示,包括基于您的代码的示例

  1. 使用例外 - 它非常适合您的情况 - 如果出现问题 - 停止它
  2. 尝试将您的需求过滤到一项任务并尝试解决它 - 然后再进行下一项任务
  3. 永远 - 永远记住 - 考虑一个术语 - 如果您在应用程序中发现重复相同的代码 - 您就知道有问题了 - 您应该重构它 -不要为冗余感到羞耻——它们总是会发生——但如果你找到它们,你必须重构那些代码 sn-ps

现在来看你的例子

你在这里的任务是什么?

  • 您可以尝试询问数据库是否已经在运行批处理 - 您不需要遍历整个表条目
  • 比较管理员输入的两个日期 - 如果开始日期在结束日期的未来,则立即停止应用程序
  • 你的交集对我来说不是很清楚你想在这里实现什么 - 但我真的相信你也可以在这里询问数据库(流行语:find_in_set

基于这些信息,我们现在可以开始开发;)(如果我没有所有东西,只需完成上面的列表并尝试实现您的任务)

控制器:

try
{
    $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');
    $days = implode(',',$this->input->post('days'));

    $objDateStart = DateTime::createFromFormat('Y-m-d h:i a', $new_batch_start_date.' '.$new_batch_start_time);
    $objDateEnd = DateTime::createFromFormat('Y-m-d h:i a', $new_batch_end_date.' '.$new_batch_end_time);

    if ($objDateEnd < $objDateStart)    throw new Exception('End Date Should be Greater than Start Date');

    if ($this->Batch_model->hasBatchesBetweenDates($objDateStart, $objDateEnd)) throw new Exception('Other Batch already running On from '.$objDateStart->format('d-m-Y H:i').' to '.$objDateEnd->format('d-m-Y H:i').'. Please Change Time Slot for Start and End Date'); 

    $data = array(      
        'activity_list_id' => $this->input->post('activity_name'),          
        'batch_venue_id'   => $venue_id,      
        'batch_name'       => $this->input->post('batch_name'),     
        'start_date'       => $objDateStart->format('Y-m-d'),     
        'end_date'         => $objDateEnd->format('Y-m-d'),     
        'start_time'       => $objDateStart->format('H:i'),     
        'end_time'         => $objDateEnd->format('H:i'),     
        'total_capacity'   => $this->input->post('total_capecity'),
        'batch_status'     => 1,
        'created_by'       => trim($this->session->userdata['login_data']['user_id']),
        'created_date'     => date('d-m-Y h:i:s A'),
        'batch_days'     => $days
    );

    $this->Batch_model->createBatch($data);
}
catch(Exception $e)
{
    $arrError = [
        'error' => false,
        'msg' => $e->getMessage()
    ];

    echo json_encode($arrError);
}

型号:

public function hasBatchesBetweenDates(DateTime $objDateStart, DateTime $objDateEnd)
{
    $query = $this->db
    ->from('batch_list')
    ->join('activity_list','activity_list.activity_id = batch_list.activity_list_id')
    ->where('CONCAT(start_date,\' \',start_time)  >=', $objDateStart->format('Y-m-d H:i:s'))
    ->or_group_start()
        ->where('CONCAT(end_date, \' \', end_time) <=', $objDateEnd->format('Y-m-d H:i:s'))
        ->where('CONCAT(end_date, \' \', end_time) >=', $objDateStart->format('Y-m-d H:i:s'))
    ->group_end()
    ->get();

    return ($query->num_rows() > 0);
}

我希望你理解这里的概念 - 如果你有问题 - 不要犹豫,问

【讨论】:

  • 给我一些时间来实现这个。
  • 真的很欣赏你的效果,是的,它正在工作,但不是 100%,因为我在我的问题中添加了代码。 $batch_venue_id = explode(',',$rows->batch_venue_id); $common_venue_id = array_intersect($id,$batch_venue_id);从数据库中检查。我正在检查 $common_venue_id。为什么是 $common_venue 因为我可以在同一天选择不同的场地。我们不能在同一日期和时间选择同一地点。
  • 让我解释一下,我在 14-08-18 到 20-08-18 的数据库中有一个场地 UK。现在我选择印度和日期 14-08-18 到 20-08-18。它应该创建它。因为我的 venu id 不同。
  • 如果 $common_venue id 相同且日期也相同但时间不同,则可以创建批处理列表。例如,一个常见的 id 是 1,日期是 15-08-2018,时间是早上 6:00 到早上 8:00,数据库中已经存在。如果管理员选择公共 ID 1 和日期 15-08-2018 但时间应该不同,例如上午 8:00 到上午 10:00。如果时间是早上 6:00 到早上 8:00,则管理员无法创建
  • 这方面还有什么帮助吗?
猜你喜欢
  • 1970-01-01
  • 2020-02-25
  • 2018-01-10
  • 1970-01-01
  • 2016-12-16
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
相关资源
最近更新 更多