php 无刷新分页

来源:互联网 发布:监控矩阵 编辑:程序博客网 时间:2024/03/28 23:29

首先看无刷新分页效果:

(一)

(二)

比较一二两副图可以看出,下面文字部分的翻页了,但是整个页面并没有刷新,视频依然正常播放下去。

 

无刷新的关键思路:

 

首页,上一页,下一页,尾页。点击时,调用onclick事件,然后跳转到另外一张AJAX处理页,同时传入页码参数,处理页返回满足接收页码参数的信息,并且将首页,上一页,下一页,尾页那段代码也原样返回。其实就是在一个特定的DIV里变化分页的结果而已。

 

以往分页都是提交本页面,需要刷新一次,而无刷新就是利用了AJAX,给每个翻页添加了一个点击函数,交给了另外一个处理页。

代码如下:

 

显示页indexx.php:

<?php
      include_once("function.php");
   if($_GET['page'] == '')
   {
      $_GET['page'] = 1;
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="js/xmlhttp.js" type="text/javascript"></script>
<script src="js/fenye.js" type="text/javascript"></script>
</head>

<body>
<div style="width:900px; height:auto; margin:auto;">
<embed src="http://player.youku.com/player.php/Type/Folder/Fid/6039532/Ob/1/Pt/0/sid/XMjY0NzY1NjQw/v.swf" quality="high" width="480" height="400" align="middle" allowScriptAccess="allways" mode="transparent" type="application/x-shockwave-flash"></embed>

</div>
<div id="fenye" style="width:900px; height:auto; border:1px solid #FF0000; margin:auto;">
<?php
      if($_GET['page'])
   {
      $contenter = iconv("gb2312","utf-8",file_get_contents('file/mr_synopsis.txt'));
   $length = strlen(unhtml($contenter));
   $pageCount = ceil($length/950);
   $prePage = msubstr($contenter,0,($_GET['page']-1)*950);
   $nowPage = msubstr($contenter,0,$_GET['page']*950);
   echo substr($nowPage,strlen($prePage),strlen($nowPage)-strlen($prePage));
   }
?>
<div style="width:900px;height:30px; margin:auto; background:#FFFF99;">
<span>
页次:<?php echo $_GET['page']; ?>/<?php echo $pageCount; ?>页
</span>
    <a href="#" onclick='return fenye("indexxok.php?page=1")'>首页</a>
 <?php   
if($_GET['page'] > 1 && $_GET['page'] <= $pageCount)
{
?>
    <a href="#" onclick="return fenye('indexxok.php?page=<?php echo $_GET["page"]-1; ?>')">上一页</a>
   
   
<?php
}
?>
 <?php   
if($_GET['page'] >= 1 && $_GET['page'] < $pageCount)
{
?>
   
    <a href="#" onclick="return fenye('indexxok.php?page=<?php echo $_GET["page"]+1; ?>')">下一页</a>
   
<?php
}
?>
<a href="#" onclick="return fenye('indexxok.php?page=<?php echo $pageCount; ?>')">尾页</a>
</div>
</div>

</body>
</html>

 

function.php:

<?php
    function unhtml($content)
 {
   $content = htmlspecialchars($content);                //转换文本中的特殊字符
   $content = str_replace(chr(13),"<br>",$content);  //替换文本中的换行符
      $content = str_replace(chr(32),"&nbsp;",$content);  //替换文本中的&nbsp;
      $content = str_replace("[_[","<",$content);   //替换文本中的大于号
      $content = str_replace(")_)",">",$content);   //替换文本中的小于号
      $content = str_replace("|_|"," ",$content);    //替换文本中的空格
   $content = trim($content);                             //删除文本中首尾的空格
    return $content;
 }
 
 function msubstr($str,$start,$len)           //定义一个用于截取一段字符串的函数msubstr()
 {                                            //$str指的是字符串,$start指的是字符串的起始位置,$len指的是长度。
   $strlen = $start+$len;                     //用$strlen存储字符串的总长度(从字符串的起始位置到字符串的总长度)
   $tmpstr = '';                              //定义变量$tmpstr
   for($i=0;$i<$strlen;$i++)                  //通过for循环语句,循环读取字符串
   {
       if(ord(substr($str,$i,1)) > 0xa0)      //如果字符串中首个字节的ASCII序数值大于0xa0,则表示为汉字,ord() 函数返回字符串第一个字符的 ASCII 值。
    {
        $tmpstr .= substr($str,$i,2);      //每次取出两位字节赋给变量$tmpstr,即等于一个汉字
     $i++;                                 //变量自加1
    }
    else                                  //如果不是汉字,则每次取出一位字符赋给变量$tmpstr
    {
        $tmpstr .=substr($str,$i,1);
    }
   
   }
   return $tmpstr;
 }
?>

 

indexxok.php:


<?php
      include_once("function.php");
   if($_GET['page'] == '')
   {
      $_GET['page'] = 1;
   }
   if($_GET['page'])
   {
      $contenter = iconv("gb2312","utf-8",file_get_contents('file/mr_synopsis.txt'));
   $length = strlen(unhtml($contenter));
   $pageCount = ceil($length/950);
   $prePage = msubstr($contenter,0,($_GET['page']-1)*950);
   $nowPage = msubstr($contenter,0,$_GET['page']*950);
   echo substr($nowPage,strlen($prePage),strlen($nowPage)-strlen($prePage));
   }
  
?>

 


<div style="width:900px;height:30px; margin:auto; background:#FFFF99;">
<span>
页次:<?php echo $_GET['page']; ?>/<?php echo $pageCount; ?>页
</span>
    <a href="#" onclick='return fenye("indexxok.php?page=1")'>首页</a>
 <?php   
if($_GET['page'] > 1 && $_GET['page'] <= $pageCount)
{
?>
    <a href="#" onclick="return fenye('indexxok.php?page=<?php echo $_GET["page"]-1; ?>')">上一页</a>
   
   
<?php
}
?>
 <?php   
if($_GET['page'] >= 1 && $_GET['page'] < $pageCount)
{
?>
   
    <a href="#" onclick="return fenye('indexxok.php?page=<?php echo $_GET["page"]+1; ?>')">下一页</a>
   
<?php
}
?>
<a href="#" onclick="return fenye('indexxok.php?page=<?php echo $pageCount; ?>')">尾页</a>
</div>

 

 

 

 

原创粉丝点击