用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5138|回复: 3
收起左侧

[程序 App] 带分类的分页

[复制链接]
发表于 2009-9-27 18:05:33 | 显示全部楼层 |阅读模式
原文:http://bbs.phpall.cn/read.php?tid=198

所有源码在附件中,已测试成功的。

我想表全部内容的分页,大家都已经很熟悉了,我这里写了个带分类的分页。带分类的分页以后大家也会经常遇到。
带分类的分页实际上是在已有的分页做了一些相应的改动。

我新建了2个简单的表,能说明问题就行。
表1
  news表
              

我这里把原始的分页和带分类的分页代码都贴出来,这样大家好对比着来看。
先看controller层的:
  • <?php
  • class Fenye extends Controller
  • {
  • function Fenye(){
  •   parent::Controller();
  •   $this->load->helper('url');
  • }
  • function index(){
  •   $this->load->database();
  •   $this->load->library('pagination');
  • }
  • //apple函数是对整表内容的分页
  • function apple()
  • {
  •   $page_num = '1';//每页的数据
  •   $this->load->model('listmodel');
  •   $data= $this->listmodel->page('news',$page_num,$this->uri->segment(3)); //这里主要传入每页的页数和数据的在数据库中的个数偏移量
  •   $total_nums=$this->db->count_all('news');;//这里得到从数据库中的总页数
  •   $data['query']=$data[0];//把查询结果放到$data['query']中
  •   //print_r($data);exit;
  •   $this->load->library('pagination');
  •   $config['base_url'] = $this->config->item('base_url').'/index.php/fenye/apple/';
  •   $config['total_rows'] = $total_nums;//总共多少条数据
  •   $config['per_page'] = $page_num;//每页显示几条数据
  •   $config['full_tag_open'] = '<p>';
  •   $config['full_tag_close'] = '</p>';
  •   $config['first_link'] = '首页';
  •   $config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
  •   $config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
  •   $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
  •   $config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
  •   $config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
  •   $config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
  •   $config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
  •   $config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
  •   $config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
  •   $config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
  •   $config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
  •   $config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
  •   $config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
  •   $config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
  •   $config['num_tag_close'] = '</li>';
  •   $this->pagination->initialize($config);
  •   $this->load->view('listnews',$data);
  • }
  • //applelove函数是来对表中sortid的分类进行分页的。
  • function applelove(){
  •    $page_num = '1';//每页的数据
  •    $this->load->model('listmodel');
  •   $data= $this->listmodel->page_where($page_num,$this->uri->segment(4),$this->uri->segment(3));/*这里需要注意一点的是$this->uri-segment(4)和$this->uri-segment(3)分别指的是什么,大家需要结合着model层里面的page_where函数来一起看。$this->uri-segment(3)指的是需要传入的分类号,我们在下面的$config[base_url']中已经给出。然后$this->uri-segment(4)指的是什么了,加入分类号的uri就变成这样的了,//http://******/index.php/fenye/apllelove/分类号/页号      (页号实际表示的是偏移量),
  • 这里的页号就相当于没有带分类的$this->uri-segment(3)的值了。 */
  •   $cat= $this->uri->segment(3);
  •   $total_nums=$this->listmodel->nums_where('news','sortid',2);//这里得到从数据库中某个分类的总页数
  •   $data['query']=$data[0];//把查询结果放到$data['query']中
  •   $this->load->library('pagination');
  •   $config['base_url'] = $this->config->item('base_url').'/index.php/fenye/applelove/'.$this->uri->segment(3).'/''; //.$this->uri->segment(3)这个分类id号一般都是一个变量
  •   $config['total_rows'] = $total_nums;//总共多少条数据
  •   $config['per_page'] = $page_num;//每页显示几条数据
  •   $config['full_tag_open'] = '<p>';
  •   $config['full_tag_close'] = '</p>';
  •   $config['first_link'] = '首页';
  •   $config['first_tag_open'] = '<li>';//“第一页”链接的打开标签。
  •   $config['first_tag_close'] = '</li>';//“第一页”链接的关闭标签。
  •   $config['last_link'] = '尾页';//你希望在分页的右边显示“最后一页”链接的名字。
  •   $config['last_tag_open'] = '<li>';//“最后一页”链接的打开标签。
  •   $config['last_tag_close'] = '</li>';//“最后一页”链接的关闭标签。
  •   $config['next_link'] = '下一页';//你希望在分页中显示“下一页”链接的名字。
  •   $config['next_tag_open'] = '<li>';//“下一页”链接的打开标签。
  •   $config['next_tag_close'] = '</li>';//“下一页”链接的关闭标签。
  •   $config['prev_link'] = '上一页';//你希望在分页中显示“上一页”链接的名字。
  •   $config['prev_tag_open'] = '<li>';//“上一页”链接的打开标签。
  •   $config['prev_tag_close'] = '</li>';//“上一页”链接的关闭标签。
  •   $config['cur_tag_open'] = '<li class="current">';//“当前页”链接的打开标签。
  •   $config['cur_tag_close'] = '</li>';//“当前页”链接的关闭标签。
  •   $config['num_tag_open'] = '<li>';//“数字”链接的打开标签。
  •   $config['num_tag_close'] = '</li>';
  •   $config['uri_segment']=4;      /*这里是非常关键的一个配置,因为添加分类的uri的段数多了一个,所以这里参数要变成4,在原始的分页类中是$this->uri-segment(3) */
  •   $this->pagination->initialize($config);
  •   $this->load->view('listnewsort.php',$data);
  • }
  • }



