Web实验六-----实现PHP的翻页功能

show_student.php 采用分页形式完成,每页只显示 20 个学生,提供“上一页”、 “下一页”的超链接,以及“翻到”某一页的输入框与按钮。

<?php
     $limit=20;//每页输出20条数据
     $start=0;
     $s=0;
//获取当前页码
//$page2=$_POST['page2'];
if (!empty($page2=$_POST['page2'])){  //输入框
    $page=$page2;
}
else{
    $page=$_GET['page'];   //上一页、下一页
}
if($page==0){
    $page=1;
}
//连接数据库
     $db=new MySqli('localhost','root','123456','web_exp');
     $db->query('set names "utf8"');
     $sql="Select * from students Order By sno";//查找全部数据
     $result=$db->query($sql);
     $num_max=mysqli_num_rows($result);//数据库数据全部的行数
     $start=($page-1)*$limit;   //更新下一页序号
     $result2=$db->query("select * from students order by sno limit $start,$limit");
     $num=mysqli_num_rows($result2);//每页中的数据

//计算总页数
if($num_max==0)
    $pagecount=0;
else if($num_max<$limit||$num_max==$limit){
    $pagecount=1;
}
else if($num_max%$limit==0){
    $pagecount=$num_max/$limit;
}
else
    $pagecount=(int)($num_max/$limit)+1;
//echo ("当前页码:".$page."/".$pagecount."<br/>");
     ?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
        <style type="text/css">
            * {font-size:16px;}
        #main {width:850px; margin: 50px auto;}

        #main table {text-align:center;
            vertical-align:middle;
            width:100%}
        #main table, #main td, #main th  {
            border:1px solid black;
            border-collapse:collapse;
        }

        #main tr {height:40px;}

        tr:nth-child(odd){background-color:lime;}
        tr:nth-child(1)  {background-color:yellow;color:red;
            font-weight:bold;}
        #main a{display:inline-block; margin-right:20px;}

        #main form input[type="text"] {width:20px; text-align:right;padding-right:5px}
    </style>
</head>
<body>
<div id="main">
<table>
    <tr>
        <td>序号</td>
        <td>学号</td>
        <td>姓名</td>
        <td>题目</td>
        <td>状态</td>
        <td>录入时间</td>
        <td>合作学生</td>
    </tr>
    <?php
    $i=$start;  //序号
    while ($obj=$result2->fetch_object())
    {
        $i++;
        ?>
    <tr>
        <td><?=$i?></td>
        <td><?=$obj->sno?></td>
        <td><?=$obj->name?></td>
        <td><?=$obj->title?></td>
        <td><?=$obj->state?></td>
        <td><?=$obj->last_time?></td>
        <td><?=$obj->partner?></td>
    </tr>
    <?php }
    $result2->free_result();
    $db->close();
//    //显示分页连接
//    if ($page==1){
//        echo ("第一页");
//    }
//    else
//        echo ("<a href=showstudents.php?page=1>第一页</a>");
//    //设置上一页连接
//    if($page==1){
//        echo ("上一页");
//    }
//    else
//        echo ("<a href=showstudents.php?page=".($page-1).">上一页</a>");
//
//    //设置下一页连接
//    if($page==$pagecount){
//        echo ("下一页");
//    }
//    else
//        echo ("<a href=showstudents.php?page=".($page+1).">下一页</a>");
//    //设置最后一页
//    if ($page==$pagecount){
//        echo ("最后一页");
//    }
//    else
//        echo ("<a href=showstudents.php?page=".$pagecount.">最后一页</a>");
    ?>
</table>
</div>
<div style="margin: auto">
    <p><a href="web_实验6_2.html">返回输入界面</a></p>
    <form action="#" method="post">
        <input type="submit" value="翻到">
        <input type="text" name="page2" size="2" value="<?=$page2?>" >/<?=$pagecount?>页&nbsp;&nbsp;
        <a href="showstudents.php?page=<?=($page-1)?>">前一页</a>
        <a href="showstudents.php?page=<?=($page+1)?>">后一页</a>
    </form>
</div>
</body>
</html>

结果显示:(翻到最后一页)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值