一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ajax +php无刷新分页代码

时间:2022-06-24 18:30:06 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

//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'");
?>

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;

   
   ?>


   
   
   
    
   
   
  

    search
     
    

   
   

    
    
          $counter = $starting + 1;
     while($data = mysql_fetch_array($result)) {?>
      
      
      
             $counter ++;
     } ?>
    
      
     
     
    
     
    
    
    
sl noname
anchors; ?>
total; ?>
no data found

   

   

   


 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

 

 代码如下 复制代码

/*
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 = "

    ";
      if($previous < 0){
       $anc .= "
  • first
  • ";
       $anc .= "
  • previous
  • ";
      }else{
       $anc .= "";
       $anc .= "";
      }
      
      ################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 = "
  • $fpreviouspage
  • ".$anch;
       if($j == $norepeat) break;
       $j++;
      }
      $anc .= $anch;
      $anc .= "
  • ".$page_showing."
  • ";
      $j = 1;
      for($i=$page_showing; $i<$total_page; $i++){
       $fnextpage = $i+1;
       $page = ceil($fnextpage*$recpage)-$recpage;
       $anc .= "
  • $fnextpage
  • ";
       if($j==$norepeat) break;
       $j++;
      }
      ############################################################
      if($next >= $numrows){
       $anc .= "
  • next
  • ";
       $anc .= "
  • last
  • ";
      }else{
       $anc .= "";
       $anc .= "";
      }
       $anc .= "
";
  $this->anchors = $anc;
  
  $this->total = "page : $page_showing of  $total_page . total records found: $numrows";
 }
}
?>

数据库教程

-- phpmyadmin sql dump
-- version 3.2.4
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2010 年 07 月 07 日 09:26
-- 服务器版本: 5.1.41
-- php 版本: 5.3.1

set sql_mode="no_auto_value_on_zero";

--
-- 数据库: `mydemo`
--

-- --------------------------------------------------------

--
-- 表的结构 `students`
--

create table if not exists `students` (
  `id` int(11) not null auto_increment,
  `name` varchar(50) not null default '',
  primary key (`id`)
) engine=myisam  default charset=utf8 auto_increment=21 ;

--
-- 转存表中的数据 `students`
--

insert into `students` (`id`, `name`) values
(1, '小明'),
(2, 'aniesh'),
(3, 'babu'),
(4, '小黄'),
(5, 'praveesh'),
(6, 'dixon'),
(7, 'sanju'),
(8, 'neeraj'),
(9, 'siju'),
(10, 'noble'),
(11, 'bibin'),
(12, 'febin'),
(13, 'binu'),
(14, 'charles'),
(15, 'jaggu'),
(16, 'mani'),
(17, 'milu'),
(18, 'aravind'),
(19, 'jay'),
(20, 'hari');

热门栏目