php实现分页功能的详细实例方法(2)

4、在页面进行显示

在页面显示的话,需要用到了foreach循环了,代码简单,看下面:

<table border="1" style="text-align: center" cellpadding="0">

 <tr>

  <td>编号</td>

  <td>新闻标题</td>

  <td>来源</td>

  <td>点击率</td>

  <td>发布日期</td>

 </tr>

 <?php

 foreach($array as $key=>$values){

  echo "<tr>";

  echo "<td>{$values->id}</td>";

  echo "<td>{$values->title}</td>";

  echo "<td>{$values->src}</td>";

  echo "<td>{$values->indexs}</td>";

  echo "<td>{$values->times}</td>";

  echo "</tr>";

 }

 ?>

</table>

5、实现上一页,下一页的效果

要实现页面跳转的效果,我们需要用到了a标签的href属性,地址写“?pageNum = ...”这个pageNum是我们自己定义的,由于是get来传递的,在上一步里面我们已经用$_GET接受了参数,所以我们只需要进行get的参数传递就可以了;

首页:“pageNum=1”;

上一页:"pageNum=<?php echo $pageNum==1?1:$pageNum-1?>"

下一页:"pageNum=<?php echo $pageNum==$endPage ?$endPage :$pageNum+1?>"

尾页:“pageNum=<?php echo $pageNum =$endPage?>”;

特别注意的是,”pageNum=”的时候后面千万千万不要空格,代码如下:

<a href="?pageNum=1" rel="external nofollow" rel="external nofollow" >首页</a>

<a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>" rel="external nofollow" rel="external nofollow" >上一页</a>

<a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>" rel="external nofollow" rel="external nofollow" >下一页</a>

<a href="?pageNum=<?php echo $endPage?>" rel="external nofollow" rel="external nofollow" >尾页</a>

代码如下:

<!DOCTYPE html>

<html>

<head>

 <meta charset="UTF-8">

 <title>分页</title>

 <?php

 

//分页的函数

function news($pageNum = 1, $pageSize = 3)

{

 $array = array();

 $coon = mysqli_connect("localhost", "root");

 mysqli_select_db($coon, "jereh");

 mysqli_set_charset($coon, "utf8");

 // limit为约束显示多少条信息,后面有两个参数,第一个为从第几个开始,第二个为长度

 $rs = "select * from n_content limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;

 $r = mysqli_query($coon, $rs);

 while ($obj = mysqli_fetch_object($r)) {

  $array[] = $obj;

 }

 mysqli_close($coon,"jereh");

 return $array;

}

 

//显示总页数的函数

function allNews()

{

 $coon = mysqli_connect("localhost", "root");

 mysqli_select_db($coon, "jereh");

 mysqli_set_charset($coon, "utf8");

 $rs = "select count(*) num from n_content"; //可以显示出总页数

 $r = mysqli_query($coon, $rs);

 $obj = mysqli_fetch_object($r);

 mysqli_close($coon,"jereh");

 return $obj->num;

}

 

 @$allNum = allNews();

 @$pageSize = 3; //约定没页显示几条信息

 @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"];

 @$endPage = ceil($allNum/$pageSize); //总页数

 @$array = news($pageNum,$pageSize);

 ?>

</head>

<body>

<table border="1" style="text-align: center" cellpadding="0">

 <tr>

  <td>编号</td>

  <td>新闻标题</td>

  <td>来源</td>

  <td>点击率</td>

  <td>发布日期</td>

 </tr>

 <?php

 foreach($array as $key=>$values){

  echo "<tr>";

  echo "<td>{$values->id}</td>";

  echo "<td>{$values->title}</td>";

  echo "<td>{$values->src}</td>";

  echo "<td>{$values->indexs}</td>";

  echo "<td>{$values->times}</td>";

  echo "</tr>";

 }

 ?>

</table>

<div>

 <a href="?pageNum=1" rel="external nofollow" rel="external nofollow" >首页</a>

 <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>" rel="external nofollow" rel="external nofollow" >上一页</a>

 <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>" rel="external nofollow" rel="external nofollow" >下一页</a>

 <a href="?pageNum=<?php echo $endPage?>" rel="external nofollow" rel="external nofollow" >尾页</a>

 

</div>

 

</body>

</html>
      

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/5298.html