Php 带有搜索过滤器的Codeigniter分页

Php 带有搜索过滤器的Codeigniter分页,php,codeigniter,search,pagination,filtering,Php,Codeigniter,Search,Pagination,Filtering,我一直致力于在codeigniter中实现一种搜索方法,该方法使用codeigniter分页功能。 这需要一点前期研究,因为我们包括过滤器,用户可以选择不同数量的过滤器, 抛出uri_段计数。我在下面有一个可行的解决方案(只发布了我认为与理解相关的代码) 这一过程是为了避免混淆。如果我错过了什么,请告诉我),如果有人遇到同样的问题,这会让他们开始 问题,但想问几个问题: 问题: 有没有更好、更有效的方法对过滤后的搜索结果进行分页 有人会做些什么来改进这种方法吗 在初次发布后,如何在用户浏览器中显

我一直致力于在codeigniter中实现一种搜索方法,该方法使用codeigniter分页功能。 这需要一点前期研究,因为我们包括过滤器,用户可以选择不同数量的过滤器, 抛出uri_段计数。我在下面有一个可行的解决方案(只发布了我认为与理解相关的代码) 这一过程是为了避免混淆。如果我错过了什么,请告诉我),如果有人遇到同样的问题,这会让他们开始 问题,但想问几个问题:

问题:

  • 有没有更好、更有效的方法对过滤后的搜索结果进行分页

  • 有人会做些什么来改进这种方法吗

  • 在初次发布后,如何在用户浏览器中显示当前uri?分页库的工作方式 在通过生成的链接单击“first_url”之前,它不会显示“first_url”。我在胡思乱想一个改变方向的主意,但是 想在走那条路之前得到反馈

  • 查看

    <!-- SEARCH FILTERS -->
    <form action="/account/classified/browse" method="post" id="searchForm">
    
    <p>Search
    <br/>
    <input type="text" name="phrase" id="searchString" value="<?php echo $selectedPhrase; ?>"/></p>
    
    <p>Type<br/>
    <?php echo form_dropdown('type', $types, $selectedType); ?>
    </p>
    
    <p>County<br/>
    <?php echo form_dropdown('location', $locations, $selectedLocation); ?>
    </p>
    
    <p>Market<br/>
    <?php echo form_dropdown('market', $markets, $selectedMarket); ?>
    </p>
    
    <p>
        <input type="submit" id="searchClassifiedButton" name="" value="Search"/>
    
    </p>
    
    </form>
    <table>
    
    <?
    
    /*
        Results returned
    */
    if (count($classifieds) > 0) {
        foreach ($classifieds as $classified) {
            echo '<tr>';
    
            //rows returned from query using search filters
    
            echo '</tr>';
        }
    } else {
        echo 'There are no classifieds posted at this time';
    }
    ?>
    
    </table>
    

    通过使用DataMapperORM,您可以大大减少这段代码。插入后期搜索时,您只需执行
    $object->get_paged()
    ,并将
    where_in
    having
    等添加到对象定义中即可。这样,您几乎不需要编写代码,而且几乎不需要任何工作就可以进行非常强大的搜索
    get_paged
    还为您提供了一个包含所有所需设置的漂亮数组。即使你需要一个特定的设置,它也不会显示;你可以自己加进去。最后,您可以将验证(如表单验证)放入DataMapper模型中。我已经浏览了文档,但还得再深入一点。如果它允许您用sql编写自己的查询,那就太完美了@Kyslik-我不确定我是否热衷于创建密钥并将它们存储在数据库中,而不需要调用数据库。有趣的解决办法似乎很管用。@nullReference是的,你可以这样做。DataMapper完全构建在本机ActiveRecord类之上。搜索功能可能比菜单本身更容易找到内容
    <?
    public function browse() {
        $this -> load -> helper("url");
        $this -> load -> library("pagination");
        $this -> load -> model('classified_model');
        $post = $this -> input -> post();
    
        /*
            Here we determine if the post array contains values. If it does, then we know that the
            user has clicked the search button and wishs to view results by different filters.
    
            If the post array is empty, but the uri segment contains values, this would mean that we
            are viewing listings narrowed by the search filters. 
    
            If the post array is empty as well as the uri segment, then we are viewing all results
            with no filters applied.
        */
        if ($post) {
            foreach ($post as $key => $val) {
                if ($val != '') {
                    $filters[$key] = $val;
                }
            }
        } elseif ($this -> uri -> uri_to_assoc(4)) {
            $filters = $this -> uri -> uri_to_assoc(4);
        } else {
            $filters = array();
        }
    
        /*
            Here we create the config array for the pagination.
    
            We assign 'first_url' so that the first generated paginated link will contain the uri filter
    
            When filters array is not empty we assign 'suffix' so that the uri segment can be appended to the link AFTER the page offest
        */
        $pageConfig = array();
        $pageConfig['first_url'] = !empty($filters) ? '/account/classified/browse/0/' . $this -> uri -> assoc_to_uri($filters) : '/account/classified/browse/0';
        $pageConfig["suffix"] = !empty($filters) ? '/' . $this -> uri -> assoc_to_uri($filters) : '';
        $pageConfig["base_url"] = "/account/classified/browse/";
        $pageConfig["per_page"] = 15;
        $pageConfig["uri_segment"] = 3;
        $pageConfig["total_rows"] = $this -> classified_model -> approved_classifieds_count($filters);
    
    
        $this -> pagination -> initialize($pageConfig);
    
    
        $page = ($this -> uri -> segment(3)) ? $this -> uri -> segment(3) : 0;
    
    
        $data['classifieds'] = $this -> classified_model -> approved_classifieds($filters, $pageConfig["per_page"], $page);
        $data['links'] = $this -> pagination -> create_links();
    
    
        //PHRASE
        $data['selectedPhrase'] = (isset($filters['phrase'])) ? $filters['phrase'] : '';
    
        //TYPES
        $returnTypes = array('' => '-- View All --');
        foreach($this->classified_model->classified_search_types() as $type){
            $returnTypes[$type->id] = $type->name;
        }
        $data['types'] = $returnTypes;
        $data['selectedType'] = (isset($filters['type'])) ? $filters['type'] : '';
    
        //MARKETS
        $returnMarkets = array('' => '-- View All --');
        foreach($this->classified_model->market_types() as $market){
            $returnMarkets[$market->id] = $market->name;
        }
        $data['markets'] = $returnMarkets;
        $data['selectedMarket'] = (isset($filters['market'])) ? $filters['market'] : '';
    
        //COUNTIES
        $returnLocations = array('' => '-- View All --');
        foreach($this->classified_model->locations() as $key => $val){
            $returnLocations[$key] = $val;
        }
        $data['locations'] = $returnLocations;
        $data['selectedLocation'] = (isset($filters['location'])) ? $filters['location'] : '';
    
    
        //using phil sturgeon's template library
        $this -> template -> build('browse_classified', $data);
    
    
    }
    
    public function classified_search_types(){
        $q = $this->db->query("SELECT * FROM classified_types");
        return $q->result();
    }
    
    public function market_types(){
        $query = $this -> db -> get('market_types');
        return $query -> result();
    }
    
    public function locations(){
        $query = $this -> db -> get('locations');
        return $query -> result();
    }
    
    public function approved_classifieds_count($filters){
        $result = $this->search($filters);
        return $result->num_rows();
    }
    
    public function approved_classifieds($filters, $limit, $start) {
        $result = $this->search($filters, $limit, $start);
        return $result->result();
    }
    
    function search($filters, $limit='', $start=''){
        $this->load->helper('security');
    
        /*
            Obtain values from filters array, set to '' if value not in array
        */
        $phrase = xss_clean((isset($filters['phrase'])) ? $filters['phrase'] : '');
        $type = xss_clean((isset($filters['type'])) ? $filters['type'] : '');
        $market = xss_clean((isset($filters['market'])) ? $filters['market'] : '');
        $location = xss_clean((isset($filters['location'])) ? $filters['location'] : '');
    
    
        /*
            QUERY LOGIC
        */
    
    
        $q = $this->db->query($query);
    
    
        return $q;
    
    }