課程
/后端開發(fā)
/PHP
/PHP+MySQL分頁原理實現(xiàn)
沒有課程源碼,請問怎么練習,可以提供下嗎,謝謝
2015-09-15
源自:PHP+MySQL分頁原理實現(xiàn) 2-1
正在回答
我寫的,你可以參考一下
<!DOCTYPE html >
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<style >
body{
font-size:12px;
FONT-FAMILY:Microsoft YaHei;
width:100%;
}
div.page{
text-align:center;
div.content{
height:300px;
div.page a{
border:#aaaadd 1px solid;
text-decoration:none;
padding:2px 5px;
margin:3px;
div.page span.current{
border:#000099 1px solid;
background-color:#000099;
padding:2px 6px;
color:#fff;
font-weight:bold;
div.page span.disable{
border:#eee 1px solid;
color:#ddd;
div.page form{
display:inline;
</style>
<body>
<?php
//1傳入頁碼
$page = $_GET['p'];
$pagesize = 10;
//2根據(jù)頁碼取出數(shù)據(jù)
$host= "localhost";
$username = "root";
$password = "";
$db = "test";
$showpage = 5;
$conn = mysql_connect($host,$username,$password);
if(!$conn){
? ? echo "fail";
? ? exit;
mysql_select_db($db);
mysql_query("set names utf8");
$sql = "SELECT * FROM page LIMIT ".($page-1)*$pagesize .",$pagesize";
$result = mysql_query($sql);
echo "<div class='content'>";
echo "<table border=1 cellspacing=0 width=30% 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>";
echo "</div>";
mysql_free_result($result);
$total_sql = "select count(*) from page";
$total_result = mysql_fetch_array(mysql_query($total_sql));
$total = $total_result[0];
$total_pages = ceil($total/$pagesize);
mysql_close($conn);
//3顯示數(shù)據(jù)+分頁條
$page_banner = "<div class='page'>";
$pageoffset = ($showpage-1)/2;
$start = 1;
$end = $total_pages;
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'>首頁</span>";
? ? $page_banner.="<span class='disable'><上一頁</span>";
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>";
? ? $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>";
? ? $page_banner.="<span class='disable'>下一頁></span>";
? ? $page_banner.="<span class='disable'>尾頁</span>";
? }
$page_banner.="共{$total_pages}頁";
$page_banner.="<form action = 'index.php' method='get'>";
$page_banner.="到第<input type='text' size='2' name='p' value='$page'>頁";
$page_banner.="<input type='submit' value='確定'>";
$page_banner.="</form></div>";
echo $page_banner;
?>
</body>
</html>
看懂原理,自己開始寫。
舉報
引導大家完成一個PHP+MySQL分頁功能,由簡入繁的剖析原理
2 回答誰有代碼啊,求源代碼
2 回答麻煩老師提供下課件源碼下載
1 回答求源碼 最好有注釋
3 回答老師講的好棒有源代碼嗎
1 回答源代碼分享
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2015-09-22
我寫的,你可以參考一下
<!DOCTYPE html >
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<style >
body{
font-size:12px;
FONT-FAMILY:Microsoft YaHei;
width:100%;
}
div.page{
text-align:center;
}
div.content{
height:300px;
}
div.page a{
border:#aaaadd 1px solid;
text-decoration:none;
padding:2px 5px;
margin:3px;
}
div.page span.current{
border:#000099 1px solid;
background-color:#000099;
padding:2px 6px;
margin:3px;
color:#fff;
font-weight:bold;
}
div.page span.disable{
border:#eee 1px solid;
padding:2px 5px;
margin:3px;
color:#ddd;
}
div.page form{
display:inline;
}
</style>
<body>
<?php
//1傳入頁碼
$page = $_GET['p'];
$pagesize = 10;
//2根據(jù)頁碼取出數(shù)據(jù)
$host= "localhost";
$username = "root";
$password = "";
$db = "test";
$showpage = 5;
$conn = mysql_connect($host,$username,$password);
if(!$conn){
? ? echo "fail";
? ? exit;
}
mysql_select_db($db);
mysql_query("set names utf8");
$sql = "SELECT * FROM page LIMIT ".($page-1)*$pagesize .",$pagesize";
$result = mysql_query($sql);
echo "<div class='content'>";
echo "<table border=1 cellspacing=0 width=30% 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>";
echo "</div>";
mysql_free_result($result);
$total_sql = "select count(*) from page";
$total_result = mysql_fetch_array(mysql_query($total_sql));
$total = $total_result[0];
$total_pages = ceil($total/$pagesize);
mysql_close($conn);
//3顯示數(shù)據(jù)+分頁條
$page_banner = "<div class='page'>";
$pageoffset = ($showpage-1)/2;
$start = 1;
$end = $total_pages;
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'>首頁</span>";
? ? $page_banner.="<span class='disable'><上一頁</span>";
}
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'>下一頁></span>";
? ? $page_banner.="<span class='disable'>尾頁</span>";
? }
$page_banner.="共{$total_pages}頁";
$page_banner.="<form action = 'index.php' method='get'>";
$page_banner.="到第<input type='text' size='2' name='p' value='$page'>頁";
$page_banner.="<input type='submit' value='確定'>";
$page_banner.="</form></div>";
echo $page_banner;
?>
</body>
</html>
2015-09-15
看懂原理,自己開始寫。