最简单的php分页

WBOY
发布: 2016-07-29 09:16:10
原创
1077 人浏览过
<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教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!