0

我有一个图片库;我想做的是一个简单的分页来显示图像;在某种程度上,我在每一页中只显示 18 张图片!所以我在我的控制器中尝试了以下代码:

    $this->load->model('Gallery_model');
    $data['images'] = $this->Gallery_model->get_images(18,  $this->uri->segment(3));
    $this->load->library('pagination');
    $config['base_url'] = 'http://localhost/Gallery/index.php/gallery/index/';                
    $config['total_rows'] = count($this->Gallery_model->get_all_images());
    $config['per_page'] = 18;
    $this->pagination->initialize($config);
    $data['main_content'] = "gallery";
    $this->load->view('includes/template', $data);

在我的 Gallery_model 中,我以每次基于 uri->segment 显示 18 张图像的方式进行处理。如果您需要我的模型方法:

public function get_images($per_page, $segment) {

    $files = scandir($this->gallery_path . "\output");
    $newFiles = array_diff($files, array('.', '..', 'thumbs'));

    $images = array();

    foreach ($newFiles as $file) {

        $images[] = array(
            'url' => $this->gallery_path_url . 'output/' . $file,
            'thumb_url' => $this->gallery_path_url . 'output/' . $file,
        );
    }

    $newImage = array();
    if ((($segment * $per_page)+$per_page) < count($images)) {

        for ($i = 0; $i < $per_page; $i++) {

            $newImage[$i] = $images[($segment * $per_page) + $i];
        }
    }else
    {
        for ($i = 0; $i < count($images)-($segment * $per_page); $i++) {

            $newImage[$i] = $images[($segment * $per_page) + $i];
        }
    }
    return $newImage;
}

    public function get_all_images() {
    $files = scandir($this->gallery_path . "\output");
    $newFiles = array_diff($files, array('.', '..', 'thumbs'));
    $images = array();
    foreach ($newFiles as $file) {

        $images[] = array(
            'url' => $this->gallery_path_url . 'output/' . $file,
            'thumb_url' => $this->gallery_path_url . 'output/' . $file,
        );
    }


    return $images;

}

现在一切都很好,我有 25 张图片(超过 18 张图片),我应该有两页,但我确实有 2 页,但问题是当我点击第二页时,网址转到...。 /index/18 而不是 ..../index/1

可能是什么问题?

我可能不清楚,所以如果您需要更多说明,请告诉我。

谢谢

4

2 回答 2

2

分页没问题,会到 /index/18 ,用这个uri-

segment(3) 作为查询中的偏移量以获取下一组结果

于 2013-10-23T18:22:07.673 回答
1

我的理解是,您希望下一页索引/2 然后索引/3 等等,而不是索引/18。默认情况下,Codeigniter 会在 url 中添加下一页的开始位置,这意味着页面将从数据库中的记录 18 开始。

我想你想要的是

$config['use_page_numbers'] = 真; 默认情况下,URI 段将使用您正在分页的项目的起始索引。如果您希望显示实际页码,请将其设置为 TRUE。

http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

于 2013-10-23T18:15:19.457 回答