从第2页开始,按价格分页进行Codeigniter排序无效

时间:2018-08-21 03:39:55

标签: codeigniter sorting pagination

我正面临CI分页问题。现在排序都很好,但是当我尝试导航到第2页以上时,列表的排序将被“重新启动”。重新启动是指我第一次加载页面时返回第一种排序方法。

排序选项:最近,价格从低到高和价格从高到低

页面控制器:

public function index() {
    $query_string = $this->input->server('QUERY_STRING');
    $page_start = $this->input->get('page');
    $products = $this->Marketplace_model->get_products($query_string);
    $data['products'] = array_slice($products, $page_start, 12);

    $this->_doPagination($data['products'][0]["total_rows"], $test);

    $data['sorts'] = array("recent" => "Most Recent", "lowtohigh" => "Price Low To High", "hightolow" => "Price High To Low");
    $data['sortselected'] = get_sortby();

    $data['selectedSortBy'] = $query_string;

    $this->template->set('title', 'Marketplace');
    $this->template->load('default_layout', 'contents', 'index', $data);
}

分页控制器:

private function _doPagination($total_rows = NULL) {
    $per_page = get_perpage();

    $this->load->library('pagination');
    $config['total_rows'] = ($total_rows != NULL) ? $total_rows : 0;
    $config['per_page'] = ($per_page != NULL) ? $per_page : 12;
    $config['num_links'] = 2;
    $config['use_page_numbers'] = TRUE;
    $config['base_url'] = '';
    $config['page_query_string'] = TRUE;
    $config['query_string_segment'] = 'page';
    $config['full_tag_open'] = '<ul class="pagination justify-content-center">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = '«';
    $config['first_tag_open'] = '<li class="page-item">';
    $config['first_tag_close'] = '</li>';
    $config['first_url'] = '?page=1&';
    $config['last_link'] = "»";
    $config['last_tag_open'] = '<li class="page-item">';
    $config['last_tag_close'] = '</li>';
    $config['next_link'] = false;
    $config['next_tag_open'] = '<li class="page-item">';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = false;
    $config['prev_tag_open'] = '<li class="page-item">';
    $config['prev_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li class="page-item">';
    $config['num_tag_close'] = '</li>';
    $this->pagination->initialize($config);
}

型号:

public function get_products($query_string) {

    if ($query_string == 'recent') {
        $sql = "select total_rows=(select count(*) from o2o_pit_live_product)
            ,   * from o2o_pit_live_product order by supplier_on DESC";
    } elseif ($query_string == 'lowtohigh') {
        $sql = "select total_rows=(select count(*) from o2o_pit_live_product)
            ,   * from o2o_pit_live_product order by regular_price ASC";
    } elseif ($query_string == 'hightolow') {
        $sql = "select total_rows=(select count(*) from o2o_pit_live_product)
            ,   * from o2o_pit_live_product order by regular_price DESC";
    } else {
        $sql = "select total_rows=(select count(*) from o2o_pit_live_product)
            ,   * from o2o_pit_live_product";
    }

     $result = $this->db->query($sql)->result_array();
    if ($result) {
        return $result;
    }
    return NULL;
}

观看次数:

<select name="filter" class="custom-select filter" id="filter" onchange="location = this.value;">
<?php foreach ($sorts as $skey => $sname): ?>
    <option value="?<?php echo $skey; ?>" 
    <?php
    if ($selectedSortBy === 'recent' && $skey === 'recent') {
    echo 'selected';
    }
    if ($selectedSortBy === 'lowtohigh' && $skey === 'lowtohigh') {
    echo 'selected';
    }
    if ($selectedSortBy === 'hightolow' && $skey === 'hightolow') {
    echo 'selected';
    }
    ?> >
    <?php echo $sname; ?>
    </option>
<?php endforeach; ?>
</select>

请帮助我。预先感谢。

1 个答案:

答案 0 :(得分:0)

array_slice第二个参数是从记录的起始位置设置的。您设置页码而不是从记录的起始位置。所以您的代码应如下所示

$start = ($page_start-1)*get_perpage();
array_slice($products,$start,get_perpage());