首页 >后端开发 >php教程 >最简单的php分页

最简单的php分页

WBOY
WBOY原创
2016-07-29 09:16:101106浏览
<style type="text/css">
div.pagination {
	padding: 3px;
	margin: 4px;
}
div.pagination a {
	padding: 2px 5px 2px 5px;
	margin: 4px;
	border: 1px solid #666;	
	text-decoration: none; /* no underline */
	color: #666;
}
div.pagination a:hover, div.pagination a:active {
	border: 1px solid #333;
	color: #000;
}
div.pagination span.current {
	padding: 2px 5px 2px 5px;
	margin: 4px;
	border: 1px solid #333;	
	font-weight: bold;
	background-color: #666;
	color: #FFF;
}
div.pagination span.disabled {
	padding: 2px 5px 2px 5px;
	margin: 4px;
	border: 1px solid #EEE;
	color: #DDD;
}
</style>
<?php $c 
	$tbl_name="main";<span style="white-space:pre">	//查询的表格
	$limit=5;<span style="white-space:pre">		</span>//每页条数
	$adjacents = 3;  	//当前页的左n页,右n页
	$query = "SELECT COUNT(*) FROM $tbl_name";
	$total_pages = mysqli_fetch_array(mysqli_query($conn,$query));
	$total_pages = $total_pages[0];
	$targetpage = "main2.php";
	@$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit;
	else
		$start = 0;
	$sql = "select * from `".$tbl_name."` limit ".$start.",".$limit;//主查询语句
	$result1 = mysqli_query($conn,$sql);
	if ($page == 0) $page = 1;
	$prev = $page - 1;
	$next = $page + 1;
	$lastpage = ceil($total_pages/$limit);
	$lpm1 = $lastpage - 1;
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class='\"pagination\"' align='\"center\"'>";
		if ($page > 1) 
			$pagination.= "<a href="%5C%22%24targetpage?page=%24prev%5C%22">前一页</a>";
		else
			$pagination.= "<span class='\"disabled\"'>前一页</span>";
		if ($lastpage $counter";
				else
					$pagination.= "<a href="%5C%22%24targetpage?page=%24counter%5C%22">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))
		{
			if($page $counter";
					else
						$pagination.= "<a href="%5C%22%24targetpage?page=%24counter%5C%22">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href="%5C%22%24targetpage?page=%24lpm1%5C%22">$lpm1</a>";
				$pagination.= "<a href="%5C%22%24targetpage?page=%24lastpage%5C%22">$lastpage</a>";		
			}
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href="%5C%22%24targetpage?page=1%5C%22">1</a>";
				$pagination.= "<a href="%5C%22%24targetpage?page=2%5C%22">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter $counter";
					else
						$pagination.= "<a href="%5C%22%24targetpage?page=%24counter%5C%22">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href="%5C%22%24targetpage?page=%24lpm1%5C%22">$lpm1</a>";
				$pagination.= "<a href="%5C%22%24targetpage?page=%24lastpage%5C%22">$lastpage</a>";		
			}
			else
			{
				$pagination.= "<a href="%5C%22%24targetpage?page=1%5C%22">1</a>";
				$pagination.= "<a href="%5C%22%24targetpage?page=2%5C%22">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter $counter";
					else
						$pagination.= "<a href="%5C%22%24targetpage?page=%24counter%5C%22">$counter</a>";					
				}
			}
		}
		if ($page 下一页";
		else
			$pagination.= "<span class='\"disabled\"'>下一页</span>";
		$pagination.= "</div>\n";		
	}
?>

//此处放主表格

=$pagination?>//显示页码
出处已不可考……

以上就介绍了最简单的php分页,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn