Paging implementation
Release: 2016-07-25 09:09:07
Original
939 people have browsed it
- header('Content-type:text/html; charset=UTF-8');
- $conn = mysql_connect('localhost', 'root', '123456') or die('connect to database error!');
- mysql_select_db('xsphp') or die('select database error!');
- mysql_query('SET NAMES utf8');
- function showTable($name,$page=1)
- {
- $eachPages = 5;
- $url = $_SERVER['PHP_SELF'];
- $sql = "SELECT count(*) as total FROM {$name}";
- $result = mysql_query($sql);
- list($rowNums) = mysql_fetch_row($result);
- $pageNums = ceil($rowNums / $eachPages);
- $rowFrom = ($page > 1) ? (($page - 1) * $eachPages) : 0;
-
- $sql = "SELECT id,title FROM {$name} LIMIT {$rowFrom},{$eachPages}";
- $result = mysql_query($sql);
-
- echo '
';
- echo '
'.$name.'';
-
- //显示标题
- $cols = mysql_num_fields($result);
- echo '
';
- for ($i=0; $i<$cols; $i++) {
- echo '
'.mysql_field_name($result,$i).' | ';
- }
- echo '
';
- //显示记录
- while ($row = mysql_fetch_assoc($result)) {
- echo '
';
- echo '
'.$row['id'].' | ';
- echo '
'.$row['title'].' | ';
- echo '
';
- }
- //显示Page导航
- echo '
记录共有:'.mysql_num_rows($result) . ' | ';
- echo ($page == 1) ? '首页' : '首页';
- echo ' ';
- echo ($page == 1) ? '上一页' : '上一页';
- echo ' ';
- echo ($page == $pageNums) ? '下一页' : '下一页';
- echo ' ';
- echo ($page == $pageNums) ? '尾页' : '尾页';
- echo '
| ';
- }
-
- if (isset($_GET['page']) && $_GET['page'] > 0) {
- $page = $_GET['page'];
- } else {
- $page = 1;
- }
-
- showTable('category',$page);
复制代码
|
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