如何在Codeigniter中过滤数据

时间:2016-09-28 03:01:33

标签: php html5 codeigniter

我的代码有问题,我想按2类过滤。但在我所做的工作中,只需过滤一个类别就可以工作,而过滤其他类别也无效。有人可以帮我解决这个问题吗?

这是我的控制器

public function index($state = null , $category = null)
{
    if (!empty($state)) {
    $this->load->model('M_kegiatan');
    $data['username'] = $this->session->userdata('username');
    $data['kegiatan'] = $this->M_kegiatan->get_status($state);
    $this->load->view('beranda', $data); 
    }else if (!empty($category)) { 
    $this->load->model('M_kegiatan');
    $data['username'] = $this->session->userdata('username');
    $data['kegiatan'] = $this->M_kegiatan->get_kategori($category);
    $this->load->view('beranda', $data);
} else {

    $this->load->model('M_kegiatan');
    $data['username'] = $this->session->userdata('username');
    $data['kegiatan'] = $this->M_kegiatan->get_kegiatan();
    $this->load->view('beranda', $data);
}

这是我的模特:

    public function get_status($status) {
    $this->db->order_by('tanggal','desc');
    $this->db->where('status',$status);
    $query = $this->db->get('kegiatan');

    if ($query->num_rows() > 0) {
        return $query -> result();
    }
}
    public function get_kategori ($kategori) {
    $this->db->order_by('tanggal','desc');
    $this->db->where('kategori',$kategori);
    $query = $this->db->get('kegiatan');

    if ($query->num_rows() > 0) {
        return $query -> result();
    }
}

从这段代码中,只需按get_status()过滤即可,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,我认为这就是你要找的。

public function get_filtered ($status, $category) 
{
   $this->db->order_by('tanggal','desc');

   If (!empty($status))
   {
     $this->db->where('status',$status);
   }

   If (!empty($category))
   {
      $this->db->where('kategori',$category);
   }

   $query = $this->db->get('kegiatan');
   return ($query->num_rows() > 0) ? $query->result() : NULL;

将此功能添加到模型

function myFunction() {
window.open("mydomain/?a=" + document.getElementById('text-one').value +   '&b=' + document.getElementById('text-two').value + '&c=' +   document.getElementById('text-three').value;
}