1、函数: function showpage($page,$pagelistnum,$total,$style=1,$url='') { global $pagelist,$showpage,$_SERVER; $page==''?$page=1:'';//如果page为空则为1,否则为空 $pagelist = ($page-1)*$pagelistnum; $total % $pagelistnum==0?$totalpage = $total / $pagelistnumtotalpage = floor($total / $pagelistnum)+1; $page>1?$uppage = $page-1uppage = 1; $page>=$totalpage?$nextpage = $totalpagenextpage = $page+1; $tabletr="<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td height=20>"; $tabletd="</td></tr></table>"; $nav=" "; $uppage=$page-1; //上一页 $dmpage=$page+1; //下一页 //如果$url使用默认,即空值,则赋值为本页URL: if(!$url){ $url=$_SERVER["REQUEST_URI"];} //URL分析: $thispage =(stristr($url,'?page')!=false)?'?page':'&page'; $url=str_replace($thispage."=".$page,'',$url); //Url里有"?"就加"&"没有就加"?" if(stristr($url,'?')!=false){ $url.="&page"; }else { $url.="?page"; } switch($style) { case '1': //9首页 7前页 后页8 尾页: $nav .=" 总计<B>$total</B>条记录. 当前第<B>$page</B>页,共<strong>$totalpage</strong>页. "; if ($page > 1) { $nav .= '<a href="'.$url.'=1"><font face=webdings>9</font>首页</a> '; $nav .= '<a href="'.$url.'='.$uppage .'"><font face=webdings>7</font>前页</a> '; }else { $nav .= '<span><font face=webdings>9</font>首页</span> '; $nav .= '<span><font face=webdings>7</font>前页</span> '; } if ($page < $totalpage) { $nav .= '<a href="'.$url.'='.$dmpage.'">后页<font face=webdings>8</font></a> '; $nav .= '<a href="'.$url.'='.$totalpage.'">尾页<font face=webdings>:</font></a>'; }else { $nav .= '<span>后页<font face=webdings>8</font></span> '; $nav .= '<span>尾页<font face=webdings>:</font></span>'; } //下拉跳转列表,循环列出所有页码: $nav .=" 转到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n"; for($i=1;$i<=$totalpage;$i++){ if($i==$page) $nav.="<option value='$i' selected>$i</option>\n"; else $nav.="<option value='$i'>$i</option>\n"; } $nav.="</select> 页"; break; case '2'://分页 $nav .="<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"> <tr> <td height=\"35\" width=150>Total<B>$total</B>.Now<B>$page/$totalpage</b>age</td> <td> </td>"; if ($page > 1) { $nav .= '<td width=\"20\"><a href="'.$url.'='.$uppage .'">First</a></td> '; }else { $nav .= '<td width=\"20\"></td>'; } $mid = ceil(($pagelistnum+1)/2); if($page<=$mid ) { $begin = 1; }else if($page > $totalpage-$mid) { $begin = $totalpage-$pagelistnum+1; }else { $begin = $page-$mid+1; } if($begin<0) $begin = 1; $end = ($begin+$pagelistnum>$totalpage)?$totalpage+1begin+$pagelistnum; $nav .="<td width=\"50\" align=\"left\">"; for($i=$begin; $i<$end; $i++) { $nav .=($page!=$i)?" <a href='$url=$i' title='第{$i}页'>$i</a> ":" <b>$i</b>"; } $nav .="</td>"; if ($page < $totalpage) { $nav .= '<td width=\"50\"><a href="'.$url.'='.$dmpage.'" class=\"page_linked\">Last</a></td>'; }else { $nav .= ''; } $nav .='</tr></table>'; break; } $showpage=$tabletr.$nav.$tabletd; return $showpage; } 2、下来我们看如何调用 <?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); include('common.php'); $page = !empty($_REQUEST['page']) ? intval($_REQUEST['page']) : 1; /* 获得文章总数 */ $size = 3; $count = get_article_count($cat_id); $pages = ($count > 0) ? ceil($count / $size) : 1; if ($page > $pages) { $page = $pages; } $smarty->assign('pages_show',showpage($page,$size,$count,2,'')); $smarty->assign('articles_list',get_cat_articles($cat_id,$page,$size));//这是ec的一个获取文章的函数 在includes/lib_articles.php里。我们不用管。 $smarty->display("blog.dwt");
新建blog.dwt 按照ECSHOP模板显示方式 循环文章 然后把我们的分页调进来 |