慕前端6476978
2019-04-25 10:29
麻烦老师提供下课件源码下载
<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>
跟敲,数据库表名自行修改,如遇到问题请指正
<html><head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" ></head><style> body{ font-size:12px; FONT-FAMILY:verdana; width:100%; } div.page{ text-align:certer; } div.content{ height:300px; } div.page a{ border:#aaaaddd 1px solid; text-decoration:none; padding:2px 5px 2px 5px; margin:2px; } div.page span.current { border:#000099 1px solid; background-color:#000099; padding:4px 6px 4px 6px; margin:2px; color:#fff; font-weight:bold; } div.page span.disable{ border:#eee 1px solid; padding:2px 5px 2px 5px; margin:2px; color:#ddd; } div.page form{ display:inline; }</style><body><?php //传入页码 $page = $_GET['p']; //根据页码取出数据:php->mysql处理 $host = "localhost"; $username = "root"; $password = "root"; $db = "名字" $pageSize = 10; $showPage = 5; //链接数据库 $conn=mysql_connect($host,$username,$password); if(!$conn){ echo "数据库连接失败"; exit; } //选择所要操作的数据库 mysql_select_db($db); //设置数据库编码格式 mysql_query("SET NAMES UTF8"); //编写sql获取分页(拼接用.)数据SELECT * FROM 表明 LIMIT 起始位置,显示条数 $sql = "SELECT * FROM page LIMIT" .($page-1)*$pageSize .", {$pageSize}"; //把sql语句传送数据库 $result = mysql_query($sql); //处理数据源$result echo "<div class='content'>"; echo "<table border=1 cellspace=0 width=40% align='center'>"; echo "<tr><td>ID</td><td>NAME</td></tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "</tr>"; } echo "</table></div>"; //释放结果,关闭链接 mysql_free_result($result); //获取数据总数 $total_sql = "SELECT COUNT(*) FROM 表名"; $total_result = mysql_fetch_array(mysql_query($total_sql)); $total = $total_result[0]; //计算页数 $total_pages = ceil($total/$pageSize); mysql_close($conn); //显示数据+分页条 $page_banner = "<div class='page'>"; //计算偏移量 $pageoffset = ($showPage-1)/2; if($page > 1){ $page_banner .= "<a href='" .$_SERVER['PHP_SELF']. "?p=1'><<首页</a>"; $page_banner .= "<a href='" .$_SERVER['PHP_SELF']. "?p=" .($page-1). "'><上一页</a>"; }else{ $page_banner .= "<span class='disable'><<首页</a></span>"; $page_banner .= "<span class='disable'><上一页</a></span>"; } //初始化数据 $start = 1; $end = $total_pages; if($total_pages > $showPage){ if($page > $pageoffset + 1){ $page_banner .= "..."; } if($page > $pageoffset){ $start = $page - $pageoffset; $end = $total_pages > $page+$pageoffset ? $page+$pageoffset : $total_pages; }else{ $start = 1; $end = $total_pages > $showPage ? $showPage : $total_pages; } if($page + $pageoffset > $total_pages){ $start = $start - ($page + $pageoffset - $end); } } for($i = $start;$i<=$end;$i++){ if($page == $i){ $page_banner .= "<span class='current'>{$i}</span>"; }else{ $page_banner .= "<a href='" .$_SERVER['PHP_SELF']. "?p=" .$i. "'>{$i}</a>"; } } //尾部省略 if($total_pages > $showPage && $total_pages > $page + $pageoffset){ $page_banner .="..."; } if($page < $total_pages){ $page_banner .= "<a href='" .$_SERVER['PHP_SELF']. "?p=" .($page+1). "'>下一页></a>"; $page_banner .= "<a href='" .$_SERVER['PHP_SELF']. "?p=".($total_pages)"'>尾页>></a>"; }else{ $page_banner .= "<span class='disable'>下一页></a></span>"; $page_banner .= "<span class='disable'>尾页>></a></span>"; } $page_banner .= "共{$total_pages}页,"; $page_banner .= "<form action='fenye.php' method='get'>"; $page_banner .= "到第<input type='text' size='2' name='p'>页"; $page_banner .= "<imput type='submit' value='确定'"; $page_banner .= "</form></div>"; echo $page_banner;?></body></html>
PHP+MySQL分页原理实现
41180 学习 · 175 问题
相似问题