蛮好的。
用align=center
shide
你把更新方法加一个定时器不就得了?
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> </head> <body> <?php /** * Created by PhpStorm. * User: 123 * Date: 2020/2/14 * Time: 12:16 */ /*1.传入页码*/ $page = $_GET['p']; /*2.根据页码取出数据, php -> mysql*/ $host = "localhost"; $username = "root"; $password = "123456"; $db = "page"; $pageSize = 3; $showPage = 5; //连接数据库 $conn = mysqli_connect($host, $username, $password, $db); if (!$conn) { var_dump("连接失败"); } //设置数据库的编码格式,防止乱码 mysqli_query($conn, "SET NAMES UTF8"); //编写sql获取分页数据 SELECT * FROM 表名 LIMIT 起始位置,显示条数 $sql = "SELECT * FROM test LIMIT " . ($page - 1) * $pageSize . ",{$pageSize}"; //把sql语句传送数据中 $result = mysqli_query($conn, $sql); //处理数据 echo "<table border='1' cellspacing='0' width='40%' class='margin: 0 auto'>"; echo "<tr><td>ID</td><td>name</td></tr>"; while ($row = mysqli_fetch_assoc($result)) { echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>"; } echo "</table>"; //释放结果,关闭连接 mysqli_free_result($result); //获取数据总数 $total_sql = "SELECT COUNT(*) FROM test"; $total_result = mysqli_fetch_assoc(mysqli_query($conn, $total_sql)); $total = $total_result['COUNT(*)']; $total_page = ceil($total / $pageSize); mysqli_close($conn); //3.显示数据 + 分页条 $page_banner = ''; if ($page > 1) { $page_banner .= "<a href='" . "{$_SERVER['PHP_SELF']}?p=1" . "'>首页</a>"; $page_banner .= "<a href='" . "{$_SERVER['PHP_SELF']}?p=" . ($page - 1) . "'>上一页</a>"; } //初始化数据 $start = 1; //开始页面 $end = $total_page; //结束页面 $pageOffset = ($showPage - 1) / 2; if ($total_page > $showPage) { //如果总页面大于显示的页面 if ($page > $pageOffset + 1) { //如果当前页面大于偏移量 $page_banner .= "..."; } if ($page > $pageOffset) { //当前页大于偏移量 $start = $page - $pageOffset; $end = $total_page > $page + $pageOffset ? $page + $pageOffset : $total_page; } else { $start = 1; $end = $total_page > $showPage ? $showPage : $total_page; } if ($page + $pageOffset > $total_page) { $start = $start - ($page + $pageOffset - $end); } } for ($i = $start; $i <= $end; $i++) { $page_banner .= "<a href='" . "{$_SERVER['PHP_SELF']}?p={$i}" . "'>{$i}</a>"; } //尾部省略 if ($total_page > $showPage && $total_page > $page + $pageOffset){ $page_banner .= "..."; } if ($page < $total_page) { $page_banner .= "<a href='" . "{$_SERVER['PHP_SELF']}?p=" . ($page + 1) . "'>下一页</a>"; $page_banner .= "<a href='" . "{$_SERVER['PHP_SELF']}?p={$total_page}" . "'>尾页</a>"; } $page_banner .= "总页数{$total_page},"; $page_banner .= "<form action='demo1.php' method='get'>"; $page_banner .= "到第<input type='text' size='2' name='p'>页"; $page_banner .= "<input type='submit' value='确定'>"; $page_banner .= "</form>"; echo $page_banner; ?> </body> </html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?php
include "conn.php";
$page = isset($_GET['p']) ? $_GET['p'] : '';
$page_size = 10;
$page_from = ($page-1) * $page_size;
$sql = "SELECT * FROM message LIMIT $page_from, $page_size";
$res = $conn->query($sql);
?>
<table class="one" border="1">
<tr>
<td>id</td><td>用户</td><td>内容</td><td>时间</td><td>操作</td>
</tr>
<?php while($row = $res->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['mes']; ?></td>
<td><?php echo $row['mestime']; ?></td>
<td><a href="#">修改</a> | <a href="#">删除</a></td>
</tr>
<?php } ?>
</table>
<br /><br />
<div class="page">
<?php
$page_banner = "";
$sql = "SELECT * FROM message";
$res = $conn->query($sql);
$total_records = $res->num_rows;
$total_pages = ceil($total_records / $page_size);
$start = $page-5;
$end = $page+4;
if($page < 6){
$start = 1;
$end = 10;
}
if($page > $total_pages-4){
$start = $total_pages-9;
$end = $total_pages;
}
if($total_pages < 10){
$start = 1;
$end = $total_pages;
}
if($page > 1){
$page_banner = "<a href='page.php?p=1'>首页</a>";
$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";
}
for($i=$start; $i<=$end; $i++){
if($page == $i){
$page_banner .= "<span class='current'>{$i}</span>";
}else{
$page_banner .= "<a href='page.php?p=".$i."'>".$i."</a>";
}
}
if($page < $total_pages){
$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";
$page_banner .= "<a href='page.php?p=$total_pages'>尾页</a>";
}
echo $page_banner;
echo "共".$total_pages."页";
?>
</div>
</body>
</html>
贴代码
会不会是数据库密码错误了
你可以通过使用var_dump($total_result)查看数组内容,通过看对应的键就可以知道该键对应的值,这里的0键对应的应该是id
我跟你出错是一样的啊
这个错误
执行完$total_result = mysql_fetch_array(mydqli_query($total_sql));
后的得到的是一个数组
array{
[0] =>数据数
[counte(*)] =>数据数
}
所以你只需要取其中的一个就行了的
$sql="SELECT * FROM empmanage LIMIT ".(($page-1)*10).",10"; 像我这样写是没问题的
我个人觉得,这个p可以用,但是会报错。为什么这么说呢。因为:
在地址框里文件名后,输入?p=1 后回车,是有显示的;
用$pageSize=10定义好单页展示条数,用sql语句分页:$sql="SELECT * FROM article LIMIT " .(($page-1)*$pageSize) .",".($pageSize); 页面分好之后,地址框输入 ?p=数值,回车会跳到指定页面;
这里我出现一个问题,就是浏览器打开php文件,地址框不带p的值,会报错,报错的地方,就是接收p的语句,我的是:$page=$_GET['p']; 。但是,跳转页面之后,就不再报错了。我怎么知道的呢,因为我先把分页条做了出来,不管报错,点击页码跳转到任何一个页面,只要地址框出现 ?p=数值,就不再报错了。
所以我说,想直接获取页码,好像不太行,但是之后的操作可以进行。
等号前面一个.
if($page<1 || $page>$total_page || !preg_match("/^\d*$/",$page)){ echo "页码不正确"; die(); }
可以用int把浮点型自己强制转换成整形,不转化的话应该也可以但是考虑到浮点型有精度丢失还是转一下比较好
如果是0改为1,大于总页数让page等于总页数
图片分页原理是一样的
我刚刚打完,你要我代码吗
$sql="SELECT * FROM test LIMIT ".(($page-1)*10).",10"; 加个括号
ID,用户名,密码,这是最基本的
class Page{ private $total; // 总共有多少条记录 private $pagenum; // 分成多少页 private $pagesize; // 每页多少条记录 private $current; // 当前所在的页数 private $url; // url private $first; // 首页 private $last; // 末页 private $prev; // 上一页 private $next; // 下一页 /** * 构造函数 * @access public * @param $total number 总的记录数 * @param $pagesize number 每页的记录数 * @param $current number 当前所在页 * @param $script string 当前请求的脚本名称,默认为空 * @param $params array url所携带的参数,默认为空 */ public function __construct($total,$pagesize,$current,$script = '',$params = array()){ $this->total = $total; $this->pagesize = $pagesize; $this->pagenum = $this->getNum(); $this->current = $current; //设置url $p = array(); foreach ($params as $k => $v) { $p[] = "$k=$v"; } $this->url = $script . '?' . implode('&', $p) . '&page='; $this->first = $this->getFirst(); $this->last = $this->getLast(); $this->prev = $this->getPrev(); $this->next = $this->getNext(); } private function getNum(){ return ceil($this->total / $this->pagesize); } private function getFirst(){ if ($this->current == 1) { return '[首页]'; } else { return "<a href='{$this->url}1'>[首页]<a/>"; } } private function getLast(){ if ($this->current == $this->pagenum) { return '末页'; } else { return "<a href='{$this->url}{$this->pagenum}'>[末页]</a>"; } } private function getPrev(){ if ($this->current == 1) { return '[上一页]'; } else { return "<a href='{$this->url}".($this->current - 1)."'>[上一页]</a>"; } } private function getNext(){ if ($this->current == $this->pagenum ){ return '[下一页]'; } else { return "<a href='{$this->url}".($this->current+1)."'>[下一页]</a>"; } } /** * getPage方法,得到分页信息 * @access public * @return string 分页信息字符串 */ public function showPage(){ if ($this->pagenum > 1){ return "共有 {$this->total} 条记录,每页显示 {$this->pagesize} 条记录, 当前为 {$this->current}/{$this->pagenum} {$this->first} {$this->prev} {$this->next} {$this->last}"; }else{ return "共有 {$this->total} 条记录"; } } } //使用:配合mysql操作类一起使用 /* $total = $db->total(); $pagesize = 3; $current = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1; $offset = ($current - 1) * $pagesize; $rows = $db->getAll("SELECT * FROM category limit $offset,$pagesize" ); $page = new page($total,$pagesize,$current,'test.php',array('goods_id'=>2)); $str = "<table width='400' border='1'>"; $str .= "<tr><th>编号</th><th>名称</th><th>父编号</th></tr>"; foreach ($rows as $v) { $str .= '<tr>'; $str .= "<td>{$v['cat_id']}</td>"; $str .= "<td>{$v['cat_name']}</td>"; $str .= "<td>{$v['parent_id']}</td>"; $str .= '</tr>'; } $str .= "</table>"; echo $str; echo $page->showPage(); */
希望能帮助到你。
<c:if ${PageModel.pageIndex}==1> <a>【首页】</a> <a>【第一页】</a> </c:if>
判断当前页等于1的时候显示空链接的首页和第一页