分页实现
时间:2021-07-01 10:21:17
帮助过:12人阅读
<无详细内容>
- 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);
|