当前位置:首页 > PHP教程 > php分页 > 列表

ajax +php无刷新分页代码

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-09 13:21:13 浏览: 评论:0 

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.   
  21. $obj = new pagination_class($qry,$starting,$recpage);   
  22. $result = $obj->result; 
  23.  
  24.     
  25.    ?><form name="form1" action="testpage.php" method="post"> 
  26.     
  27.    <table border="1" align="center" width="40%"> 
  28.    <tr> 
  29.      <td colspan="2"> 
  30.     search <input type="text" name="search_text" id="search_text" value="<?php echo $searchtext; ?>"> 
  31.      <input type="submit" value="search"> 
  32.      </td>  
  33.    </tr> 
  34.    <tr><td colspan="2"> 
  35.     
  36.    <div id="page_contents"> 
  37.     <table border="1" align="center" width="100%"> 
  38.     <tr><td>sl no</td><td>name</td></tr> 
  39.     <?if(mysql_num_rows($result)!=0){ 
  40.      $counter = $starting + 1; 
  41.      while($data = mysql_fetch_array($result)) {?> 
  42.       <tr> 
  43.       <td><? echo $counter; ?></td> 
  44.       <td><? echo $data['name']; ?></td> 
  45.       </tr><? 
  46.       $counter ++; 
  47.      } ?> 
  48.      
  49.        
  50.      <tr><td colspan="2"><? echo $obj->anchors; ?></td></tr> 
  51.      <tr><td colspan="2"><? echo $obj->total; ?></td></tr> 
  52.     <?}else{?>//开源代码phpfensi.com 
  53.      <tr><td align="center" colspan="2">no data found</td></tr> 
  54.     <?}?> 
  55.     </td></tr> 
  56.     </table> 
  57.    </div> 
  58.    </td></tr> 
  59.   </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.  
  16. var xmlhttp 
  17. function pagination(page) 
  18. { 
  19. xmlhttp=getxmlhttpobject(); 
  20. if (xmlhttp==null) 
  21.   { 
  22.   alert ("your browser does not support ajax!"); 
  23.   return; 
  24.   } 
  25. var url="test_sub.php"; 
  26. url = url+"?starting="+page; 
  27. url = url+"&search_text="+$('search_text').value; 
  28. url=url+"&sid="+math.random(); 
  29. xmlhttp.onreadystatechange=statechanged; 
  30. xmlhttp.open("get",url,true); 
  31. xmlhttp.send(null); 
  32. } 
  33.  
  34. function statechanged()  
  35. {  
  36. if (xmlhttp.readystate==4) 
  37. {  
  38. $("page_contents").innerhtml=xmlhttp.responsetext; 
  39. } 
  40. } 
  41.  
  42. function getxmlhttpobject() 
  43. { 
  44. var xmlhttp=null; 
  45. try 
  46.   { 
  47.   // firefox, opera 8.0+, safari 
  48.   xmlhttp=new xmlhttprequest(); 
  49.   } 
  50. catch (e) 
  51.   { 
  52.   // internet explorer 
  53.   try 
  54.     { 
  55.     xmlhttp=new activexobject("msxml2.xmlhttp"); 
  56.     } 
  57.   catch (e) 
  58.     { 
  59.     xmlhttp=new activexobject("microsoft.xmlhttp"); 
  60.     } 
  61.   } 
  62. return xmlhttp; 
  63. } 

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.  
  22.   if($numrows % $recpage != 0){ 
  23.    $last = ((intval($numrows/$recpage)))*$recpage; 
  24.   }else{ 
  25.    $last = ((intval($numrows/$recpage))-1)*$recpage; 
  26.   } 
  27.   $previous = $starting-$recpage; 
  28.   $anc = "<ul id='pagination-flickr'>"; 
  29.   if($previous < 0){ 
  30.    $anc .= "<li class='previous-off'>first</li>"; 
  31.    $anc .= "<li class='previous-off'>previous</li>"; 
  32.   }else{ 
  33.    $anc .= "<li class='next'><a href='网页特效:pagination(0);'>first </a></li>"; 
  34.    $anc .= "<li class='next'><a href='javascript:pagination($previous);'>previous </a></li>"; 
  35.   } 
  36.    
  37.   ################if you dont want the numbers just comment this block###############  
  38.   $norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors  
  39.   $j = 1; 
  40.   $anch = ""; 
  41.   for($i=$page_showing; $i>1; $i--){ 
  42.    $fpreviouspage = $i-1; 
  43.    $page = ceil($fpreviouspage*$recpage)-$recpage; 
  44.    $anch = "<li><a href='javascript:pagination($page);'>$fpreviouspage </a></li>".$anch; 
  45.    if($j == $norepeat) break; 
  46.    $j++; 
  47.   } 
  48.   $anc .= $anch; 
  49.   $anc .= "<li class='active'>".$page_showing."</li>"; 
  50.   $j = 1; 
  51.   for($i=$page_showing; $i<$total_page; $i++){ 
  52.    $fnextpage = $i+1; 
  53.    $page = ceil($fnextpage*$recpage)-$recpage; 
  54.    $anc .= "<li><a href='javascript:pagination($page);'>$fnextpage</a></li>"; 
  55.    if($j==$norepeat) break; 
  56.    $j++; 
  57.   } 
  58.   ############################################################ 
  59.   if($next >= $numrows){ 
  60.    $anc .= "<li class='previous-off'>next</li>"; 
  61.    $anc .= "<li class='previous-off'>last</li>"; 
  62.   }else{ 
  63.    $anc .= "<li class='next'><a href='javascript:pagination($next);'>next </a></li>"; 
  64.    $anc .= "<li class='next'><a href='javascript:pagination($last);'>last</a></li>"; 
  65.   } 
  66.    $anc .= "</ul>"; 
  67.   $this->anchors = $anc; 
  68.    
  69.   $this->total = "page : $page_showing <i> of  </i> $total_page . total records found: $numrows"; 
  70.  } 
  71. } 
  72. ?> 

数据库代码如下:

  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.  
  10. set sql_mode="no_auto_value_on_zero"; 
  11.  
  12. -- 
  13. -- 数据库: `mydemo` 
  14. -- 
  15.  
  16. -- -------------------------------------------------------- 
  17.  
  18. -- 
  19. -- 表的结构 `students` 
  20. -- 
  21.  
  22. create table if not exists `students` ( 
  23.   `id` int(11) not null auto_increment, 
  24.   `name` varchar(50) not null default '', 
  25.   primary key (`id`) 
  26. ) engine=myisam  default charset=utf8 auto_increment=21 ; 
  27.  
  28. -- 
  29. -- 转存表中的数据 `students` 
  30. -- 
  31.  
  32. insert into `students` (`id`, `name`) values 
  33. (1, '小明'), 
  34. (2, 'aniesh'), 
  35. (3, 'babu'), 
  36. (4, '小黄'), 
  37. (5, 'praveesh'), 
  38. (6, 'dixon'), 
  39. (7, 'sanju'), 
  40. (8, 'neeraj'), 
  41. (9, 'siju'), 
  42. (10, 'noble'), 
  43. (11, 'bibin'), 
  44. (12, 'febin'), 
  45. (13, 'binu'), 
  46. (14, 'charles'), 
  47. (15, 'jaggu'), 
  48. (16, 'mani'), 
  49. (17, 'milu'), 
  50. (18, 'aravind'), 
  51. (19, 'jay'), 
  52. (20, 'hari');

Tags: php无刷新分页 ajax无刷新分页

分享到: