ci_phper_mj 发表于 2012-2-14 22:23:36

如何分页

小弟是CI菜鸟,最近刚做到分页,遇到了一个纠结的问题,特上来求指教。首先,分页是要用到GET传参,既要用到GET传参那就是要用这样的格式,比如:index.php?m=方法&...&per_page=2,既然要用到这样的格式,那就必须在index.php那里面进行跳转,但是这样一来, CI框架默认的控制器/方法/参数这样的格式就没意义了,直接在index.php里面直接进行跳转就可以了,求办法解决或者求代码(分页),$this->uri->segment()这样的就不要了,很容易出问题的。

ci_phper_mj 发表于 2012-2-14 22:49:14

补充说明,用index.php?m=方法&...&per_page=2这样的格式,URL辅助函数完全不能使用。

gxcnvip 发表于 2012-2-14 23:04:06

Controllor:

function index($offset = ''){
      // 加载分页类
      $limit = 20;
          $config['base_url'] = site_url('admin_user/index');
      $config['total_rows'] = $this->My_a_model->count_table();
      $config['per_page'] = $limit;
      $config['first_link'] = '首页';
      $config['prev_link'] = '上一页';
      $config['next_link'] = '下一页';
      $config['last_link'] = '尾页';
      $config['num_links'] = 2;
      $data['limit'] = $limit;
      $data['offset'] = $offset+1;
      $this->pagination->initialize($config);
      $total = $this->My_a_model->count_table();//总页数
      $current = $offset+1;//当前页
      $pagination = $this->pagination->create_links();
      $data['page'] = '总记录'.$total.'条记录  当前页'.$current.'/'.$total.'  '. $pagination;
      
      //$data['query'] = $this->db->get('admin_user');
      $data['listQuery'] = $this->My_a_model->show_list($limit, $offset);
                $data['title'] = '系统用户列表';
               
                $this->load->view('admin/admin_user', $data);
                exit();
        }



model:
//查询表记录,分页类
    function count_table($table = 'admin_user'){
      return $this->db->count_all($table);
    }
   
    function show_list($limit, $offset, $table = 'admin_user'){
      if(!$limit){
            $this->db->order_by('id', 'DESC');
            return $this->db->get($table, $order);
      }else{
            $this->db->order_by('id', 'DESC');
            $this->db->limit($limit,$offset);
            return $this->db->get($table, $limit, $offset);
            //OR
            //return $this->db->get($table);
      }
    }

View

<?php echo $page;?>


zzly 发表于 2012-2-14 23:42:17

我也是初学者,也卡在这儿了。顶一下。

ci_phper_mj 发表于 2012-2-15 21:28:09

gxcnvip 发表于 2012-2-14 23:04 static/image/common/back.gif
Controllor:

function index($offset = ''){


这样不好,$offset是无法控制的,用户可以自己修改的。

ci_phper_mj 发表于 2012-2-15 22:40:50

zzly 发表于 2012-2-14 23:42 static/image/common/back.gif
我也是初学者,也卡在这儿了。顶一下。

想出来了,我错了。
index.php?c=控制器&m=方法这个要起作用
只要在config配置文件里面的$config['enable_query_strings']设置为TRUE,这样我们的分页就OK了。

Hex 发表于 2012-2-17 12:39:20

GET传参和URI分段可以结合使用。
分页类也支持这种结合使用。
2.0.0以后的CI支持支持,不需要设置。
页: [1]
查看完整版本: 如何分页