<?phpclass PageBar { /**+-----------------------------------------------
| 总记录数    
|  +-----------------------------------------------
*/
var $total;

/**+-----------------------------------------------
| 每页记录数    
|  +-----------------------------------------------
*/
var $onepage; /**+-----------------------------------------------
| 数字条显示个数    
|  +-----------------------------------------------
*/
var $num;

/**+-----------------------------------------------
| 当前页数    
|  +-----------------------------------------------
*/
var $pagecount; /**+-----------------------------------------------
| 总页数    
|  +-----------------------------------------------
*/
var $total_page; /**+-----------------------------------------------
| MYSQL查询指针    
|  +-----------------------------------------------
*/
var $offset; /**+-----------------------------------------------
| 链接的前部分    
|  +-----------------------------------------------
*/
var $linkhead;
     /**+-----------------------------------------------
| $form_vars为当前页的表单变量,用"|"隔开。
| i.e. $pb = new PageBar(50, 10, "action|username")
|  +-----------------------------------------------
*/
function PageBar($total, $onepage, $form_vars='') {
$pagecount = $_GET['page'];
$this->total      = $total;
$this->onepage    = $onepage;
$this->total_page = ceil($total/$onepage); if (empty($pagecount))  {
$this->pagecount = 1;
$this->offset  = 0;
}else {
$this->pagecount = $pagecount;
$this->offset    = ($pagecount-1)*$onepage;
}

if (!empty($form_vars)) {

$vars = explode("|", $form_vars);
$chk  = $vars[0];
$chk2 = $vars[1];
$chk_value  = $_POST["$chk"];
$chk_value2 = $_POST["$chk2"];
if (empty($chk_value) && empty($chk_value2)) {
$formlink = "";
}else {
for ($i=0; $i<sizeof($vars); $i++) {
$var     = $vars[$i];
$value   = $_POST["$var"];
$addchar = $var."=".$value;

$formlink = $formlink.$addchar."&";
}
}
}else {
$formlink = "";
} $linkarr = explode("page=", $_SERVER['QUERY_STRING']);
$linkft  = $linkarr[0]; if (empty($linkft)) {
$this->linkhead = $_SERVER['PHP_SELF']."?".$formlink;
}else {
$linkft    = (substr($linkft, -1)=="&")?$linkft:$linkft."&";
$this->linkhead = $_SERVER['PHP_SELF']."?".$linkft.$formlink;
} }
#End of function PageBar();

/**+-----------------------------------------------
| 用于取得select的指针.
| i.e. $pb     = new PageBar(50, 10);
|  $offset = $pb->offset();
|  +-----------------------------------------------
*/
function offset() {
return $this->offset;
}
#End of function offset();
/**+-----------------------------------------------
| 取得第一页.$link为1是为带链接
| i.e. $pb         = new PageBar(50, 10);
|  $first_page = $pb->first_page(1);
|  +-----------------------------------------------
*/
function first_page($link='') {
$linkhead  = $this->linkhead;
if ($link==1) {
return "<a href=\"$linkhead"."page=1\" title=\"首页\">首页</a> ";
}else {
return 1;
}
}
#End of function first_page(); /**+-----------------------------------------------
| 取得最末页.$link为1是为带链接
| i.e. $pb         = new PageBar(50, 10);
|  $total_page = $pb->total_page(1);
|  +-----------------------------------------------
*/
function total_page($link='') {
$linkhead  = $this->linkhead;
$total_page = $this->total_page;
if ($link==1) {
return " <a href=\"$linkhead"."page=$total_page\" title=\"尾页\">尾页</a>";
}else {
return $total_page;
}
}
#End of function total_page(); /**+-----------------------------------------------
| 取得上一页.$char为链接的字符,默认为"[<]"
| i.e. $pb       = new PageBar(50, 10);
|  $pre_page = $pb->pre_page("上一页");
|  +-----------------------------------------------
*/
function pre_page($char='') {
$linkhead  = $this->linkhead;
$pagecount = $this->pagecount;
if (empty($char)) {
$char = "[<]";
} if ($pagecount>1) {
$pre_page = $pagecount - 1;
return "<a href=\"$linkhead"."page=$pre_page\" title=\"previous page\">$char</a>";
} else {
return '';
}
}
#End of function pre_page(); /**+-----------------------------------------------
| 取得下一页.$char为链接的字符,默认为"[>]"
| i.e. $pb        = new PageBar(50, 10);
|  $next_page = $pb->next_page("下一页");
|  +-----------------------------------------------
*/
function next_page($char='') {
$linkhead   = $this->linkhead;
$total_page = $this->total_page;
$pagecount  = $this->pagecount;
if (empty($char)) {
$char = "[>]";
}
if ($pagecount<$total_page) {
$next_page = $pagecount + 1;
return "<a href=\"$linkhead"."page=$next_page\" title=\"next page\">$char</a>";
} else {
return '';
}
}
#End of function next_page();

解决方案 »

  1.   

    Select id From topic;
    然后计算一下id数,根据页面显示多少帖子来分割,除一下,给url一个page值
    然后取得page,比如=3,一页20个帖子:
    Select * from topic limit 20*(3-1),20;
      

  2.   


    /**+-----------------------------------------------
    | 取得页码数字条.  $num 为个数,默认为10
    |                    $color 为当前链接的突显颜色
    |  $left 数字左边 默认为"[" 
    |                    $right 数字左右 默认为"]"
    | i.e. $pb      = new PageBar(50, 10);
    |  $num_bar = $pb->num_bar(9, "$cccccc");
    |  +-----------------------------------------------
    */
    function num_bar($num='', $color='', $left='', $right='') {
    $num       = (empty($num))?10:$num;
    $this->num = $num;
    $mid       = floor($num/2);
    $last      = $num - 1; 
    $pagecount = $this->pagecount;
    $totalpage = $this->total_page;
    $linkhead  = $this->linkhead;
    $left      = (empty($left))?"[":$left;
    $right     = (empty($right))?"]":$right;
    $color     = (empty($color))?"#ff0000":$color;
    $minpage   = (($pagecount-$mid)<1)?1:($pagecount-$mid);
    $maxpage   = $minpage + $last;
    if ($maxpage>$totalpage) {
    $maxpage = $totalpage;
    $minpage = $maxpage - $last;
    $minpage = ($minpage<1)?1:$minpage;
    } for ($i=$minpage; $i<=$maxpage; $i++) {
    $char = $left.$i.$right;
    if ($i==$pagecount) {
    $char = "<font color='$color'>$char</font>";
    } $linkchar = "<a href='$linkhead"."page=$i'>".$char."</a>";
    $linkbar  = $linkbar.$linkchar;
    } return $linkbar;
    }
    #End of function num_bar();
    /**+-----------------------------------------------
    | 取得上一组数字条.$char为链接的字符,默认为"[<<]"
    | i.e. $pb        = new PageBar(50, 10);
    |        $num_bar   = $pb->num_bar();
    |  $pre_group = $pb->pre_group();
    |  +-----------------------------------------------
    */
    function pre_group($char='') {
    $pagecount   = $this->pagecount;
    $linkhead    = $this->linkhead;
    $num      = $this->num;
    $mid         = floor($num/2);
    $minpage     = (($pagecount-$mid)<1)?1:($pagecount-$mid);
    $char        = (empty($char))?"[<<]":$char;
    $pgpagecount = ($minpage>$num)?$minpage-$mid:1;
    return "<a href='$linkhead"."page=$pgpagecount' title=\"上一组\">".$char."</a>";
    }
    #End of function pre_group();

    /**+-----------------------------------------------
    | 取得下一组数字条.$char为链接的字符,默认为"[>>]"
    | i.e. $pb         = new PageBar(50, 10);
    |        $num_bar    = $pb->num_bar();
    |  $next_group = $pb->next_group();
    |  +-----------------------------------------------
    */
    function next_group($char='') {
    $pagecount = $this->pagecount;
    $linkhead  = $this->linkhead;
    $totalpage = $this->total_page;
    $num    = $this->num;
    $mid       = floor($num/2);
    $last      = $num; 
    $minpage   = (($pagecount-$mid)<1)?1:($pagecount-$mid);
    $maxpage   = $minpage + $last;
    if ($maxpage>$totalpage) {
    $maxpage = $totalpage;
    $minpage = $maxpage - $last;
    $minpage = ($minpage<1)?1:$minpage;
    } $char = (empty($char))?"[>>]":$char;
    $ngpagecount = ($totalpage>$maxpage+$last)?$maxpage + $mid:$totalpage; return "<a href='$linkhead"."page=$ngpagecount' title=\"下一组\">".$char."</a>";
    }
    #End of function next_group(); /**+-----------------------------------------------
    | 取得整个数字条,上一页,下一页,上一组
    |   下一组的等.$num数字个数,$color 当前链接的突显色
    | i.e. $pb               = new PageBar(50, 10);
    |        $whole_num_bar    = $pb->whole_num_bar(9);
    |  +-----------------------------------------------
    */
        function whole_num_bar($num='', $color='') {
        $num_bar = $this->num_bar($num, $color);
    return  $this->first_page(1).$this->pre_group().$this->pre_page().$num_bar.$this->next_page().$this->next_group().$this->total_page(1);
        }
    #End of function whole_bar();
    /**+-----------------------------------------------
    | 取得整链接,等于whole_num_bar加上表单跳转.
    |   $num数字个数,$color 当前链接的突显色
    | i.e. $pb           = new PageBar(50, 10);
    |        $whole_bar    = $pb->whole_bar(9);
    |  +-----------------------------------------------
    */
    function whole_bar($jump='', $num='', $color='') {
    $whole_num_bar = $this->whole_num_bar($num, $color)."&nbsp;";
    $jump_form     = $this->jump_form($jump);
    return <<<EOT
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr> 
        <td align="right">$whole_num_bar</td>
        <td width="50" align="right">$jump_form</td>
      </tr>
    </table>
    EOT;
    } /**+-----------------------------------------------
    | 跳转表单
    |   i.e. $pb           = new PageBar(50, 10);
    |        $Jump_form    = $pb->Jump_form();
    |  +-----------------------------------------------
    */
    function Jump_form($jump='') {
    $formname = "pagebarjumpform".$jump;
    $jumpname = "jump".$jump; 
    $linkhead = $this->linkhead;
    $total   = $this->total_page;
    $form = <<<EOT
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <script language="javascript">
    function $jumpname(linkhead, total, page){

    var pagecount = (page.value>total)?total:page.value;
    pagecount = (pagecount<1)?1:pagecount;
    location.href = linkhead + "page=" + pagecount;
    return false;
    }
    </script>
           <form name="$formname" method="post" onSubmit="return $jumpname('$linkhead', $total, $formname.page)"><tr>
              <td>
            <input name="page" type="text" size="1">
    <input type="button" name="Submit" value="GO" onClick="return $jumpname('$linkhead', $total, $formname.page)">
          </td>
            </tr></form></table>
    EOT; return $form;
    }
    #End of function Jump_form();}
    #End of class PageBar;
    /*****
    //example$total = 1000;
    $onepage = 20;$pb       = new PageBar($total, $onepage);
    $offset   = "offset=".$pb->offset();
    $pagebar1  = $pb->whole_bar();
    $pagebar2  = $pb->whole_bar(2);
    echo $offset."<br>".$pagebar1."<br>";
    echo $offset."<br>".$pagebar1."<br>";/** return:
       offset=0
    [1][<<][1][2][3][4][5][6][7][8][9][10][>][>>][50]        
    *****/
    ?>