php+mysql分页类
分页方法文件名 pagestyle.php
<?php function page($page,$total,$phpfile,$pagesize=3,$pagelen=3){ $pagecode = ""; $page = intval($page); $total = intval($total); if(!$total) return array(); $pages = ceil($total/$pagesize); if($page<1) $page = 1; if($page>$pages) $page = $pages; $offset = $pagesize*($page-1); $init = 1; $max = $pages; $pagelen = ($pagelen%2)?$pagelen:$pagelen+1; $pageoffset = ($pagelen-1)/2; if($page!=1){ $pagecode.=" <a href=\"{$phpfile}&page=1\"><IMG align=absMiddle src='/kor/img/btn_prev2.gif' width=11 height=11></a> "; $pagecode.=" <a href=\"{$phpfile}&page=".($page-1)."\"><IMG align=absMiddle src='/kor/img/btn_prev.gif' width=11 height=11></a> "; } if($pages>$pagelen){ if($page<=$pageoffset){ $init=1; $max = $pagelen; }else{ if($page+$pageoffset>=$pages+1){ $init = $pages-$pagelen+1; }else{ $init = $page-$pageoffset; $max = $page+$pageoffset; } } } for($i=$init;$i<=$max;$i++){ if($i==$page){ $pagecode.='<STRONG>'.$i.'</STRONG>'; } else { $pagecode.=" <a href=\"{$phpfile}&page={$i}\">[$i]</a> "; } } if($page!=$pages){ $pagecode.=" <a href=\"{$phpfile}&page=".($page+1)."\"><IMG align=absMiddle src='/kor/img/btn_next2.gif' width=11 height=11></a> "; $pagecode.=" <a href=\"{$phpfile}&page={$pages}\"><IMG align=absMiddle src='/kor/img/btn_next.gif' width=11 height=11></a> "; } $pagecode.='</div>'; return array('pagecode'=>$pagecode,'sqllimit'=>' limit '.$offset.','.$pagesize); } /********** $page = $_GET['page']; $total = 101; $phpfile = "1.php"; $pagesize = 10; $code = page($page,$total,$phpfile,$pagesize,$pagelen=6); echo $code['pagecode']; **************/ ?>
调用页面代码member.php
<?php $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM member WHERE 1"; $sql = $sql." ORDER BY `mb_no` DESC"; if (!$page) { $page = 1; } $page_row = ($page-1)*10; $sql = $sql." LIMIT ".$page_row." , 10"; $query=mysql_query($sql); $sql2 = "select found_rows() as no";//获得查询出来的记录总数 $query2=mysql_query($sql2); $row2=mysql_fetch_array($query2); while($row=mysql_fetch_array($query)){ echo "……"; …… …… 等等输出代码,在这里就不做演示了 } $page = $_GET['page'];//获得当前页数 $total = $row2['no'];//记录总条数 $phpfile = "member.php";//链接的文件名 $pagesize = 10;//每页记录数 $pagelen = 7;//显示7个页数 $code = page($page,$total,$phpfile,$pagesize,$pagelen); echo $code['pagecode']; ?>