不罗嗦了,直接上范例

function.paginate_first.php、function.paginate_last.php、function.paginate_prev.php、function.paginate_next.php、function.paginate_middle.php五个文件放在Smarty的plugins目录下,SmartyPaginate.class.php我放在了Smarty的根目录下,你们可以参考。

PHP程序

require_once ‘includes/Smarty/SmartyPaginate.class.php’;  //包含分页类的类文件

SmartyPaginate::connect(); 连接到分类页
SmartyPaginate::setLimit(10); //一页取多少条记录
 
$smarty->assign(‘results’,get_od_results()); //将要分页的数据进行分割并指定Smarty变量
SmartyPaginate::assign($smarty); // 赋值给Smarty

$smarty->display(‘scorderform.html’); //smarty模板

函数:

        $_query = sprintf("SELECT SQL_CALC_FOUND_ROWS * FROM table ORDER BY id DESC LIMIT %d,%d",            SmartyPaginate::getCurrentIndex(), SmartyPaginate::getLimit());        $_result = $GLOBALS['db']->query($_query);        while ($_row = $GLOBALS['db']->fetch_array($_result, MYSQL_ASSOC)) {            // collect each record into $_data            $_data[] = $_row;        }                // now we get the total number of records from the table        $_query1 = "SELECT FOUND_ROWS() as total";        $_result1 = $GLOBALS['db']->query($_query1);        $_row = $GLOBALS['db']->fetch_array($_result1, MYSQL_ASSOC);        SmartyPaginate::setTotal($_row['total']);                return $_data;

说明:$GLOBALS[‘db’]是我自己事先用的数据库类,大家可以举一反三,使用自己的数据库类

模板相关写法

 {paginate_first text="第一页"}{paginate_prev text="上一页"} 
{paginate_middle format="page" page_limit="5" link_prefix="" link_suffix="" prefix="" suffix="" class="number current"} 
{paginate_next text="下一页"} {paginate_last text="末页"}

{section name=res loop=$results}

这里循环你要分页的数据

{/section}

在function.paginate_middle.php中,大约130行左右,将代码修改成以下

if($_item != $_curr_item) {     $_this_url = $_url;    $_this_url .= (strpos($_url, '?') === false) ? '?' : '&';    $_this_url .= SmartyPaginate::getUrlVar($_id) . '=' . $_item;    $_ret .= $_link_prefix . '<a class="number" href="' . str_replace('&', '&amp;', $_this_url) . '"' . $_attrs . '>' . $_text . '</a>' . $_link_suffix;} else {    $_ret .= $_link_prefix . '<a class="number current" href="' . str_replace('&', '&amp;', $_this_url) . '"' . $_attrs . '>' . $_text . '</a>' . $_link_suffix;}这样就是在当前页和非当前页采用不同的样式

作者 龙飞