PHP+Mysql development and paging improvement
Paging improvement
<html> <head> <meta http-equiv="CONTENT-TYPE" content="text/html;"> </head> <title>分頁(yè)</title> <style> div.page{ text-align: center; } div.page a{ border: #aa0027 solid 1px; text-decoration: none; padding: 2px 5px 2px 5px; margin: 2px; } div.page span.current{ border: #000099 1px solid;background-color: #992b6c;padding: 4px 6px 4px 6px;margin: 2px;color: #fff; font-weight: bold; } div.page form{ display: inline; } div.content{ height: 200px; } </style> <body> <?php error_reporting(E_ALL ^ E_DEPRECATED); ?> <?php /** 1.傳入頁(yè)面 **/ $page= isset($_GET['p']) ? trim($_GET['p']) : 1; /** 2.根據(jù)頁(yè)面取出數(shù)據(jù):php->mysql **/ $host = "localhost"; $username = 'root'; $password = '123456789'; $db = 'bbs2'; $PageSize=5; $ShowPage=3; //連接數(shù)據(jù)庫(kù) $conn = mysql_connect($host, $username, $password); if(!$conn){ echo "數(shù)據(jù)庫(kù)連接失敗"; exit; } //選擇所要操作的數(shù)據(jù)庫(kù) mysql_select_db($db); //設(shè)置數(shù)據(jù)庫(kù)編碼格式 mysql_query('SET NAMES UTF8'); //編寫sql獲取分頁(yè)數(shù)據(jù):SELECT * FROM 表名 LIMIT 起始位置 , 顯示條數(shù) $sql = "SELECT*FROM test LIMIT ".($page-1)*$PageSize .",$PageSize"; if(!$sql){ echo "取出不成功"; }; //把sql語(yǔ)句傳送到數(shù)據(jù)庫(kù) $result = mysql_query($sql); //處理我們的數(shù)據(jù) echo "<div class='content'>"; echo "<table border=1 cellspacing=0 width=15% align='center'>"; echo "<tr><td>ID</td><td>名字</td><td>性別</td></tr>"; while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['name']}</td>"; echo "<td>{$row['sex']}</td>"; echo "<tr>"; } echo "</table>"; echo "</div>"; //釋放結(jié)果 mysql_free_result($result); //獲取數(shù)據(jù)總數(shù) $to_sql="SELECT COUNT(*)FROM test"; $to_result=mysql_fetch_array(mysql_query($to_sql)); $to=$to_result[0]; //計(jì)算頁(yè)數(shù) $to_pages=ceil($to/$PageSize); mysql_close($conn); /** 3.顯示數(shù)據(jù)+分頁(yè)條 **/ $page_banner="<div class='page'>"; //計(jì)算偏移量 $pageffset=($ShowPage-1)/2; if($page>1){ $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=1'>首頁(yè)</a>"; $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'><上一頁(yè)</a>"; } //初始化數(shù)據(jù) $start=1; $end=$to_pages; if ($to_pages>$ShowPage){ if($page>$pageffset+1){ $page_banner.="..."; } if ($page>$pageffset){ $start=$page-$pageffset; $end=$to_pages>$page+$pageffset?$page+$pageffset:$to_pages; }else{ $start=1; $end=$to_pages>$ShowPage?$ShowPage:$to_pages; } if ($page+$pageffset>$to_pages){ $start=$start-($page+$pageffset-$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 ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){ $page_banner.="..."; } if ($page<$to_pages){ $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一頁(yè)></a>"; $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($to_pages)."'>尾頁(yè)</a>"; } $page_banner.="共{$to_pages}頁(yè)"; $page_banner.="<form action='mupage.php' method='get'>"; $page_banner.="到第<input type='text'size='2'name='p'>頁(yè)"; $page_banner.="<input type='submit'value='確定'>"; $page_banner.="</form></div>"; echo $page_banner; ?> </body> </html>
$page=($_GET['P']) was changed to $page= isset($_GET[' p']) ? trim($_GET['p']): 1; Because you need to set the default page number when opening for the first time
The basic paging bar has been completed, and The page number style has been modified. You can also design the paging bar style according to your own style, conduct more detailed modification tests, or even encapsulate it and use it as your own class.
This article is only used as a starting point to enable readers to deepen their understanding of the operations between php and mysql, and at the same time, it will make future learning more convenient.