Codeigniter,设置分页 [英] Codeigniter, setting up pagination

查看:120
本文介绍了Codeigniter,设置分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个问题,在我的网站上设置分页。

So, i'm having a problem to setting up pagination on my site.

首先,如果我在我的网站的第一页,并按下以进入第2页,1仍保持粗体。虽然我可以看到在网址,我在第2页。

First of all, if i'm at the first page of my site, and pressing to go to page 2, the "1" still stays bold. Though i can see in the url that i am at page 2.

其次,我需要一些帮助,以正确检索数据。如果我在第1页,我想得到所有的记录1-10。如果我在第2页,我想从11-20得到所有的记录。

Secondly, i need some help to retrieve the data correctly. If i am at page 1, i want to get all records 1-10. If i am on page 2, i want to get all records from 11-20. Got it?

我有google'd并尝试找到任何解决方案,但没有成功。

I have google'd and tried to find any solution, without any success.

$this->db->select('*');
$this->db->from('comments');
$this->db->where('comments.url_friendly', $id);
$this->db->join('allt', 'allt.url_friendly = comments.url_friendly', 'left');
$this->db->order_by('comments.date', 'asc');
$data['query'] = $this->db->get();

$data['title'] = 'Kommentarer - '.$data['query']->row()->amne;

$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $data['query']->num_rows();
$config['per_page'] = 10;

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

$data['pagination'] = $this->pagination->create_links();

此外,我使用的代码在post 2,在此主题

Also, i'm using the code in post 2, in this thread.

是的

推荐答案

$qry = "select * from comment c left join allt a on c.url_friendly = a.url_friendly where c.url_friendly = {$id}";

$per_page = 10;
$offset = ($this->uri->segment(3) != '' ? $this->uri->segment(3):0);//this will get the current page(assuming that page number is in 3rd uri segment). if youy on page one,this will be set to zero

$config['base_url'] = base_url(). 'kommentarer/'.$data['query']->row()->url_friendly;
$config['total_rows'] = $this->db->query($qry)->num_rows();
$config['per_page'] = $per_page;
$config['uri_segment'] = 3; //this is dependent to where your page number displays in url

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

$qry .= " limit {$per_page} offset {$offset} ";
$data['result'] = $this->db->query($qry)->result();

$data['pagination'] = $this->pagination->create_links();

希望这将有所帮助。

这篇关于Codeigniter,设置分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