|
本帖最后由 gosimple 于 2009-6-1 14:54 编辑
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Pagination extends CI_Pagination {
//定义基本变量
var $countsql = '';
var $listsql = '';
var $cur_page = 1;
function MY_Pagination($params = array())
{
if(count($params)>0)
{
$this->initialize($params);
}
}
/**
* 初始化
*
* @param unknown_type $params
*/
function initialize($params = array())
{
if (count($params) > 0)
{
foreach ($params as $key => $val)
{
if (isset($this->$key))
{
$this->$key = $val;
}
}
}
}
function getTotalcount()
{
$CI =& get_instance();
$query = $CI->db->query($this->countsql);
$res = $query->row_array();
return $res['totalcount'];
}
/**
* 获取指定页的记录数组
*
*/
function getDataArray()
{
$sql = $this->listsql.' limit '.($this->cur_page-1)*$this->per_page.','.$this->per_page;
$CI =& get_instance();
$query = $CI->db->query($sql);
if($query->num_rows()>0)
{
return $query->result_array();
}
return '';
}
/**
* 获取分页工具栏
*
* @return unknown
*/
function getPageToolmenu()
{
$this->total_rows = $this->getTotalcount();
return $this->create_links();
}
}
?>
使用如下:
$channelid = $this->uri->segment(3);
$cur_page = $this->uri->segment(4,1);
$selsql = " from content_piclist cp where cp.status=1 and cp.delflag is null and cp.langtype='".$this->session->userdata('langtype')."'";
if($channelid != 0)
$selsql = $selsql." and cp.channelid=".$channelid;
$listsql = "select *".$selsql;
$countsql = "select count(*) as totalcount".$selsql;
$config['listsql'] = $listsql;
$config['cur_page'] = $cur_page;
$config['per_page'] = 8;
$config['countsql'] = $countsql;
$config['base_url'] = base_url().'welcome/showgczp/'.$channelid;
$config['uri_segment'] = 4;
$this->load->library('pagination');
$this->pagination->initialize($config);
$pics = $this->pagination->getDataArray();
$pagetool = $this->pagination->getPageToolmenu();
$dataout['pics'] = $pics;
$dataout['pagetool'] = $pagetool;
$dataout['channelid'] = $channelid;
$dataout['channeldata'] = $this->channeldata;
$this->smartyextended->view('showgczp',$dataout);
这样写我遇到了两个问题,一是不知怎么国际化,另一个是生成的getPageToolmenu()生成的分页工具栏页数不对,如下:
<strong>1</strong> <a href="http://tongds.vicp.net/welcome/showgczp/12/8">2</a> <a href="http://tongds.vicp.net/welcome/showgczp/12/16">3</a> <a href="http://tongds.vicp.net/welcome/showgczp/12/8">></a> <a href="http://tongds.vicp.net/welcome/showgczp/12/56">Last ›</a>
在uri的最后一个段是页数 |
|