Vik*_*ors 5 php pagination codeigniter codeigniter-2
我的控制器功能
function test($start_from = 0)
{
$this->load->library('pagination');
$data = array();
$per_page = 3;
$total = $this->activity_model->count_by();
$config['base_url'] = base_url() . 'test';
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$config['num_links'] = 2;
$config['use_page_numbers'] = TRUE;
$data['follow'] = $this->activity_model->get($per_page, $start_from);
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('front_end/test' ,$data);
}
Run Code Online (Sandbox Code Playgroud)
我的路线:
$route['test'] = "user_activity/test";
$route['test/(:any)'] = "user_activity/test/$1";
Run Code Online (Sandbox Code Playgroud)
型号:
function get($limit,$start_from)
{
$sql = "SELECT * FROM user_follow LIMIT $start_from, $limit";
$query = $this->db->query($sql);
return $query->result_array();
}
Run Code Online (Sandbox Code Playgroud)
问题是我有分页1,2,3,4,5 ....并且在每个页面中我显示3个项目.我想在url中显示我的页码1,2,3,4,5
当我点击第二页url show 3当我点击第三页url show 6等等+3时
是否有可能,我花了几个小时在互联网上寻求建议,但我没理解[code] $ config ['use_page_numbers'] = TRUE; [/ code]做我需要的但是在我的情况下它仍然无法正常工作.
也许你可以建议任何图书馆?
0v3*_*4wn 11
我设法做到这一点,而没有修改类.最好的方法是制作分页类的副本,进行更改并使用它.这样,如果更新CI,则不会丢失修改.这是我的解决方案,没有修改类.
首先,我想说只使用配置选项$config['use_page_numbers'] = TRUE也可以完成但不完全.我发现仅使用此选项无法正常工作的内容如下:
如果您尝试手动编辑网址栏页面,则将其视为偏移,而不是像页面一样,如果您尝试使用"从第2页返回到第1页" prev"链接它也将页码视为偏移量.
代码:
$config['base_url'] = base_url('my/url/page');
$config['total_rows'] = count($this->my_model->get_all());
$config['per_page'] = 2;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 4;
//i'm looading the pagination in the constuctor so just init here
$this->pagination->initialize($config);
if($this->uri->segment(4) > 0)
$offset = ($this->uri->segment(4) + 0)*$config['per_page'] - $config['per_page'];
else
$offset = $this->uri->segment(4);
//you should modify the method in the model to accept limit and offset or make another function - your choice
$data['my_data'] = $this->my_model->get_all($config['per_page'], $offset);
Run Code Online (Sandbox Code Playgroud)
这样
页= FALSE(我/ URL)或(我/网址/网页) -基本上,如果第四URI段是假的,
页面= 0(我/ URL /页/ 0),
和
页= 1(我/ URL /页/ 1)
将显示第一页,然后其他链接将正常工作.我也在验证页面,例如 - 如果有人想要输入(my/url/page/2323),这将引发错误,在模型中你应该检查结果是否为假,如果是控制器应该显示错误页面什么的.希望这可以帮助.
在Pagination类(/system/libraries/Pagination.php)中进行以下更改,以便它使用页码而不是偏移量.
旧(第146-153行):
if ($CI->uri->segment($this->uri_segment) != 0)
{
$this->cur_page = $CI->uri->segment($this->uri_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
Run Code Online (Sandbox Code Playgroud)
新:
在if语句中添加'else'选项以确保默认为; page = 1.
if ($CI->uri->segment($this->uri_segment) != 0)
{
$this->cur_page = $CI->uri->segment($this->uri_segment);
// Prep the current page - no funny business!
$this->cur_page = (int) $this->cur_page;
}
else
{
$this->cur_page = 1;
}
Run Code Online (Sandbox Code Playgroud)
旧(第175行):
$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
Run Code Online (Sandbox Code Playgroud)
新:
只需注释掉这一行,以便当前页面服从控制器/ URI.
//$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
Run Code Online (Sandbox Code Playgroud)
旧(第206行):
$i = $uri_page_number - $this->per_page;
Run Code Online (Sandbox Code Playgroud)
新:
上一页应始终将当前页面减1.
$i = $uri_page_number - 1;
Run Code Online (Sandbox Code Playgroud)
旧(第230行):
if ($this->cur_page == $loop)
Run Code Online (Sandbox Code Playgroud)
新:
缺少分页的URI应该被视为第1页.
if ($this->cur_page == $loop || ($this->cur_page == 1 && $this->cur_page == $loop))
Run Code Online (Sandbox Code Playgroud)
老(第238-247行):
if ($n == '' && $this->first_url != '')
{
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
}
Run Code Online (Sandbox Code Playgroud)
新:
页面URL应使用页码而不是偏移量.
if ($n == '' && $this->first_url != '')
{
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
}
else
{
$n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
$output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$loop.'">'.$loop.'</a>'.$this->num_tag_close;
}
Run Code Online (Sandbox Code Playgroud)
旧(第256行):
$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
Run Code Online (Sandbox Code Playgroud)
新:
下一页应始终是当前页面和1的总和.
$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
Run Code Online (Sandbox Code Playgroud)
旧(第262行):
$i = (($num_pages * $this->per_page) - $this->per_page);
Run Code Online (Sandbox Code Playgroud)
新:
最后一页应该是总页数.
$i = $num_pages;
Run Code Online (Sandbox Code Playgroud)
用新行替换所有旧行.确保在更改之前备份文件.
希望这可以帮助 :)
编辑:
您需要更新控制器功能测试,如:
function test($start_from = 0)
{
$this->load->library('pagination');
$data = array();
$per_page = 3;
$total = $this->activity_model->count_by();
$config['base_url'] = base_url() . 'test';
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$config['num_links'] = 2;
$config['use_page_numbers'] = TRUE;
$start = $per_page * ($start_from-1);
$data['follow'] = $this->activity_model->get($per_page, $start);
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('front_end/test' ,$data);
}
Run Code Online (Sandbox Code Playgroud)
这里我添加了一个新的变量$ start $per_page * ($start_from-1).现在将此$ start作为参数传递给model.
这样做是将每页的项目数乘以(当前页码-1).这意味着如果您的每页项目是,10并且您在第二页$start = 10 *(2-1)上给出了10.所以你的结果将从10,20开始
希望这可以帮助 :)
| 归档时间: |
|
| 查看次数: |
13376 次 |
| 最近记录: |