2016-05-04 85 views
-1

您好,我想在复选框笨过滤...但它显示无法正常输出......如果有人知道这个请尽量解决这个笨复选框过滤

这是我的控制器

<?php 
class Check extends CI_Controller { 
    public function laptops(){ 
    $this->load->model('check_m'); 
    $filter = array(
     'price' => $this->input->get('price'), 
     'name' =>$this->input->get('name') 
    ); 
    $data['laptop'] = $this->check_m->laptops_m($filter); 

    // echo json_encode($data['laptop']); 
    $this->load->view('check_view',$data); 
} 
} 
?> 

型号:

<?php 
class check_m extends CI_Model { 
    function laptops_m($filter = null){ 
    $this->db->select('*') 
      ->from('mobile_phones'); 
    // $query = $this->db->get('laptop_notebook')->result(); 
    // return $query; 
    if($filter['name']){ 
     $this->db->where('name', $filter['name']); 
    } 
    if($filter['price']){ 
     $this->db->where('price', $filter['price']); 
    } 
    $query = $this->db->get()->result(); 
    return $query; 
    } 
} 
?> 

查看

<input type="checkbox" name="name" value="acer" class="searcType">  
<input type="checkbox" name="name" value="lenovo">  
<input type="checkbox" name="price" value="1000"> 
<table> 
    <tbody> 
    <?php foreach ($laptop as $laptops_all) { ?> 
     <tr> 
     <td><p>Laptop <?php echo $laptops_all->name ?> </p></td> 
     </tr> 
    <?php } ?> 
    </tbody> 
</table> 

<script> 
    $('.searchType').click(function() { 
    alert($(this).attr('id')); //-->this will alert id of checked checkbox. 
    if(this.checked){ 
     $.ajax({ 
     url: localhost/code/check/laptops, 
     dataType: 'json', 
     success: function(data){ 
      $.each(data, function(index, element) { 
      $("tbody").empty(); 
      $("tbody").append("<tr><td>"+ 
       "Laptop "+element.brand+""+ 
       "</td></tr>"); 
      }); 
      } 
     }); 
    } 
    }); 
</script> 

在这里我要为过滤与复选框的项目......但我没有得到滤波输出....

+0

我想笨过滤复选框..动态...我想要做的是...这是正确的代码...或任何修改? – chandu

+0

你的问题不清楚。在复选框中点击你想过滤数据?对? 当匹配字符串使用like而不是where: $ this-> db-> like('name',$ filter ['name']); –

+0

亚..我已经尝试在核心php ..没关系......但在codeigniter它,s不工作 – chandu

回答

0

你不发送任何参数$这个 - >输入 - >获得()去接。

如果你只是发送GET 要求,你会不会需要发送csrf_token像在评论中提到的人。


您可能需要检查/设置一些配置变量的第一个。 这些都是的application/config/config.php文件

$config['uri_protocol'] = 'QUERY_STRING'; // OR AUTO 
$config['allow_get_array'] = TRUE; 
$config['enable_query_strings'] = TRUE; 

var BASEHREF = "<?php echo base_url();?>"; 

你的Ajax选项应该然后包括以下

{ 
    url : BASEHREF + 'check/laptops', 
    type : 'GET', 
    data : { name : 'toshiba', price : '1000'} 
} 
// GET http://yoursite.com/check/laptops?name=toshiba&price=1000