ajax +php无刷新分页代码

index.php代码如下:

  1. header("content-type: text/html; charset=utf-8");
  2. error_reporting(e_all^e_notice);
  3. include('pagination_class.php');
  4. mysql_connect('localhost', 'root', '') or die(mysql_error());
  5. mysql_select_db('mydemo');
  6. mysql_query("set names 'utf8'");
  7. ?>
  8. <script language="javascript" src="pagination.js"></script>
  9. <link rel="stylesheet" type="text/css" href="style.css" />
  10. <?
  11. $qry = "select * from students";
  12. $searchtext = "";
  13. if($_request['search_text']!=""){
  14. $searchtext = $_request['search_text'];
  15. $qry .=" where name like '$searchtext%'";
  16. }
  17. //for pagination
  18. $starting=0;
  19. $recpage = 2;//number of records per page
  20. $obj = new pagination_class($qry,$starting,$recpage);
  21. $result = $obj->result;
  22. ?><form name="form1" action="testpage.php" method="post">
  23. <table align="center" width="40%">
  24. <tr>
  25. <td colspan="2">
  26. search <input type="text" name="search_text" value="<?php echo $searchtext; ?>">
  27. <input type="submit" value="search">
  28. </td>
  29. </tr>
  30. <tr><td colspan="2">
  31. <div >
  32. <table align="center" width="100%">
  33. <tr><td>sl no</td><td>name</td></tr>
  34. <?if(mysql_num_rows($result)!=0){
  35. $counter = $starting + 1;
  36. while($data = mysql_fetch_array($result)) {?>
  37. <tr>
  38. <td><? echo $counter; ?></td>
  39. <td><? echo $data['name']; ?></td>
  40. </tr><?
  41. $counter ++;
  42. } ?>
  43. <tr><td colspan="2"><? echo $obj->anchors; ?></td></tr>
  44. <tr><td colspan="2"><? echo $obj->total; ?></td></tr>
  45. <?}else{?>//开源代码phpfensi.com
  46. <tr><td align="center" colspan="2">no data found</td></tr>
  47. <?}?>
  48. </td></tr>
  49. </table>
  50. </div>
  51. </td></tr>
  52. </table></form>
pagination.js文件,代码如下:
  1. function $()
  2. {
  3. var elements = new array();
  4. for (var i = 0; i < arguments.length; i++)
  5. {
  6. var element = arguments[i];
  7. if (typeof element == 'string')
  8. element = document.getelementbyid(element);
  9. if (arguments.length == 1)
  10. return element;
  11. elements.push(element);
  12. }
  13. return elements;
  14. }
  15. var xmlhttp
  16. function pagination(page)
  17. {
  18. xmlhttp=getxmlhttpobject();
  19. if (xmlhttp==null)
  20. {
  21. alert ("your browser does not support ajax!");
  22. return;
  23. }
  24. var url="test_sub.php";
  25. url = url+"?starting="+page;
  26. url = url+"&search_text="+$('search_text').value;
  27. url=url+"&sid="+math.random();
  28. xmlhttp.onreadystatechange=statechanged;
  29. xmlhttp.open("get",url,true);
  30. xmlhttp.send(null);
  31. }
  32. function statechanged()
  33. {
  34. if (xmlhttp.readystate==4)
  35. {
  36. $("page_contents").innerhtml=xmlhttp.responsetext;
  37. }
  38. }
  39. function getxmlhttpobject()
  40. {
  41. var xmlhttp=null;
  42. try
  43. {
  44. // firefox, opera 8.0+, safari
  45. xmlhttp=new xmlhttprequest();
  46. }
  47. catch (e)
  48. {
  49. // internet explorer
  50. try
  51. {
  52. xmlhttp=new activexobject("msxml2.xmlhttp");
  53. }
  54. catch (e)
  55. {
  56. xmlhttp=new activexobject("microsoft.xmlhttp");
  57. }
  58. }
  59. return xmlhttp;
  60. }