model层的代码如下:

   
  • <?php
  • class Listmodel extends Model{
  • function Listmodel(){
  •   parent::Model();
  •   $this->load->database();
  • }
  • function page($tablename,$per_nums,$start_position)
  • {//传入3个参数,表名字,每页的数据量,其实位置
  •   $this->db->limit($per_nums,$start_position);
  •   $query=$this->db->get($tablename);
  •   $data=$query->result();
  •   //print_r($data);exit;
  •   $data2[]=$data;//这里大家可能看的有点不明白,可以分别将$data和$data2打印出来看看是什么结果。
  •   return $data2;
  • }
  • function page_where($per_nums,$start_position,$value){
  •   $this->db->limit($per_nums,$start_position);
  •   $this->db->where('sortid',$value);
  •   $query=$this->db->get('news');
  •   $data=$query->result();
  •   $data2[]=$data;
  •   return $data2;
  • }
  • function nums_where($tablename,$field,$value){
  •   $sql='select count(*) as nums from '.$tablename.' where '.$field.'='.$value;
  •   //echo $sql;
  •   //exit;
  •   $rs=$this->db->query($sql);
  •   $query=$rs->result_array();
  •   return $query[0]['nums'];
  • }
  • }

视图层的代码我就在这里不给出了,我把它全部打包在附件中,有数据库。
大家测试分类分页的时候:
应该输入这样的uri来测试:http://127.0.0.1/fenye/index.php/fenye/applelove/1/  或者http://127.0.0.1/fenye/index.php/fenye/applelove/2/
始终应该记住的是index.php后面的第三段$this->uri->segment(3)是表示分类号.

我最后总结一下,带分类的分页的需要注意的在什么地方。
大家现在回过头去看controller里面的applelove(),
这一句:
$data= $this->listmodel->page_where($page_num,$this->uri->segment(4),$this->uri->segment(3));
还有这一句:
$total_nums=$this->listmodel->nums_where('news','sortid',$cat);//这里得到从数据库中某个分类的总页数
还有这句:$config['base_url'] = $this->config->item('base_url').'/index.php/fenye/applelove/'.$this->uri->segment(3).'/';
和这句:$config['uri_segment']=4;

fenye.rar

371.35 KB, 下载次数: 227

发表于 2009-9-27 19:33:59 | 显示全部楼层
支持原创
 楼主| 发表于 2009-9-27 19:51:41 | 显示全部楼层
thank you    !!
发表于 2009-10-21 12:48:15 | 显示全部楼层
不错不错

本版积分规则