当前位置: 开源爱好者 » 编程教程 » PHP教程 » 正文

PHP Ajax 分页 无使用数据库

PHP读取超长文本中的数据,实现超长文本中数据的分页显示,调用no_refurbish_pagination函数,实现无刷新的分页输出:

<?php
header('content-type:text/html;charset=utf-8');
if($_GET['page']==""){
	$_GET['page']=1;
}
?>
<table width="545" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td height="20">&nbsp;</td>
                   </tr>
                   <tr>
                     <td>
<?php
include("function.php");
//读取超长文本中的数据,实现超长文本中数据的分页显示
  if($_GET['page']){
$counter=file_get_contents("data.txt");
$length=strlen($counter);
$page_count=ceil($length/800);
$c=msubstr($counter,0,($_GET['page']-1)*800);
$c1=msubstr($counter,0,$_GET['page']*800);
echo substr($c1,strlen($c),strlen($c1)-strlen($c));
}
?>                     </td>
                   </tr>
                   <tr>
                     <td><table width="535" border="0" align="center" cellpadding="0" cellspacing="0">
                         <tr>
                           <!--设置超长文本分页显示的超级连接-->
                          <td width="37%" align="center" valign="middle" bgcolor="#FFFFFF">内容&nbsp;<?php echo $page_count;?> 页&nbsp;&nbsp;当前&nbsp;第 <?php echo $_GET['page'];?> 页</td>
                           <td width="63%" height="28" align="center" valign="middle" bgcolor="#FFFFFF">
                             <?php
	  		  if($_GET['page']!=1){
?>
<!--调用no_refurbish_pagination函数,实现无刷新的分页输出-->
<a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=1")'>首页</a>&nbsp; <a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=<?php echo $_GET['page']-1;?>")'>上一页</a>
                             <?php  }
		if($_GET['page']<$page_count){
?>
<a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=<?php echo $_GET['page']+1;?>")'>下一页</a> <a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=<?php echo $page_count;?>")'>尾页</a>
                             <?php
		   }
			  ?>
                          </td>
                         </tr>
                     </table></td>
                   </tr>
                 </table>

主处理文件,index.php文件代码:

<?php 
if($_GET['page']==""){
	$_GET['page']=1;
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax无刷新分页</title>
</head>
<script type="text/javascript" src="js/discuss.js"></script>
<body>

<table width="900" height="675" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bg.jpg">
       <tr>
         <td width="260" height="175">&nbsp;</td>
         <td width="570">&nbsp;</td>
         <td width="70">&nbsp;</td>
       </tr>
       <tr>
         <td height="380">&nbsp;</td>
         <td align="center" valign="top"><table width="550" border="0" cellspacing="0" cellpadding="0">
           <tr>
             <td width="275" align="center" valign="top"><div id="synopsis">
               <!--创建div标签,用于获取js文件中返回的分页结果-->
                 <table width="545" border="0" cellspacing="0" cellpadding="0">
                   <tr>
                     <td height="20">&nbsp;</td>
                   </tr>
                   <tr>
                     <td><?php 		
					 include("function.php");	
//读取超长文本中的数据,实现超长文本中数据的分页显示
  if($_GET['page']){
	 $counter=file_get_contents("data.txt");
     $length=strlen($counter);
     $page_count=ceil($length/800);
     $c=msubstr($counter,0,($_GET['page']-1)*800);
     $c1=msubstr($counter,0,$_GET['page']*800);
		echo substr($c1,strlen($c),strlen($c1)-strlen($c)); 
  }?>                     </td>
                   </tr>
                   <tr>
                     <td><table width="535" border="0" align="center" cellpadding="0" cellspacing="0">
                         <tr>
                           <!--设置超长文本分页显示的超级连接-->
                           <td width="37%" align="center" valign="middle" bgcolor="#FFFFFF">内容&nbsp;<?php echo $page_count;?> 页&nbsp;&nbsp;当前&nbsp;第 <?php echo $_GET['page'];?> 页</td>
                           <td width="63%" height="28" align="center" valign="middle" bgcolor="#FFFFFF">
                             <?php
	  		  if($_GET['page']!=1){?>
                             <!--调用no_refurbish_pagination函数,实现无刷新的分页输出-->
                             <a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=1")'>首页</a>&nbsp; <a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=<?php echo $_GET['page']-1;?>")'>上一页</a>
                             <?php  }
				  if($_GET['page']<$page_count){?>
                             <a href="#" onClick="return no_refurbish_pagination('index_ok.php?page=<?php echo $_GET['page']+1;?>')">下一页</a> <a href="#" onClick='return no_refurbish_pagination("index_ok.php?page=<?php echo $page_count;?>")'>尾页</a>
                             <?php		
		   }				   
			  ?>
                            </td>
                         </tr>
                     </table></td>
                   </tr>
                 </table>
             </div></td>
           </tr>
         </table></td>
         <td>&nbsp;</td>
       </tr>
       <tr>
         <td height="120">&nbsp;</td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
       </tr>
</table>
</body>
</html>

discuss.js文件代码,完成 无刷新 分页的功能:

var xmlHttp = false;
try {
  	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  	try {
    		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  	} catch (e2) {}
}

if (!xmlHttp && typeof XMLHttpRequest != "undefined") {
	try
	{
  		xmlHttp = new XMLHttpRequest();
	}catch(e3){ xmlHttp = false;}
}
//使用XMLHttpRequest对象创建异步HTTP请求
function no_refurbish_pagination(url){//创建自定义函数,获取传递的参数
	xmlHttp.open('get',url,true);//根据传递的参数,通过get方法,执行另外一个实现分页功能的文件
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readystate == 4 && xmlHttp.status == 200){//将结果返回到div标签synopsis中
			document.getElementById("synopsis").innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.send(null);
}
相关内容:

CopyRight 2015© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图