加入收藏 | 设为首页 | 会员中心 | 我要投稿 呼伦贝尔站长网 (https://www.0470zz.com/)- 科技、建站、边缘计算、物联网、开发!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

ajax +php无刷新分页代码

发布时间:2022-07-22 09:30:05 所属栏目:PHP教程 来源:互联网
导读:index.php代码如下: header(content-type: text/html; charset=utf-8); error_reporting(e_all^e_notice); include(pagination_class.php); mysql_connect(localhost, root, ) or die(mysql_error()); mysql_select_db(mydemo); mysql_query(set names utf8)
  index.php代码如下:
 
  header("content-type: text/html; charset=utf-8");
  error_reporting(e_all^e_notice);
  include('pagination_class.php');
  mysql_connect('localhost', 'root', '') or die(mysql_error());
  mysql_select_db('mydemo');
  mysql_query("set names 'utf8'");
  ?>
  <script language="javascript" src="pagination.js"></script>
  <link rel="stylesheet" type="text/css" href="style.css" />
  <?
  $qry = "select * from students";
  $searchtext = "";
  if($_request['search_text']!=""){
   $searchtext = $_request['search_text'];
   $qry .=" where name like '$searchtext%'";
  }
  //for pagination
  $starting=0;
  $recpage = 2;//number of records per page
    
  $obj = new pagination_class($qry,$starting,$recpage);   
  $result = $obj->result;
   
      
     ?><form name="form1" action="testpage.php" method="post">
      
     <table border="1" align="center" width="40%">
     <tr>
       <td colspan="2">
      search <input type="text" name="search_text" id="search_text" value="<?php echo $searchtext; ?>">
       <input type="submit" value="search">
       </td>  
     </tr>
     <tr><td colspan="2">
      
     <div id="page_contents">
      <table border="1" align="center" width="100%">
      <tr><td>sl no</td><td>name</td></tr>
      <?if(mysql_num_rows($result)!=0){
       $counter = $starting + 1;
       while($data = mysql_fetch_array($result)) {?>
        <tr>
        <td><? echo $counter; ?></td>
        <td><? echo $data['name']; ?></td>
        </tr><?
        $counter ++;
       } ?>
       
         
       <tr><td colspan="2"><? echo $obj->anchors; ?></td></tr>
       <tr><td colspan="2"><? echo $obj->total; ?></td></tr>
      <?}else{?>//开源代码phpfensi.com
       <tr><td align="center" colspan="2">no data found</td></tr>
      <?}?>
      </td></tr>
      </table>
     </div>
     </td></tr>
    </table></form>
   
 
  pagination.js文件,代码如下:
     function $()  
  {  
    var elements = new array();  
    for (var i = 0; i < arguments.length; i++)  
    {  
      var element = arguments[i];  
      if (typeof element == 'string')  
        element = document.getelementbyid(element);  
      if (arguments.length == 1)  
        return element;  
      elements.push(element);  
    }  
    return elements;  
  }
   
  var xmlhttp
  function pagination(page)
  {
  xmlhttp=getxmlhttpobject();
  if (xmlhttp==null)
    {
    alert ("your browser does not support ajax!");
    return;
    }
  var url="test_sub.php";
  url = url+"?starting="+page;
  url = url+"&search_text="+$('search_text').value;
  url=url+"&sid="+math.random();
  xmlhttp.onreadystatechange=statechanged;
  xmlhttp.open("get",url,true);
  xmlhttp.send(null);
  }
   
  function statechanged()  
  {  
  if (xmlhttp.readystate==4)
  {  
  $("page_contents").innerhtml=xmlhttp.responsetext;
  }
  }
   
  function getxmlhttpobject()
  {
  var xmlhttp=null;
  try
    {
    // firefox, opera 8.0+, safari
    xmlhttp=new xmlhttprequest();
    }
  catch (e)
    {
    // internet explorer
    try
      {
      xmlhttp=new activexobject("msxml2.xmlhttp");
      }
    catch (e)
      {
      xmlhttp=new activexobject("microsoft.xmlhttp");
      }
    }
  return xmlhttp;
  }
  pagination_class.php,代码如下:
 
  <?php
  /*
  you can use it with out any worries...it is free for you..it will display the out put like:
  first | previous | 3 | 4 | 5 | 6 | 7| 8 | 9 | 10 | next | last
  page : 7  of  10 . total records found: 20
  */
  class pagination_class{
   var $result;
   var $anchors;
   var $total;
   function pagination_class($qry,$starting,$recpage)
   {
    $rst  = mysql_query($qry) or die(mysql_error());
    $numrows = mysql_num_rows($rst);
    $qry   .= " limit $starting, $recpage";
    $this->result = mysql_query($qry) or die(mysql_error());
    $next  = $starting+$recpage;
    $var  = ((intval($numrows/$recpage))-1)*$recpage;
    $page_showing = intval($starting/$recpage)+1;
    $total_page = ceil($numrows/$recpage);
   
    if($numrows % $recpage != 0){
     $last = ((intval($numrows/$recpage)))*$recpage;
    }else{
     $last = ((intval($numrows/$recpage))-1)*$recpage;
    }
    $previous = $starting-$recpage;
    $anc = "<ul id='pagination-flickr'>";
    if($previous < 0){
     $anc .= "<li class='previous-off'>first</li>";
     $anc .= "<li class='previous-off'>previous</li>";
    }else{
     $anc .= "<li class='next'><a href='网页特效:pagination(0);'>first </a></li>";
     $anc .= "<li class='next'><a href='javascript:pagination($previous);'>previous </a></li>";
    }
     
    ################if you dont want the numbers just comment this block###############  
    $norepeat = 4;//no of pages showing in the left and right side of the current page in the anchors  
    $j = 1;
    $anch = "";
    for($i=$page_showing; $i>1; $i--){
     $fpreviouspage = $i-1;
     $page = ceil($fpreviouspage*$recpage)-$recpage;
     $anch = "<li><a href='javascript:pagination($page);'>$fpreviouspage </a></li>".$anch;
     if($j == $norepeat) break;
     $j++;
    }
    $anc .= $anch;
    $anc .= "<li class='active'>".$page_showing."</li>";
    $j = 1;
    for($i=$page_showing; $i<$total_page; $i++){
     $fnextpage = $i+1;
     $page = ceil($fnextpage*$recpage)-$recpage;
     $anc .= "<li><a href='javascript:pagination($page);'>$fnextpage</a></li>";
     if($j==$norepeat) break;
     $j++;
    }
    ############################################################
    if($next >= $numrows){
     $anc .= "<li class='previous-off'>next</li>";
     $anc .= "<li class='previous-off'>last</li>";
    }else{
     $anc .= "<li class='next'><a href='javascript:pagination($next);'>next </a></li>";
     $anc .= "<li class='next'><a href='javascript:pagination($last);'>last</a></li>";
    }
     $anc .= "</ul>";
    $this->anchors = $anc;
     
    $this->total = "page : $page_showing <i> of  </i> $total_page . total records found: $numrows";
   }
  }
  

(编辑:呼伦贝尔站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读