PHP 常用代码
一、分页
封装分页方法
/**
* @param int $total 总页数
* @param int $pageSize 每页显示条数
* @param int $page 当前页
* @param int $shift 当前页两边的偏移数
* @param string $other
**/
function page($total, $pageSize, $page, $shift, $other = '') {
if($other){
$other = '&'.$other;
}
$url = $_SERVER["REQUEST_URI"];
$arr = parse_url($url);
$path = $arr['path'];
$pageNumber = ceil($total / $pageSize);
$min = $page - $shift;
$max = $page + $shift;
if ($min < 1) {
$max = $max + (1 - $min);
$min = 1;
}
if ($max > $pageNumber) {
$min = $min - ($max - $pageNumber);
$max = $pageNumber;
}
$page_prev = $page - 1; //上一页
$page_next = $page + 1; //下一页
//$str .= '<div class="pager">';
if ($page != 1) {
$str .= '<a href="' . $path . '?page=' . $page_prev . $other . '"><</a>';
$str .= '<a href="' . $path . '?page=1' . $other . '">1...</a>';
}
for ($i = $min; $i <= $max; $i++) {
if ($page == $i) {
$str .= '<span class="PageEllipsis">' . $i . '</span>';
} else {
$str .= "<a href=\"" . $path . "?page=" . $i . $other . "\" title=\"第" . $i . "页\">" . $i . "</a>";
}
}
if ($page != $pageNumber) {
$str .= '<a href="' . $path . '?page=' . $pageNumber . $other . '">...'.$pageNumber.'</a>';
$str .= '<a href="' . $path . '?page=' . $page_next . $other . '">></a>';
}
//$str .= '</div>';
return $str = ($pageNumber < 2) ? '共' . $total . '条记录' : $str;
}应用分页方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>分页</title>
<style>
a{text-decoration:none;color:inherit;}
.pager{
width:600px;
margin:0 auto;
display:flex;
justify-content:center;
}
.pager a,
.pager span {
margin:0 5px;
font-size:14px;
padding:5px 12px;
border: 1px solid #bbb;
color: #999;
}
.pager span.PageEllipsis {
border: 1px solid coral;
color: coral;
}
</style>
</head>
<body>
<div class="pager">
<?php
error_reporting(E_ALL & ~ E_NOTICE);
$page = (!$_GET['page']) ? 1 : intval($_GET['page']);
$pageSize = 20;
$total = 306;
$shift = 3;
echo page($total, $pageSize, $page, $shift, $other);
?>
</div>
</body>
</html>
<?php
function page($total, $pageSize, $page, $shift, $other = '') {
if($other){
$other = '&'.$other;
}
$url = $_SERVER["REQUEST_URI"]; // /mycode/page/01.php?page=8
$arr = parse_url($url);
$path = $arr['path']; // /mycode/page/01.php
$pageNumber = ceil($total / $pageSize); // 共有多少页
$min = $page - $shift; // 当前page减5
$max = $page + $shift; // 当前page加5
if ($min < 1) {
$max = $max + (1 - $min); // 公式得出 $this -> page_i为负数时页面总数是11
$min = 1;
}
if ($max > $pageNumber) {
$min = $min - ($max - $pageNumber);
$max = $pageNumber;
}
$page_prev = $page - 1; //上一页
$page_next = $page + 1; //下一页
//$str .= '<div class="pager">';
if ($page != 1) {
$str .= '<a href="' . $path . '?page=' . $page_prev . $other . '"><</a>';
$str .= '<a href="' . $path . '?page=1' . $other . '">1...</a>';
}
for ($i = $min; $i <= $max; $i++) {
if ($page == $i) {
$str .= '<span class="PageEllipsis">' . $i . '</span>';
} else {
$str .= "<a href=\"" . $path . "?page=" . $i . $other . "\" title=\"第" . $i . "页\">" . $i . "</a>";
}
}
if ($page != $pageNumber) {
$str .= '<a href="' . $path . '?page=' . $pageNumber . $other . '">...'.$pageNumber.'</a>';
$str .= '<a href="' . $path . '?page=' . $page_next . $other . '">></a>';
}
//$str .= '</div>';
return $str = ($pageNumber < 2) ? '共' . $total . '条记录' : $str;
}
?>记录一些代码
获取数据总数
$pagesize=10; //设置每一页显示的记录数
$conn=mysql_connect("localhost","root",""); //连接数据库
$rs=mysql_query("select count(*) from tb_product",$conn); //取得记录总数$rs
$myrow = mysql_fetch_array($rs);
$numrows=$myrow[0];
//计算总页数
$pages=intval($numrows/$pagesize);
// 判断页数
if (isset($_GET[@#page@#])){
$page=intval($_GET[@#page@#]);
}else{
$page=1; // 否则,设置为第一页
}获取数据总数
$sql = "select count(*) as amount from table"; $result = mysql_query($sql); $row = mysql_fetch_row($result); $amount = $row['amount'];
Leave a comment
0 Comments.
