<?php

/**
 * 个人用户中心分页
 * @param int $page_now 当前页
 * @param int $pages 总页数
 * @param int $numrows 总行数
 * @param int $show_num 分页显示的页数
 * @param string  $form 表单名
 * @return string  分页字符串
 * @author esf dev team 2014-02-26
 */
function helper_pagination($page_now,$pages,$numrows,$show_mum = 7,$form = "search_form")
{

	$form = 'document.'.$form;

	$ss = '';

	//如果页数小于1则返回空
	if( $pages <= 1 || !is_numeric($page_now) )
	{
		return '';
	} 
	else
	{
		$pre = $page_now == 1   ? $page_now : $page_now - 1;	//前一页
		$end = $page_now == $pages  ? $pages : $page_now + 1;	//下一页

		//首页、前一页
		if( $page_now == 1 )
		{
			$ss .= "<a class='p-prev'>首 页</a>";
			$ss .= "<a class='p-prev'>上一页</a>";
		}
		else
		{
			$first_page = 1;//第一页

			$ss .= "<a  class='p-prev' onclick=\"$form.page.value=".$first_page.";$form.submit();return false;\" style ='cursor:pointer'>首 页</a>";

			$ss .= "<a  class='p-prev' onclick=\"$form.page.value=".$pre.";$form.submit();return false;\" style ='cursor:pointer'>上一页</a>";
		}

		//前三页
		if( $page_now > 3 )
		{
			if( $pages - $page_now <= 3 )
			{
				$num = $page_now - ( $show_mum - ( $pages + 1 - $page_now ) ) ;
				$num = $num >= 1 ? $num : 1;
				for( $i = $num ; $i < $page_now ; $i ++)
				{
					$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a>";
				}
			}
			else
			{
				for($i = $page_now - 3 ; $i < $page_now ; $i ++)
				{
					$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a>";
				}
			}
		}
		else
		{
			$show_num   = $pages > $page_now ? $page_now : $pages;
			for($i = 1 ; $i < $show_num ; $i ++)
			{
				$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a>";
			}
		}

		//当前页面
		$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$page_now.";$form.submit();return false;\" class='on'>{$page_now}</a>";

		//后三页数
		if( $pages - $page_now > 3 )
		{

			//当前页数小于等于3时，强制显示到第7页
			for($i = $page_now + 1 ; $i <= $page_now + 3; $i++){
					$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$i.";$form.submit();return false;\">{$i}</a>";
			}
		}
		else
		{

			for($i = ($page_now + 1) ; $i <= $pages; $i ++)
			{
				$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$i.";$form.submit();return false;\" >{$i}</a>";
			}
		}

		//下一页、尾页
		if( ($page_now == $pages) || ($pages == 0) )
		{
			$last_page = $pages;
			$ss .= "<a class='p-next'>下一页</a>";
			$ss .= "<a class='p-next'>末  页</a>";
		} 
		else 
		{
			$next_page = $page_now + 1;
			$last_page = $pages;
			$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$next_page.";$form.submit();return false;\" class='p-next'>下一页</a>";
			$ss .= "<a style ='cursor:pointer' onclick=\"$form.page.value=".$last_page.";$form.submit();return false;\" class='p-next'>末 页</a>";
		}
	}

	return $ss;
}

?>
