Newbie try-php paging example code
Release: 2016-07-25 09:05:21
Original
812 people have browsed it
-
- /*
- test数据库,一个test表,唯一字段Id,作为测试使用
- @link http://bbs.it-home.org
- */
- $conn = mysql_connect("localhost","root","");
- $maxnum = 2; //每页显示记录条数
- mysql_select_db("test", $conn);
- $query1 = "SELECT COUNT(*) AS totalrows FROM test ";
- $result1 = mysql_query($query1, $conn) or die(mysql_error());
- $row1 = mysql_fetch_assoc($result1);
- $totalRows1 = $row1['totalrows']; //数据集数据总条数
- $totalpages = ceil($totalRows1/$maxnum);//计算可分页总数,ceil()为上舍函数
- if(!isset($_GET['page']) || !intval($_GET['page']) || $_GET['page'] > $totalpages) $page = 1; //对3种出错进行默认处理
- //在url参数page不存在时,page不为10进制数时,page大于可分页数时,默认为1
- else $page = $_GET['page'];
- $startnum = ($page - 1)*$maxnum; //从数据集第$startnum条开始取,注意数据集是从0开始的
- $query = "SELECT * FROM test LIMIT $startnum,$maxnum";//选择出符合要求的数据 从$startnum条数据开始,选出$maxnum行
- $result = mysql_query($query, $conn) or die(mysql_error());
- $row = mysql_fetch_assoc($result);
- ?>
-
-
-
- 分页示例
-
-
-
-
-
-
-
-
- mysql_free_result($result1);
- mysql_free_result($result);
- ?>
Copy code
|
Statement of this Website
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31