>백엔드 개발 >PHP 튜토리얼 >가장 간단한 PHP 페이지 매김

가장 간단한 PHP 페이지 매김

WBOY
WBOY원래의
2016-07-29 09:16:101108검색
<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">	</span>//查询的表格
	$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=\"$targetpage?page=$prev\">前一页</a>";
		else
			$pagination.= "<span class=\"disabled\">前一页</span>";
		if ($lastpage < 7 + ($adjacents * 2))
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))
		{
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
			}
		}
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">下一页</a>";
		else
			$pagination.= "<span class=\"disabled\">下一页</span>";
		$pagination.= "</div>\n";		
	}
?>

//此处放主表格

<?=$pagination?>//显示页码
출처를 확인할 수 없습니다...

위 내용은 관련 내용을 포함하여 가장 간단한 PHP 페이징을 소개한 내용이 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.