大家在浏览网页的时候,都会看到每篇文章都会会显示“上一页”“下一页”。下面跟大家分享一下如何用php实现简单的分页功能。
首先,先创建一个mysql表:
SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `test`
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
然后在里面插入数据:
-
- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', '张三');
INSERT INTO `test` VALUES ('2', '李四');
INSERT INTO `test` VALUES ('3', '王五');
INSERT INTO `test` VALUES ('4', '赵六');
INSERT INTO `test` VALUES ('5', '阿萨达');
INSERT INTO `test` VALUES ('6', '回火');
INSERT INTO `test` VALUES ('7', 'adada');
然后新建一个php文件,连接数据库:
header("content-type:text/html;charset=UTF-8");//防止页面乱码
$link = mysql_connect("localhost","root","") or die("数据库连接失败");
$result = mysql_select_db("test",$link);
mysql_query("SET NAMES 'utf8'");//设置数据库连接编码
然后调取数据,并且计算出该表里面总共有多少条数据:
function selectdata(){
$sql = "SELECT name FROM `test` ORDER BY id desc";
$arr =  array();
$result = mysql_query($sql);

while ($array = mysql_fetch_row($result)) {
    $arr[] = $array;
}
return $arr;
}
function selectdatabypages($limit1,$limit2){
$sql = "SELECT name FROM `test` ORDER BY id desc LIMIT $limit1,$limit2";
$arr =  array();
$result = mysql_query($sql);
简单网页while ($array = mysql_fetch_row($result)) {
    $arr[] = $array;
}
return $arr;

}
function countdata(){
$sql = "SELECT name FROM `test` ORDER BY id desc";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
return $num;

}
最后,来制作页码:
$page = @$_GET["page"];
if ($page == null) {
    $page = 1;
}
$each_disNums=5;//每次显示的条数
$pageNums = ceil($num / $each_disNums);//总页数
$startPage=($page-1)*$each_disNums;//开始每一页的第一条记录
echo "<a href=index.php?page=".($page-1).">上一页</a>";
echo "<a href=index.php?page=".($page+1).">下一页</a>";
echo '共有' . $num . '条数据' . '' . $pageNums . '';
下面是完整的代码:
<?php
header("content-type:text/html;charset=UTF-8");
$link = mysql_connect("localhost","root","") or die("数据库连接失败");
$result = mysql_select_db("test",$link);
mysql_query("SET NAMES 'utf8'");
$data = selectdata();
$num = countdata();

$page = @$_GET["page"];
if ($page == null) {
    $page = 1;#
}
$each_disNums=5;//每次显示的条数
$pageNums = ceil($num / $each_disNums);//总页数
$startPage=($page-1)*$each_disNums;//开始每一页的第一条记录
$arr = selectdatabypages($startPage,$each_disNums);
foreach ($arr as $key => $value) {
    $name = $value[0];
    echo "姓名:" . $name ."<br>";
}
echo "<a href=index.php?page=".($page-1).">上一页</a>";
echo "<a href=index.php?page=".($page+1).">下一页</a>";
echo '共有' . $num . '条数据' . '' . $pageNums . '';
function selectdata(){
$sql = "SELECT name FROM `test` ORDER BY id desc";
$arr =  array();
$result = mysql_query($sql);

while ($array = mysql_fetch_row($result)) {
    $arr[] = $array;
}
return $arr;
}
function selectdatabypages($limit1,$limit2){
$sql = "SELECT name FROM `test` ORDER BY id desc LIMIT $limit1,$limit2";
$arr =  array();
$result = mysql_query($sql);
while ($array = mysql_fetch_row($result)) {
    $arr[] = $array;
}
return $arr;

}
function countdata(){
$sql = "SELECT name FROM `test` ORDER BY id desc";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
return $num;

}


?>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。