Home  >  Article  >  Backend Development  >  PHP and ajax non-refresh paging implementation code (compatible with multiple browsers)

PHP and ajax non-refresh paging implementation code (compatible with multiple browsers)

WBOY
WBOYOriginal
2016-07-25 09:00:16920browse
php与ajax结合实现的无刷新分页的简单例子,供初学的朋友参考。

无刷新分页,兼容IE与Firefox等浏览器。

1、index.php

<?php
 header("Content-Type:text/html;charset=utf-8");
?>
<html>
<head>
<title>无刷新分页_bbs.it-home.org</title>
<style>
A{text-decoration:none;}
A:link {COLOR:#33CCFF;}
A:active {COLOR:#FF6666;}    
A:visited {COLOR:#33CCFF;}    
A:hover {COLOR:#FF6699; TEXT-DECORATION: underline;position:relative;left:1px;top:1px}
</style>

<script language="javascript">
    $(document).ready(function(){
        changepage(1);
    });
    function changepage(page){
        $.post("sql.php",{page:page},function(data){
            $("#test").html(data);
        });
    }
</script>
</head>
<body>
    <ul id="test"></ul>
</body>
</html>

2、sql.php

<?php
mysql_connect("","root","");
mysql_select_db("test");
mysql_query("set names utf8");

if(isset($_POST["page"])){
    @$page = max(1, intval($_POST["page"]));
    $pagesize=10;
    $startindex=($page-1)*$pagesize;
    $sql="SELECT * FROM test ORDER BY id LIMIT $startindex,$pagesize";
    $rec=mysql_query($sql);
    while($row=mysql_fetch_array($rec)){
        $str.="<li>".$row["uname"]."</li>";
    }
    $num=mysql_num_rows(mysql_query("select * from test"));
    $pagenum=@ceil($num/$pagesize);
    for($i=1;$i<=$pagenum;$i++){
        if($page==$i){
            $str.="[".$i."]   ";
        }else{
            $str.="".$i."   ";
        }
    }
    echo $str;
}else{
    die();
}
?>

3、test数据库 test表

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(10) NOT NULL auto_increment,
  `uname` char(50) character set utf8 NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 COLLATE=gb2312_bin;

-- ----------------------------
-- Records
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'aaaaaaaaaaa');
INSERT INTO `test` VALUES ('2', 'bbbbbbbbbbbb');
INSERT INTO `test` VALUES ('3', 'cccccccccccc');
INSERT INTO `test` VALUES ('4', 'dddddddddddddddd');
INSERT INTO `test` VALUES ('5', 'eeeeeeeeeee');
INSERT INTO `test` VALUES ('6', 'ffffffffff');
INSERT INTO `test` VALUES ('7', 'ggggggggggggggg');
INSERT INTO `test` VALUES ('8', 'hhhhhhhhhhhh');
INSERT INTO `test` VALUES ('9', 'jjjjjjjjjjjjjjjj');
INSERT INTO `test` VALUES ('10', 'kkkkkkkkkkk');
INSERT INTO `test` VALUES ('11', 'mmmmmmmmmmm');
INSERT INTO `test` VALUES ('12', 'cccccccccccccccccc');
INSERT INTO `test` VALUES ('13', 'bbbbbbbbbbbb');
INSERT INTO `test` VALUES ('14', 'ooooooooooooooooo');
INSERT INTO `test` VALUES ('15', 'pppppppppppp');
INSERT INTO `test` VALUES ('16', 'aaaaaaaaaaaaaaa');
INSERT INTO `test` VALUES ('17', 'ggggggggggggggggg');
INSERT INTO `test` VALUES ('18', 'dddddddddddddddd');
INSERT INTO `test` VALUES ('19', 'cccccccccccccc');
INSERT INTO `test` VALUES ('20', 'xxxxxxxxxx');
INSERT INTO `test` VALUES ('21', 'zzzzzzzzzzzzzzz');
INSERT INTO `test` VALUES ('22', 'oooooooooooooooooooo');
INSERT INTO `test` VALUES ('23', 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii');
INSERT INTO `test` VALUES ('24', 'uuuuuuuuuuuuuuuuuuuuuuuuuuuuu');
INSERT INTO `test` VALUES ('25', 'yyyyyyyyyyyyyyyyyy');
INSERT INTO `test` VALUES ('26', 'ttttttttttttttttttttttttttt');
INSERT INTO `test` VALUES ('27', 'rrrrrrrrrrrrrrrrrrrrrrrr');
INSERT INTO `test` VALUES ('28', 'eeeeeeeeeeeeeeeeeeeee');
INSERT INTO `test` VALUES ('29', 'wwwwwwwwwwwwwwwwwwwww');
INSERT INTO `test` VALUES ('30', 'qqqqqqqqqqqqq');


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn