Heim >Backend-Entwicklung >PHP-Tutorial >Die einfachste PHP-Paginierung

Die einfachste PHP-Paginierung

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:16:101140Durchsuche
<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?>//显示页码
Die Quelle kann nicht überprüft werden...

Das Obige stellt die einfachste PHP-Seite vor, einschließlich des Inhalts. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn