PHP+Mysql development and paging improvement

Paging improvement

   
分页 mysql **/ $host = "localhost"; $username = 'root'; $password = '123456789'; $db = 'bbs2'; $PageSize=5; $ShowPage=3; //连接数据库 $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 test LIMIT ".($page-1)*$PageSize .",$PageSize"; if(!$sql){ echo "取出不成功"; }; //把sql语句传送到数据库 $result = mysql_query($sql); //处理我们的数据 echo "
"; echo ""; echo ""; while($row = mysql_fetch_assoc($result)){ echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
ID名字性别
{$row['id']}{$row['name']}{$row['sex']}
"; echo "
"; //释放结果 mysql_free_result($result); //获取数据总数 $to_sql="SELECT COUNT(*)FROM test"; $to_result=mysql_fetch_array(mysql_query($to_sql)); $to=$to_result[0]; //计算页数 $to_pages=ceil($to/$PageSize); mysql_close($conn); /** 3.显示数据+分页条 **/ $page_banner="
"; //计算偏移量 $pageffset=($ShowPage-1)/2; if($page>1){ $page_banner.="首页"; $page_banner.="<上一页"; } //初始化数据 $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 .= "{$i}"; } else { $page_banner .= "{$i}"; } } //尾部省略 if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){ $page_banner.="..."; } if ($page<$to_pages){ $page_banner.="下一页>"; $page_banner.="尾页"; } $page_banner.="共{$to_pages}页"; $page_banner.="
"; $page_banner.="到第页"; $page_banner.=""; $page_banner.="
"; echo $page_banner; ?>

$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.

Continuing Learning
||
分页 mysql **/ $host = "localhost"; $username = 'root'; $password = '123456789'; $db = 'bbs2'; $PageSize=5; $ShowPage=3; //连接数据库 $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 test LIMIT ".($page-1)*$PageSize .",$PageSize"; if(!$sql){ echo "取出不成功"; }; //把sql语句传送到数据库 $result = mysql_query($sql); //处理我们的数据 echo "
"; echo ""; echo ""; while($row = mysql_fetch_assoc($result)){ echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
ID名字性别
{$row['id']}{$row['name']}{$row['sex']}
"; echo "
"; //释放结果 mysql_free_result($result); //获取数据总数 $to_sql="SELECT COUNT(*)FROM test"; $to_result=mysql_fetch_array(mysql_query($to_sql)); $to=$to_result[0]; //计算页数 $to_pages=ceil($to/$PageSize); mysql_close($conn); /** 3.显示数据+分页条 **/ $page_banner="
"; //计算偏移量 $pageffset=($ShowPage-1)/2; if($page>1){ $page_banner.="首页"; $page_banner.="<上一页"; } //初始化数据 $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 .= "{$i}"; } else { $page_banner .= "{$i}"; } } //尾部省略 if ($to_pages>$ShowPage&&$to_pages>$page+$pageffset){ $page_banner.="..."; } if ($page<$to_pages){ $page_banner.="下一页>"; $page_banner.="尾页"; } $page_banner.="共{$to_pages}页"; $page_banner.="
"; $page_banner.="到第页"; $page_banner.=""; $page_banner.="
"; echo $page_banner; ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!