108)PHP分页显示
一个代码页的链接:https://www.cnblogs.com/mmykdbc/p/6688460.html
首先一个简单的代码展示:
目录关系:
数据库表格展示:
结果展示:
然后 代码展示:
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>分页</title> 6 <?php 7 8 //分页的函数 9 function news($pageNum = 1, $pageSize = 3) 10 { 11 $array = array(); 12 $coon = mysqli_connect("localhost", "root",'root','test'); 13 // limit为约束显示多少条信息,后面有两个参数,第一个为从第几个开始,第二个为长度 14 $rs = "select * from chengji where xuehao >='150100' and xuehao <150200 limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize; 15 $r = mysqli_query($coon, $rs); 16 while ($obj = mysqli_fetch_object($r)) { 17 $array[] = $obj; 18 } 19 mysqli_close($coon); 20 return $array; 21 } 22 23 //显示总页数的函数 24 function allNews() 25 { 26 $coon = mysqli_connect("localhost", "root",'root','test'); 27 $rs = "select count(*) num from chengji"; //可以显示出总页数 28 $r = mysqli_query($coon, $rs); 29 $obj = mysqli_fetch_object($r); 30 mysqli_close($coon); 31 return $obj->num; 32 } 33 34 @$allNum = allNews(); 35 @$pageSize = 3; //约定没页显示几条信息 36 @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"]; 37 @$endPage = ceil($allNum/$pageSize); //总页数 38 @$array = news($pageNum,$pageSize); 39 ?> 40 </head> 41 <body> 42 43 <table border="1" style="text-align: center" cellpadding="0"> 44 <tr> 45 <th>学号</th> 46 <th>姓名</th> 47 <th>课程名字</th> 48 <th>平时成绩</th> 49 <th>实验</th> 50 <th>作业</th> 51 <th>期末成绩</th> 52 <th>总成绩</th> 53 </tr> 54 <?php 55 foreach($array as $key=>$values){ 56 echo "<tr>"; 57 echo "<td>{$values->xuehao}</td>"; 58 echo "<td>{$values->xingming}</td>"; 59 echo "<td>{$values->class_name}</td>"; 60 echo "<td>{$values->pingshihcengji}</td>"; 61 echo "<td>{$values->shiyanfenshu}</td>"; 62 echo "<td>{$values->zuoye}</td>"; 63 echo "<td>{$values->qimochengji}</td>"; 64 echo "<td>{$values->zongchengji}</td>"; 65 echo "</tr>"; 66 } 67 ?> 68 69 </table> 70 71 <div> 72 <a href="?pageNum=1">首页</a> 73 <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>">上一页</a> 74 <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>">下一页</a> 75 <a href="?pageNum=<?php echo $endPage?>">尾页</a> 76 77 </div> 78 79 </body> 80 </html>
在一个小的作业中的应用:
1 5)最大的体会就是分页显示,由于,每一次点击‘上一页’或者‘下一页’,都是页面刷新一次,使得, 2 我用$_POST接收的变量在第二次请求时会清空,然后点击‘上一页’或者‘下一页’就没有显示,为什么会这样?因为我的第一次显示是接着上一次查询页面的,自然我用$_POST['neirong']是有值的, 3 但是,我再一次刷新看这个页面,那个 $_POST['neirong']就会清空,因为此次的url请求是承接我上一次的页面请求,上一次没有向服务器提交 4 $_POST['neirong'],所以 没有这个值, 5 我后面的很多与之相关的变量就是空值, 6 自然,就没有数据显示。不过后来 7 我把第一次收到的这个$_POST['neirong']通过GET形式, 8 每次传给新的页面,这样$_POST['neirong']就不会丢失了。
代码展示:
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <link rel="stylesheet" type="text/css" href="../CSS/H-ui.min.css"/> 6 <link rel="stylesheet" type="text/css" href="../CSS/H-ui.admin.css"/> 7 <link rel="stylesheet" type="text/css" href="../CSS/iconfont.css"/> 8 <link rel="stylesheet" type="text/css" href="../CSS/skin.css" id="skin"/> 9 <link rel="stylesheet" type="text/css" href="../CSS/style.css"/> 10 <title>哈尔滨理工大学成绩处理页面</title> 11 <?php 12 session_start(); 13 $user=$_SESSION['user']; 14 include '../PHP/db.php'; 15 16 if(is_null($_POST['banji'])) 17 { 18 $banji=$_GET['banji']; 19 } 20 else{ 21 $banji=$_POST['banji']; 22 } 23 $number=(int) $banji[0]*100000; 24 25 $number=$number+(int) $banji[1]*10000; 26 27 $number=(int) $banji[5]*100+$number; 28 // echo $number; 29 30 if($number==150100) 31 { 32 $string="15级1班"; 33 } 34 if($number==150200) 35 { 36 $string="15级2班"; 37 } 38 if($number==150600) 39 { 40 $string="15级6班"; 41 } 42 if($number==150700) 43 { 44 $string="15级7班"; 45 } 46 $numberber=$number+100; 47 /** 48 * 分页的函数 49 */ 50 function news($pageNum = 1, $pageSize = 5,$number) 51 { 52 $array = array(); 53 $numberber=$number+100; 54 $coon = mysqli_connect("localhost", "root",'root','test'); 55 // limit为约束显示多少条信息,后面有两个参数,第一个为从第几个开始,第二个为长度 56 $rs = "select * from chengji where xuehao >=$number and xuehao <$numberber limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize; 57 $r = mysqli_query($coon, $rs); 58 while ($obj = mysqli_fetch_object($r)) { 59 $array[] = $obj; 60 } 61 mysqli_close($coon); 62 return $array; 63 } 64 65 //显示总页数的函数 66 function allNews() 67 { 68 $coon = mysqli_connect("localhost", "root",'root','test'); 69 $rs = "select count(*) num from chengji"; //可以显示出总页数 70 $r = mysqli_query($coon, $rs); 71 $obj = mysqli_fetch_object($r); 72 mysqli_close($coon); 73 return $obj->num; 74 } 75 // 76 $allNum = allNews(); 77 $pageSize = 5; //约定没页显示几条信息 78 $pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"]; 79 $endPage = ceil($allNum/$pageSize); //总页数 80 $array = news($pageNum,$pageSize,$number); 81 ?> 82 </head> 83 <body> 84 <header class="navbar-wrapper"> 85 <div class="navbar navbar-fixed-top"> 86 <div class="container-fluid cl"> 87 <a class="logo navbar-logo f-l mr-10 hidden-xs" href="/aboutHui.shtml">成绩处理界面</a> 88 <nav class="nav navbar-nav"> 89 <ul class="cl"> 90 <li class="dropDown dropDown_hover"> 91 <a href="javascript:;" class="dropDown_A"> 92    你开心,我也开心😀     93 </a> 94 </li> 95 </ul> 96 </nav> 97 <nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs"> 98 <ul class="cl"> 99 <li><b><?php echo $user ?></b></li> 100 <li class="dropDown dropDown_hover"><a href="#" class="dropDown_A">admin 101 <i class="Hui-iconfont">↓</i></a> 102 <ul class="dropDown-menu menu radius box-shadow"> 103 <li><a href="#">退出</a></li> 104 </ul> 105 </li> 106 <li id="Hui-skin" class="dropDown right dropDown_hover"> 107 <a href="javascript:;" class="dropDown_A" title="换肤"> 108 <i class="Hui-iconfont" style="font-size:18px">♥</i> 109 </a> 110 <ul class="dropDown-menu menu radius box-shadow"> 111 <li><a href="javascript:;" data-val="default" title="默认(黑色)">默认(黑色)</a></li> 112 <li><a href="javascript:;" data-val="blue" title="蓝色">蓝色</a></li> 113 <li><a href="javascript:;" data-val="green" title="绿色">绿色</a></li> 114 <li><a href="javascript:;" data-val="red" title="红色">红色</a></li> 115 <li><a href="javascript:;" data-val="yellow" title="黄色">黄色</a></li> 116 <li><a href="javascript:;" data-val="orange" title="橙色">橙色</a></li> 117 </ul> 118 </li> 119 </ul> 120 </nav> 121 </div> 122 </div> 123 </header> 124 <aside class="Hui-aside"> 125 126 <div class="menu_dropdown bk_2"> 127 <dl id=""> 128 <dt> 129 <i class="Hui-iconfont"></i> 130 <a href="../PHP/daohtml_one.php">主目录</a> 131 </dt> 132 </dl> 133 <dl > 134 <dt> 135 <i class="Hui-iconfont"></i> 136 <a href="../PHP/daohtml_chamouban.php">查询某班成绩</a> 137 </dt> 138 </dl> 139 <dl > 140 <dt> 141 <i class="Hui-iconfont"></i> 142 <a href="../PHP/daohtml_tianjiachengjidan.php">添加成绩单</a> 143 </dt> 144 </dl> 145 <dl > 146 <dt> 147 <i class="Hui-iconfont"></i> 148 <a href="../PHP/daohtml_xiugaimoubanchengji.php">修改某班成绩</a> 149 </dt> 150 </dl> 151 <dl > 152 <dt> 153 <i class="Hui-iconfont"></i> 154 <a href="../PHP/daohtml_chaxunmouxueshnegchengji.php">查询某学生成绩</a> 155 </dt> 156 </dl> 157 <dl id="menu-article"> 158 <dt> 159 <i class="Hui-iconfont"></i> 160 <a href="../PHP/chasuojiaokemu.php">查看自己所教科目</a> 161 </dt> 162 </dl> 163 <dl id="menu-picture"> 164 <dt> 165 <i class="Hui-iconfont"></i> 166 <a href="../PHP/shoukebanji.php">查看自己授课班级</a> 167 </dt> 168 </dl> 169 </div> 170 </aside> 171 <div class="dislpayArrow hidden-xs"><a class="pngfix" href="javascript:void(0);" onClick="displaynavbar(this)"></a> 172 </div> 173 174 <section class="Hui-article-box"> 175 <nav class="breadcrumb"> 176 <i class="Hui-iconfont"></i> 177 <a href="../html/login.html" class="maincolor" >重新登录</a> 178 <span class="c-999 en">></span> 179 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"> 180 <i class="Hui-iconfont"></i> 181 </a> 182 </nav> 183 <div class="Hui-article"> 184 <article class="cl pd-20"> 185 <h2 align=center>班级成绩展示</h2> 186 <table class="table table-border table-bordered table-bg"> 187 <thead> 188 <tr class="text-c"> 189 <th>学号</th> 190 <th>姓名</th> 191 <th>课程名字</th> 192 <th>平时成绩</th> 193 <th>实验</th> 194 <th>作业</th> 195 <th>期末成绩</th> 196 <th>总成绩</th> 197 </tr> 198 </thead> 199 <tbody> 200 <?php 201 foreach($array as $key=>$values){ 202 echo "<tr>"; 203 echo "<td>{$values->xuehao}</td>"; 204 echo "<td>{$values->xingming}</td>"; 205 echo "<td>{$values->class_name}</td>"; 206 echo "<td>{$values->pingshihcengji}</td>"; 207 echo "<td>{$values->shiyanfenshu}</td>"; 208 echo "<td>{$values->zuoye}</td>"; 209 echo "<td>{$values->qimochengji}</td>"; 210 echo "<td>{$values->zongchengji}</td>"; 211 echo "</tr>"; 212 } 213 ?> 214 </tbody> 215 </table> 216 </article> 217 <div align="center"> 218 <br> 219 <br> 220 <a href="../PHP/chaxunmouban.php?pageNum=1&banji=<?php echo $banji?>">   首页   </a> 221 <a href="../PHP/chaxunmouban.php?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>&banji=<?php echo $banji?>">   上一页   </a> 222 <a href="../PHP/chaxunmouban.php?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>&banji=<?php echo $banji?>">   下一页   </a> 223 <a href="../PHP/chaxunmouban.php?pageNum=<?php echo $endPage?>&banji=<?php echo $banji?>">   尾页   </a> 224 225 </div> 226 227 <footer class="footer"> 228 </footer> 229 </div> 230 </section> 231 <script type="text/javascript" src="../JS/jquery.min.js"></script> 232 <script type="text/javascript" src="../JS/layer.js"></script> 233 <script type="text/javascript" src="../JS/H-ui.js"></script> 234 <script type="text/javascript" src="../JS/H-ui.admin.page.js"></script> 235 </body> 236 </html>