Codeigniter,在日期中搜索未获取数据库中的值

时间:2017-04-05 04:09:25

标签: php postgresql codeigniter

我使用关键字和值以及一些分页来构建一些搜索功能。但是当我尝试在查询之间使用日期范围时,它不仅获取正确的值并传递数据库中的所有值。但是当我尝试使用关键字值时,它的工作只能获得正确的值。

模型

function get_allAspirasi($batas =null,$offset=null,$key=null,$start_date=null,$end_date=null) {
        $selqry = 'a.* , s.id_status, s.nama_status';
        $this->db->select($selqry);
        $this->db->from('t_aspirasi as a');
        $this->db->join('t_status s', 's.id_status = a.id_status', 'left');
        $this->db->order_by('a.id_aspirasi', 'DESC');
        if($start_date != null && $end_date != null){
            /*$this->db->where('a.tgl_input BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" AND "'. date('Y-m-d', strtotime($end_date)).'"');*/
            $this->db->where('a.tgl_input >=', date('Y-m-d', strtotime($start_date)));
            $this->db->where('a.tgl_input <=', date('Y-m-d', strtotime($end_date)));
        }
        if($batas != null){
           $this->db->limit($batas,$offset);
        }
        if ($key != null) {
           $this->db->or_like($key);
        }
        $query = json_decode(json_encode($this->db->get()->result()), TRUE);
        return $query;
    }

控制器

public function cari()
    {   
        $this->breadcrumb->append('Halaman Utama','admin_unit');
        $this->breadcrumb->append('Kelola Aspirasi');

        date_default_timezone_set('Asia/Jakarta');
        $key= $this->input->get('key'); //method get key
        $page=$this->input->get('per_page');  //method get per_page
        $start_date = $this->input->get('from');
        $end_date = $this->input->get('to');

        $search=array(
            'perihal'=> $key,
            'nama_status' => $key
        ); //array pencarian yang akan dibawa ke model

        $batas=1; //jlh data yang ditampilkan per halaman
        if(!$page):     //jika page bernilai kosong maka batas akhirna akan di set 0
           $offset = 0;
        else:
           $offset = $page; // jika tidak kosong maka nilai batas akhir nya akan diset nilai page terakhir
        endif;

        $config['page_query_string'] = TRUE; //mengaktifkan pengambilan method get pada url default
        $config['base_url'] = base_url().'admin_unit/cari?'.$key;   //url yang muncul ketika tombol pada paging diklik
        $config['total_rows'] = $this->m_home->count_aspirasi_search($search,$start_date,$end_date); // jlh total barang
        $config['per_page'] = $batas; //batas sesuai dengan variabel batas

        $config['uri_segment'] = $page; //merupakan posisi pagination dalam url pada kesempatan ini saya menggunakan method get untuk menentukan posisi pada url yaitu per_page

        $config['full_tag_open'] = '<ul class="pagination">';
        $config['full_tag_close'] = '</ul>';
        $config['first_link'] = '&laquo; First';
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = 'Last &raquo;';
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '&larr; Prev';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="current"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        $this->pagination->initialize($config);
        $data['paging']=$this->pagination->create_links();
        $data['jlhpage']=$page;

        $data['qbarang'] = $this->m_home->get_allAspirasi($batas,$offset,$search,$start_date,$end_date); //query model semua barang
        print_r($data);

查看

<div class="tujuan-surat grup">

    <form class="form-inline" method="get" action="<?php echo base_url('admin_unit/cari');?>">     
        <div class="form-group col-md-7">
            <label class="control-label input-sm col-md-2" for="from">Periode</label>
            <div class="input-group col-md-10">
                <input class="form-control input-sm" type="text" id="from" name="from" placeholder="tanggal awal">
                <span class="input-group-addon"> s/d </span>          
                <input class="form-control input-sm" type="text" id="to" name="to" placeholder="tanggal akhir">        
            </div>
        </div>
        <div class="form-group" style="margin-right: 10px;">
            <input type="text" id="key" name="key" class="form-control input-sm" placeholder="cari aspirasi">                 
        </div>
        <button type="submit" class="btn-uin btn-inverse btn btn-small">Cari</button>
    </form>

</div>

即使我在dateinput中输入日期,它也会继续获取所有值。 我不知道为什么,但我认为,它来自我的模型的错误。

提前感谢!

1 个答案:

答案 0 :(得分:0)

你的模型应该是这样的:

function get_allAspirasi($batas =null,$offset=null,$key=array(),$start_date=null,$end_date=null) {

    if($batas != null){
       $this->db->limit($batas,$offset);
    }

    $this->db->select('a.* , s.id_status, s.nama_status');
    $this->db->from('t_aspirasi as a');
    $this->db->join('t_status s', 's.id_status = a.id_status', 'left');
    $this->db->order_by('a.id_aspirasi', 'DESC');
    if($start_date != null && $end_date != null){
        /*$this->db->where('a.tgl_input BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" AND "'. date('Y-m-d', strtotime($end_date)).'"');*/
        $this->db->where('a.tgl_input >=', date('Y-m-d', strtotime($start_date)));
        $this->db->where('a.tgl_input <=', date('Y-m-d', strtotime($end_date)));
    }

    if (!emppty($key)) {
       $this->db->like($key);
    }
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
}