php中分页显示文章标题

php中文章标题很多的情况下怎样才能分页显示呢?下面根据本人的体会把主要代码公开,希望对初学网友有所帮助.

  1. $page_size=10;(每页显示数量)
  2. $sql='select count(*) as rnt from mydbf';
  3. $sql .= ' order by mytime desc ';
  4. $qh=mysql_query($sql);
  5. $data=mysql_fetch_array($qh);
  6. $nr=$data["cnt"];
  7. $pages=intval($nr/$page_size);
  8. if($nr%$page_size) $pages ;
  9. if(emptyempty($page))
  10. {
  11. $page=1;
  12. $offset=0;
  13. }
  14. else
  15. {
  16. $offset=$page*$page_size-$page_size;
  17. }
  18. ?>
  19. <!--
  20. $total = mysql_numrows($qh);
  21. $total=$nr;
  22. echo "<td colspan=3>";
  23. //显示所有文章
  24. if (!isset($page)){
  25. $page = 0;
  26. }
  27. //分页开始
  28. $start= $page*$page_size;
  29. $offset=$start;
  30. $totalpage =ceil($total/$page_size);
  31. $pagestr ='[<font color=green>总数</font>]:'.$total;
  32. $pagestr.= "&nbsp".($page 1).'/'.$totalpage.'页'."&nbsp";
  33. $xjz1= "<a href=$php_self?";
  34. $sql = 'select mytxt,content_id,clicknum,from mydbf ';
  35. $sql .= ' order by ltime desc limit '.$offset.','.$pageno;
  36. $res = mysql_query($ls_sql, $dbh); 
  37. $countloop = 0;
  38. while ($row = mysql_fetch_array($res)) { 
  39. ....
  40. }
  41. $xjz1= "<a href=$php_self?";
  42. $xjz2=substr($xjz1,8);
  43. ?>
  44. <table width="100%" cellpadding="0" cellspacing="0" height="0">
  45. <form action="<?echo $php_self.$xjz2."&page=$page";?>" method="post">
  46. <td width="100%" height="9"> 
  47. <table width="100%" cellspacing="0" cellpadding="0">
  48. <tr><td width="72%" align="center" nowrap>&nbsp;<b>分页</b>:<?if($page==1){?>首页
  49. 上一页 <?
  50.    }
  51.    else
  52.    {
  53.    $p=$page-1;
  54.    echo "$xjz1&page=1>首页</a> $xjz1&page=$p>上一页</a>";
  55.    }
  56.    if($page>=$pages){?> 下一页 尾页 <?
  57.    }
  58.    else
  59.    {
  60.     $pp=$page 1;
  61.     echo "&nbsp;$xjz1&page=$pp>下一页</a>&nbsp;$xjz1&page=$pages>尾页</a>";
  62.    }
  63. ?>&nbsp;页次:<b><?echo $page?></b>/<b><?echo $pages?></b>页 <b><?echo $page_size?></b>个/页&nbsp;共<b><?echo $nr?></b>篇文章 转到: 
  64. <!--<input type="text" name="page" size="4"  class="p1" >
  65. <input type="submit" value="Go" name="B1" class="p1" >
  66. </td><td>-->
  67. <select name=pagex onChange="javascript:location.href=this.options[this.selectedIndex].value">
  68. <?
  69.   for ($i=1;$i<=$pages;$i ){
  70. if($i==$page)$selectxjz="selected";
  71. else $selectxjz='';
  72. echo "<option value="".$xjz2."&page=".$i."" ".$selectxjz.">".$i."</option>";
  73. }
  74. ?>
  75. </select>&nbsp;页
  76. </td></table></td></form></table>
  77. <?}
  78. ?>