在 CodeIgniter 中按日期过滤 MySQL 条目

Posted

技术标签:

【中文标题】在 CodeIgniter 中按日期过滤 MySQL 条目【英文标题】:Filtering MySQL entries by date in CodeIgniter 【发布时间】:2021-11-25 12:53:16 【问题描述】:

目前我正在使用 CodeIgniters MVC 框架来获取我在特定时间段之间的记录。我对下拉列表进行了相同的过滤并获得了结果,但我不知道如何使用日期选择器进行相同的操作。以下是我的视图模型:

<html lang="en">
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>    
</head>
<body>
        <div id="statusModal">
    <input type="date" name="startDate" class="startDate" id="startDate"/>
    <input type="date" name="endDate" class="endDate" id="endDate"/>
    <select name="status" class="status" id="status">
        <option>Draft</option>
        <option>Unpublish</option>
        <option>Publish</option>
    </select>
    <br/><br/>
    <button type="button" class="alertbox" id="alertbox" name="alertbox">alertbox</button>
    <br/><br/>    
    <div id="result"></div>
    </div>
    </form>
</body>
</html>

<script>

    $(document).ready(function()

        load_status();
        
    $('#alertbox').click(function()
        var startDate = $('#startDate').val();
        var endDate = $('#endDate').val();
        if(startDate != '' && endDate != '')
        $.ajax(
            url:"<?php echo base_url(); ?>testcontroller/fetch_dat",
            method:"POST",
            data:startDate:startDate, endDate:endDate,
            success:function(data)
                $('#result').html(data)
            
        )
        else
            alert("Please enter a date");
        
    )

        function load_status(status)
            $.ajax(
                url:"<?php echo base_url(); ?>testcontroller/fetch_stat",
                method:"POST",
                data:status:status,
                success:function(data)
                    $('#result').html(data)
                
            )
        

        $('#status').click(function()
            var search = $(this).val();
            if(search != '')
                load_status(search);
            
            else
                load_status();
            
        )
    )

</script>

控制器类:

class Testcontroller extends CI_Controller
     
      public function index() 
       
      $this->load->view('crm/test');      
       
         function fetch_dat()
            $output ='';
            $startDate = '';
            $endDate = '';
            $this->load->model('crm/user_model');
  
            if($this->input->post('startDate'))
              $startDate = $this->input->post('startDate');
            
            if($this->input->post('endDate'))
               $endDate = $this->input->post('endDate');
             
            $data = $this->user_model->fetch_date($startDate,$endDate);
            
            $output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Customer id</th>
                    <th>Title</th>
                    <th>Added Date</th>
                    <th>Status</th>
  
                    </tr>
              ';
           if($data->num_rows() > 0)
           
              foreach($data->result() as $row)
              
              $output .= '
                 <tr>
                 <td>'.$row->id.'</td>
                 <td>'.$row->title.'</td>
                 <td>'.$row->added_date.'</td>
                 <td>'.$row->status.'</td>
                 </tr>
              ';
              
           
           else
           
              $output .= '<tr>
                 <td colspan="5">No Data Found</td>
                 </tr>';
           
           $output .= '</table>';
           echo $output;
           

         function fetch_stat()
            $output ='';
            $status = '';
            $this->load->model('crm/user_model');
  
            if($this->input->post('status'))
              $status = $this->input->post('status');
            
            $data = $this->user_model->fetch_status($status);
            
            $output .= '
              <div class="table-responsive">
                 <table class="table table-bordered table-striped">
                    <tr>
                    <th>Customer id</th>
                    <th>Title</th>
                    <th>Added Date</th>
                    <th>Status</th>
  
                    </tr>
              ';
           if($data->num_rows() > 0)
           
              foreach($data->result() as $row)
              
              $output .= '
                 <tr>
                 <td>'.$row->id.'</td>
                 <td>'.$row->title.'</td>
                 <td>'.$row->added_date.'</td>
                 <td>'.$row->status.'</td>
                 </tr>
              ';
              
           
           else
           
              $output .= '<tr>
                 <td colspan="5">No Data Found</td>
                 </tr>';
           
           $output .= '</table>';
           echo $output;
           
           

还有我的模型课:

function fetch_status($status)
        $this->db->select(*);
        $this->db->from(crm_listings);
        if($status != '')
            $this->db->where('status',$status);
        
        $this->db->order_by('id','DESC');
        return $this->db->get();
    

    function fetch_date($startDate,$endDate)
        $this->db->select(*);
        $this->db->from(crm_listings);
        if($startDate != '' && $endDate != '')
            $this->db->where('added_date BETWEEN "' . $startDate . '" AND "' . $endDate . '"',$startDate,$endDate);
        
        $this->db->order_by('id','DESC');
        return $this->db->get();
    

【问题讨论】:

数据库表中added_date的数据类型是什么? @umefarooq 现在是日期时间 你能告诉我 $startDate 和 $endDate 格式是相同的日期时间还是只是日期? @umefarooq 我该如何检查?这两个变量都是我从输入标签中选择的变量,它们都必须引用我数据库中的 added_date 列 在您的控制器中获取简单地 echo $startDate 和 endDate 和 die();您将能够看到日期格式 【参考方案1】:

您好,毕竟上面帖子中的所有 cmets,因为您得到的是 Y-m-d 格式的 $startDate 和 $endDate,这是我的答案,您需要将 added_date 转换为日期,在此处查询是如何做到的。

 function fetch_date($startDate,$endDate)
        $this->db->select(*);
        $this->db->from(crm_listings);
        if($startDate != '' && $endDate != '')
            $this->db->where('cast(added_date as date) BETWEEN "' . $startDate . '" AND "' . $endDate . '"',null,false);
        
        $this->db->order_by('id','DESC');
        return $this->db->get();
    

它会为你工作。

【讨论】:

嘿,很抱歉打扰您,但我在语句中添加了一个 count(*) 来获取数据的行数,但我无法获得要显示的数字。我认为这与我的控制器类有关,但我不知道要更改什么 你能分享你的选择吗? 和你的代码一样,只是改了 $this->db->select("count(*)"); @umefarooq 先生,请描述一下cast() @Jay Vijay Modi 你的下一个任务是什么?

以上是关于在 CodeIgniter 中按日期过滤 MySQL 条目的主要内容,如果未能解决你的问题,请参考以下文章

如何在codeigniter中按日期降序获取订单

在 Gatsby 和 Contentful 中按日期过滤

如何在 django 中按日期范围过滤记录?

在 asp.net 中按日期过滤 DirectoryInfo 文件

在 Django admin 中按自定义日期范围过滤

Office 365 日历 API - 在 C# 客户端库中按日期过滤事件