pagination_class.php,代码如下:

  1. <?php
  2. /*
  3. you can use it with out any worries...it is free for you..it will display the out put like:
  4. first | previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | next | last
  5. page : 7 of 10 . total records found: 20
  6. */
  7. class pagination_class{
  8. var $result;
  9. var $anchors;
  10. var $total;
  11. function pagination_class($qry,$starting,$recpage)
  12. {
  13. $rst = mysql_query($qry) or die(mysql_error());
  14. $numrows = mysql_num_rows($rst);
  15. $qry .= " limit $starting, $recpage";
  16. $this->result = mysql_query($qry) or die(mysql_error());
  17. $next = $starting+$recpage;
  18. $var = ((intval($numrows/$recpage))-1)*$recpage;
  19. $page_showing = intval($starting/$recpage)+1;
  20. $total_page = ceil($numrows/$recpage);
  21. if($numrows % $recpage != 0){
  22. $last = ((intval($numrows/$recpage)))*$recpage;
  23. }else{
  24. $last = ((intval($numrows/$recpage))-1)*$recpage;
  25. }
  26. $previous = $starting-$recpage;
  27. $anc = "<ul >";
  28. if($previous < 0){
  29. $anc .= "<li class='previous-off'>first</li>";
  30. $anc .= "<li class='previous-off'>previous</li>";
  31. }else{
  32. $anc .= "<li class='next'><a href='网页特效:pagination(0);'>first </a></li>";
  33. $anc .= "<li class='next'><a href='javascript:pagination($previous);'>previous </a></li>";
  34. }
  35. ################if you dont want the numbers just comment this block###############
  36. $norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors
  37. $j = 1;
  38. $anch = "";
  39. for($i=$page_showing; $i>1; $i--){
  40. $fpreviouspage = $i-1;
  41. $page = ceil($fpreviouspage*$recpage)-$recpage;
  42. $anch = "<li><a href='javascript:pagination($page);'>$fpreviouspage </a></li>".$anch;
  43. if($j == $norepeat) break;
  44. $j++;
  45. }
  46. $anc .= $anch;
  47. $anc .= "<li class='active'>".$page_showing."</li>";
  48. $j = 1;
  49. for($i=$page_showing; $i<$total_page; $i++){
  50. $fnextpage = $i+1;
  51. $page = ceil($fnextpage*$recpage)-$recpage;
  52. $anc .= "<li><a href='javascript:pagination($page);'>$fnextpage</a></li>";
  53. if($j==$norepeat) break;
  54. $j++;
  55. }
  56. ############################################################
  57. if($next >= $numrows){
  58. $anc .= "<li class='previous-off'>next</li>";
  59. $anc .= "<li class='previous-off'>last</li>";
  60. }else{
  61. $anc .= "<li class='next'><a href='javascript:pagination($next);'>next </a></li>";
  62. $anc .= "<li class='next'><a href='javascript:pagination($last);'>last</a></li>";
  63. }
  64. $anc .= "</ul>";
  65. $this->anchors = $anc;
  66. $this->total = "page : $page_showing <i> of </i> $total_page . total records found: $numrows";
  67. }
  68. }
  69. ?>

数据库代码如下:

  1. -- phpmyadmin sql dump
  2. -- version 3.2.4
  3. -- http://www.phpmyadmin.net
  4. --
  5. -- 主机: localhost
  6. -- 生成日期: 2010 年 07 月 07 日 09:26
  7. -- 服务器版本: 5.1.41
  8. -- php 版本: 5.3.1
  9. set sql_mode="no_auto_value_on_zero";
  10. --
  11. -- 数据库: `mydemo`
  12. --
  13. -- --------------------------------------------------------
  14. --
  15. -- 表的结构 `students`
  16. --
  17. create table if not exists `students` (
  18. `id` int(11) not null auto_increment,
  19. `name` varchar(50) not null default '',
  20. primary key (`id`)
  21. ) engine=myisam default charset=utf8 auto_increment=21 ;
  22. --
  23. -- 转存表中的数据 `students`
  24. --
  25. insert into `students` (`id`, `name`) values
  26. (1, '小明'),
  27. (2, 'aniesh'),
  28. (3, 'babu'),
  29. (4, '小黄'),
  30. (5, 'praveesh'),
  31. (6, 'dixon'),
  32. (7, 'sanju'),
  33. (8, 'neeraj'),
  34. (9, 'siju'),
  35. (10, 'noble'),
  36. (11, 'bibin'),
  37. (12, 'febin'),
  38. (13, 'binu'),
  39. (14, 'charles'),
  40. (15, 'jaggu'),
  41. (16, 'mani'),
  42. (17, 'milu'),
  43. (18, 'aravind'),
  44. (19, 'jay'),
  45. (20, 'hari');