龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

PHP简单分页函数

时间:2014-07-22 14:51来源: 作者: 点击:
分享到:
写了一个php简单的分页函数,数据库调用也写到里面了 ,用的人可以自行删减!
写了一个php简单的分页函数,数据库调用也写到里面了 ,用的人可以自行删减!
 function getask(){
 	$sql = "select * from cms_ask where ansower <> ' ' ";
	//这里要改成方法
	$q_sq = mysql_query($sql);
	$count = mysql_num_rows($q_sq);
	$page_size = 8;
	$page_current = isset($GLOBALS['page']) ? intval($GLOBALS['page']) : 1;
	$page_count = ceil($count / $page_size);
	$page_start = $page_current - 4;
	$page_end = $page_current + 4;
	if ($page_current < 5) {
		$page_start = 1;
		$page_end = 5;
	}
	if ($page_current > $page_count - 4) {
		$page_start = $page_count - 8;
		$page_end = $page_count;
	}
	if ($page_start < 1)
		$page_start = 1;
	if ($page_end > $page_count)
		$page_end = $page_count;
	$pagebar = "";
	
	
	$sql = "select * from cms_ask  where ansower <> ' ' order by id desc limit " . (($page_current - 1) * $page_size) . "," . $page_size;

	$row=$this -> user -> getall("$sql");
 	foreach($row as $v){
 		echo '<dl><dt>问:'.$v["question"].'</dt><dd>答:'.$v["ansower"].date("Y-m-d H:i:s",$v["postTime"]).'</dd></dl>';
 		
  	}
 	
 	$pagebar .= "<div class=\"page\">";

	
		$pagebar .= "<ol>";
		if ($page_current != 1) {
			 $pagebar .= '<li><a href="javascript:get_comment(1)" class="sx">FIRST</a></li>';
			}
		for ($i = $page_start; $i <= $page_end; $i++) {
			if ($i == $page_current) {
				$pagebar .= "<li><span class=\"sort\">" . $i . "</span></li>";
			} else {
				$pagebar .= "<li><a href='javascript:get_comment(" . $i . ")'>" . $i . "</a></li>";
			}
		}

		
		if ($page_current != $page_count) {
			$pagebar .= "<li><a href='javascript:get_comment(" . $page_count . ")' class='sx'>END</a></li>";
		}

		$pagebar .= "</ol>";

		$pagebar .= "   </div>";
	
		echo $pagebar;
 }
精彩图集

赞助商链接