当前位置:Gxlcms > PHP教程 > 分页实现

分页实现

时间:2021-07-01 10:21:17 帮助过:12人阅读

<无详细内容>
  1. header('Content-type:text/html; charset=UTF-8');
  2. $conn = mysql_connect('localhost', 'root', '123456') or die('connect to database error!');
  3. mysql_select_db('xsphp') or die('select database error!');
  4. mysql_query('SET NAMES utf8');
  5. function showTable($name,$page=1)
  6. {
  7. $eachPages = 5;
  8. $url = $_SERVER['PHP_SELF'];
  9. $sql = "SELECT count(*) as total FROM {$name}";
  10. $result = mysql_query($sql);
  11. list($rowNums) = mysql_fetch_row($result);
  12. $pageNums = ceil($rowNums / $eachPages);
  13. $rowFrom = ($page > 1) ? (($page - 1) * $eachPages) : 0;
  14. $sql = "SELECT id,title FROM {$name} LIMIT {$rowFrom},{$eachPages}";
  15. $result = mysql_query($sql);
  16. echo '';
  17. echo '
  18. ';
  19. //显示标题
  20. $cols = mysql_num_fields($result);
  21. echo '
  22. ';
  23. for ($i=0; $i<$cols; $i++) {
  24. echo '
  25. ';
  26. }
  27. echo '
  28. ';
  29. //显示记录
  30. while ($row = mysql_fetch_assoc($result)) {
  31. echo '
  32. ';
  33. echo '
  34. ';
  35. echo '
  36. ';
  37. echo '
  38. ';
  39. }
  40. //显示Page导航
  41. echo '
  42. '.$name.'

    '.mysql_field_name($result,$i).'
    '.$row['id'].''.$row['title'].'
    记录共有:'.mysql_num_rows($result) . '';
  43. echo ($page == 1) ? '首页' : '首页';
  44. echo ' ';
  45. echo ($page == 1) ? '上一页' : '上一页';
  46. echo ' ';
  47. echo ($page == $pageNums) ? '下一页' : '下一页';
  48. echo ' ';
  49. echo ($page == $pageNums) ? '尾页' : '尾页';
  50. echo '
  51. ';
  52. }
  53. if (isset($_GET['page']) && $_GET['page'] > 0) {
  54. $page = $_GET['page'];
  55. } else {
  56. $page = 1;
  57. }
  58. showTable('category',$page);

人气教程排行