簡體   English   中英

Codeigniter分頁在頁面上復制結果

[英]Codeigniter pagination duplicating results on pages

我使用分頁時遇到了一些問題。 這是我的模型代碼:

public function count_locations() {
    return $this->db->count_all("locations");
}
public function fetch_locations($limit, $start) {
    $this->db->select('locations.id as location_id, locations.name_ka, locations.description_ka, locations.recomendation, locations.img, locations.nature, locations.culture, locations.resort, regions.region_name_ru, regions.region_type, resort.resort_name_ru, nature.nature_name_ru, nature.nature_name_en, culture.culture_name_ru, culture.culture_lat')
    ->from('locations')
    ->join('regions', 'regions.id = locations.region','left')
    ->join('culture', 'culture.id = locations.culture','left')
    ->join('resort', 'resort.id = locations.resort','left')
    ->join('nature', 'nature.id = locations.nature','left');

    $this->db->order_by('location_id','asc');
    $this->db->limit($limit, $start);
    $query = $this->db->get();
    return $query->result_array();

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

這是我的控制器:

function page(){

    $this->load->view('templates/head');
    $this->load->view('templates/header');

    $config['base_url'] = base_url() . 'location/page';
    $config['total_rows'] = $this->Location_model->count_locations();
    $config['per_page'] = 4;
    $config['uri_segment'] = 3;
    $config['use_page_numbers'] = TRUE;

    $this->pagination->initialize($config);

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data["results"] = $this->Location_model->
    fetch_locations($config["per_page"], $page);

    $this->load->view('location/location',$data);
    $data["links"] = $this->pagination->create_links();
    $this->load->view("location/pagination", $data);

    $this->load->model('Footer');
    $dataf['links'] = $this->Footer->get_links();
    $this->load->view('templates/footer',$dataf);

    $this->load->view('templates/end');
}

並查看文件內容:

$object = array();
foreach($results as $data) {
    echo $data['location_id'] . "<br>";
}
echo $links;

結果它每頁給我4個元素。 沒關系。 但是問題在進入下一頁時出現。 第一頁顯示: 56, 57, 58, 59 第二頁: 58, 59, 60, 61 第三頁: 60, 61, 62, 63 結果必須是:對於第一頁: 56, 57, 58, 5960, 61, 62, 6364, 65, 66, 67

您的$page在這里是錯誤的。 你在這里使用段。

$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

這應該如下

if ($this->input->get('page')) {
    $sgm = (int) trim($this->input->get('page'));
    $page = $config['per_page'] * ($sgm - 1);
} else {
    $page= 0;
}

現在

 $data["results"] = $this->Location_model->fetch_locations($config["per_page"], $page);

我的第一個猜測是,您在頁面上發布的項目序列並不完全一樣。 我假設你已經看過:第一次:56,57,58,59秒:58,59,60,61第三次:59,60,61,62但不是很仔細閱讀它。

這就是原因...您曾經使用過:

$config['use_page_numbers'] = TRUE;

檢查一下它的功能后, https://ellislab.com/codeigniter/user-guide/libraries/pagination.html告訴您,這使uri段成為頁碼,而不是以前的頁面上的第一項偏移。 。

這只是第一次猜測。 我建議學習一些基本的調試方法。 例如var_dumping某些關鍵點上的一些參數可以幫助您更快地找到錯誤,而不是等待stackoverflow上的響應。

提示:在我們的代碼的大致中間找到關鍵點,並檢查是否通過了正確的參數。 根據結果​​,您可以繼續執行我們代碼的另一半。

這里:例如,您可以var_dump模型中的參數,以了解它是DB /模型中的錯誤還是從視圖/控制器獲取錯誤的參數。

除非你學到這一點,否則你將無法享受有趣的編程,並且整天陷入沮喪的時刻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM