我正在研究复选框过滤器,我无法在查询原因中处理多个复选框值

时间:2017-04-04 10:40:47

标签: php codeigniter

我在数组中设置检查值并将该数组传递给模型函数,我没有得到如何处理查询的where子句中的复选框值

我的观看页面:

<form id="filterform" method="post" action="<?php echo base_url(); ?>index.php/welcome/forFilter?id=<?php echo $_GET['id']; ?>&name=<?php echo $_GET['name']; ?>">">
   <?php foreach ($supplier as $menu1) { ?>
     <div class="checkbox">
       <input type="checkbox"  name="state[]" id="" value="<?php echo $menu1->state_name; ?>" class="custm-position">
       <label><?php echo $menu1->state_name; ?></label>
     </div>
   <?php } ?>
 </form>

我的控制器代码:

 public function forFilter(){
      $id = $_GET["id"];
      $total_row = $this->productdisplay_model->filter_count($id,$state_id);
      $this->applyFilter($state_id);
    }

    public function applyFilter($state_id){
        $data['ListMenuLevel1'] = $this->Categories_model->listsector1();
        $data['supplier'] = $this->supplier_select_model->select_supplier();
        $config = array();
        $config["base_url"] = base_url() . "index.php/welcome/applyFilter";
        $total_row = $this->productdisplay_model->filter_count($this->session->userdata('forfilter_id'),$state_id);
        $config['total_rows'] = $total_row;
        $config['per_page'] = 20;
        //$config['uri_segment'] = 3;
        $config['use_page_numbers'] = TRUE;
        //$config['page_query_string'] = TRUE;
        //$config['reuse_query_string'] = FALSE;
        $config['num_links'] = 1;
        $config['cur_tag_open'] = '&nbsp;<a class="current">';
        $config['cur_tag_close'] = '</a>';
        $config['next_link'] = 'Next';
        $config['prev_link'] = 'Previous';
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        /* if($this->uri->segment(3)){
          $page = ($this->uri->segment(3)) ;
          }
          else{
          $page = 1;
          } */
        $page = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 1);
        //$offset = $config['per_page']*$page;
        $offset = (($config['per_page']) * ($page - 1));
        $limit = $config['per_page'];

       $str_links = $this->pagination->create_links();
        $data["links"] = explode('&nbsp;', $str_links);
        $data['sub3products'] = $this->productdisplay_model->filter_result($this->session->userdata('sub3_id'),$limit, $offset,$state_id);
        $this->load->view('productdetails', $data);
    }

我的模特:

public function filter_count($id,$state_id){
        $this->db->select('*');
        $this->db->from('sub3_category');
        $this->db->where('sub2_categoryid_fk', $id);
        $this->db->order_by("sub3_category.sub2_categoryid_fk ");
        $query = $this->db->get();
        return  $query->num_rows();
    }

     public function filter_result($id,$limit, $offset,$state_id){
         $this->db->select('*');
        $this->db->from('sub3_category');
        $this->db->where('sub2_categoryid_fk', $id);
        $this->db->order_by("sub3_category.sub2_categoryid_fk ");
        $this->db->join('supplier_otherdetails', 'sub3_category.supplier_id_fk=supplier_otherdetails.sup_id', 'left outer');
        $this->db->limit($limit, $offset);
        return $this->db->get()->result();
    }

我的jquery

<script>
$(document).ready(function(){
    $("#filterform").on("change", "input:checkbox", function(){
        $("#filterform").submit();
    });
});
</script>

1 个答案:

答案 0 :(得分:0)

更新后的视图页

<form id="filterform" method="post" action="<?php echo base_url(); ?>index.php/welcome/forFilter?id=<?php echo $_GET['id']; ?>&name=<?php echo $_GET['name']; ?>">">
   <?php foreach ($supplier as $menu1) { ?>
     <div class="checkbox">
       <?php $checked = (in_array($menu1->state_name, $state_ids) ? 'checked="checked"' : ''); ?>
       <input type="checkbox"  name="state[]" id="" value="<?php echo $menu1->state_name; ?>" class="custm-position" <?php echo $checked; ?> />
       <label><?php echo $menu1->state_name; ?></label>
     </div>
   <?php } ?>
 </form>

更新了控制器代码:

public function forFilter(){
    $id = $this->input->get('id', true);
    $state_id= $this->input->post('state', true);
    $total_row = $this->productdisplay_model->filter_count($id,$state_id);
    $this->applyFilter($state_id);
}

public function applyFilter($state_id){
    $data['ListMenuLevel1'] = $this->Categories_model->listsector1();
    $data['supplier'] = $this->supplier_select_model->select_supplier();
    $config = array();
    $config["base_url"] = base_url() . "index.php/welcome/applyFilter";
    $total_row = $this->productdisplay_model->filter_count($this->session->userdata('forfilter_id'),$state_id);
    $config['total_rows'] = $total_row;
    $config['per_page'] = 20;
    //$config['uri_segment'] = 3;
    $config['use_page_numbers'] = TRUE;
    //$config['page_query_string'] = TRUE;
    //$config['reuse_query_string'] = FALSE;
    $config['num_links'] = 1;
    $config['cur_tag_open'] = '&nbsp;<a class="current">';
    $config['cur_tag_close'] = '</a>';
    $config['next_link'] = 'Next';
    $config['prev_link'] = 'Previous';
    $this->load->library('pagination');
    $this->pagination->initialize($config);
    /* if($this->uri->segment(3)){
      $page = ($this->uri->segment(3)) ;
      }
      else{
      $page = 1;
      } */
    $page = ($this->uri->segment(3) != '' ? $this->uri->segment(3) : 1);
    //$offset = $config['per_page']*$page;
    $offset = (($config['per_page']) * ($page - 1));
    $limit = $config['per_page'];

    $str_links = $this->pagination->create_links();
    $data["links"] = explode('&nbsp;', $str_links);
    $data['sub3products'] = $this->productdisplay_model->filter_result($this->session->userdata('sub3_id'),$limit, $offset,$state_id);

    $data['state_ids'] = $state_id; //new line added

    $this->load->view('productdetails', $data);
}