当前位置:Gxlcms > PHP教程 > 最简单的php分页

最简单的php分页

时间:2021-07-01 10:21:17 帮助过:34人阅读

	//查询的表格
	$limit=5;		//每页条数
	$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 .= "";
		if ($page > 1) 
			$pagination.= "前一页";
		else
			$pagination.= "前一页";
		if ($lastpage < 7 + ($adjacents * 2))
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "$counter";
				else
					$pagination.= "$counter";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))
		{
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "$counter";
					else
						$pagination.= "$counter";					
				}
				$pagination.= "...";
				$pagination.= "$lpm1";
				$pagination.= "$lastpage";		
			}
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "1";
				$pagination.= "2";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "$counter";
					else
						$pagination.= "$counter";					
				}
				$pagination.= "...";
				$pagination.= "$lpm1";
				$pagination.= "$lastpage";		
			}
			else
			{
				$pagination.= "1";
				$pagination.= "2";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "$counter";
					else
						$pagination.= "$counter";					
				}
			}
		}
		if ($page < $counter - 1) 
			$pagination.= "下一页";
		else
			$pagination.= "下一页";
		$pagination.= "\n";		
	}
?>

//此处放主表格

//显示页码
出处已不可考……

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

人气教程排行