如何使用两个日期选择器从我的数据库中获取数据。笨

时间:2013-10-15 12:20:36

标签: php jquery mysql codeigniter datepicker

我想在两个日期选择器之间选择数据库中的所有数据。请帮助我在这种情况下如何使用BETWEEN AND语句。 我从第一个日期选择器中选择一个日期然后数据显示,但是当我在第二个日期选择器上选择日期时,第二个日期选择器的日期数据仅显示第一个日期选择器已消失

我使用了这个查询

$this->db->where('date_sold >=',$this->input->post('selectdate'));

$this->db->where('date_sold <=',$this->input->post('selectdate2'));

但我只得到1个数据..

<span>Select 1st sem:</span>
                <div id="datetimepicker1" class="input-append date">
                    <input data-format="MM/dd/yyyy" type="text" readonly name="selectDate" id="selectDate"></input>                                 
                    <span class="add-on">
                      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                      </i>                                                        
                    </span>
                </div>
                <script type="text/javascript">

                 $(function(){ 
                    var date = new Date();
                    date.setDate(date.getDate());

                      $('#datetimepicker1').datetimepicker({                            
                            pickTime: false
                      });

                  });
                </script>   

                <span>Select 2st sem:</span>
                <div id="datetimepicker2" class="input-append date">
                    <input data-format="MM/dd/yyyy" type="text" readonly name="selectDate" id="selectDate2"></input>                                    
                    <span class="add-on">
                      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                      </i>                                                        
                    </span>
                </div>

                <script type="text/javascript">

                 $(function(){ 
                    var date = new Date();
                    date.setDate(date.getDate());
                      $('#datetimepicker2').datetimepicker({                            
                            pickTime: false
                      });

                  });
                </script>   

/这是我的观点

    function get_semreportdata(){
    $q = $this->db->select('
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
             reserved_books.item_sold,
             reserved_books.date_sold,
             reserved_books.total_amount
            ')
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn')
        ->where('date_sold >=',$this->input->post('selectdate'))
        ->where('date_sold <=',$this->input->post('selectdate2'));



        $reserved['rows'] = $q->get()->result();
        $this->db->select_sum('total_amount');
        $this->db->where('date_sold',$this->input->post('selectdate'));

        $query = $this->db->get('reserved_books');
        $reserved['total'] = $query->result();

        return $reserved;

}

这是我的模特

3 个答案:

答案 0 :(得分:1)

您的两个字段都在代码示例中命名为 selectDate 。我想这就是你的原始查询不起作用的原因。尝试将第二个字段重命名为 selectdate2

答案 1 :(得分:0)

我会说这样的话:

其中'date_sold'介于“。$ this-&gt; input-&gt; post('selectdate')之间。”和“。$ this-&gt; input-&gt; post('selectdate')。”

假设您已为数据库表格提供了正确格式的提交日期。

答案 2 :(得分:0)

尝试一下,确保您有适当类型的date_sold日期或日期时间,并且还要关注已发布的输入,它们包含date_sold列所具有的确切日期格式。我已将三个案例用于两者之间包含单个日期数据。我通过将第三个参数传递为false

来使用自定义字符串
    $datef=$this->input->post('selectdate');
    $dates=$this->input->post('selectdate2');
    $q = $this->db->select('
             books.title,
             reserved_books.id,
             reserved_books.isbn, 
             reserved_books.price,
             reserved_books.item_sold,
             reserved_books.date_sold,
             reserved_books.total_amount
            ')
        ->from('reserved_books')
        ->join('books','reserved_books.isbn= books.isbn');

if(!empty($datef) && !empty($dates)){
$this->db->where(" `date_sold` BETWEEN '".$datef."' AND '".$dates."' ", NULL, FALSE); 

}elseif(!empty($datef)){
$this->db->where( " `date_sold` >=.'"$datef."' ", NULL, FALSE);

}elseif(!empty($dates)){

$this->db->where(" `date_sold` <=.'".$dates."' ", NULL, FALSE);
}

Reference

  

$ this-&gt; db-&gt; where()接受可选的第三个参数。如果你设置它   为FALSE,CodeIgniter不会尝试保护您的字段或表格   带反叛的名字。

Active Record