Maison > php教程 > PHP源码 > PHP 计算分页的类

PHP 计算分页的类

PHP中文网
Libérer: 2016-05-25 17:14:43
original
1165 Les gens l'ont consulté

PHP 计算分页的类

<?
class pagination
{
	public function __construct()
	{
	}
	public function calculate_pages($total_rows, $rows_per_page, $page_num)
	{
		$arr = array();
		// calculate last page
		$last_page = ceil($total_rows / $rows_per_page);
		// make sure we are within limits
		$page_num = (int) $page_num;
		if ($page_num < 1)
		{
		   $page_num = 1;
		} 
		elseif ($page_num > $last_page)
		{
		   $page_num = $last_page;
		}
		$upto = ($page_num - 1) * $rows_per_page;
		$arr[&#39;limit&#39;] = &#39;LIMIT &#39;.$upto.&#39;,&#39; .$rows_per_page;
		$arr[&#39;current&#39;] = $page_num;
		if ($page_num == 1)
			$arr[&#39;previous&#39;] = $page_num;
		else
			$arr[&#39;previous&#39;] = $page_num - 1;
		if ($page_num == $last_page)
			$arr[&#39;next&#39;] = $last_page;
		else
			$arr[&#39;next&#39;] = $page_num   1;
		$arr[&#39;last&#39;] = $last_page;
		$arr[&#39;info&#39;] = &#39;Page (&#39;.$page_num.&#39; of &#39;.$last_page.&#39;)&#39;;
		$arr[&#39;pages&#39;] = $this->get_surrounding_pages($page_num, $last_page, $arr[&#39;next&#39;]);
		return $arr;
	}
	private function get_surrounding_pages($page_num, $last_page, $next)
	{
		$arr = array();
		$show = 5; // how many boxes
		// at first
		if ($page_num == 1)
		{
			// case of 1 page only
			if ($next == $page_num) return array(1);
			for ($i = 0; $i < $show; $i  )
			{
				if ($i == $last_page) break;
				array_push($arr, $i   1);
			}
			return $arr;
		}
		// at last
		if ($page_num == $last_page)
		{
			$start = $last_page - $show;
			if ($start < 1) $start = 0;
			for ($i = $start; $i < $last_page; $i  )
			{
				array_push($arr, $i   1);
			}
			return $arr;
		}
		// at middle
		$start = $page_num - $show;
		if ($start < 1) $start = 0;
		for ($i = $start; $i < $page_num; $i  )
		{
			array_push($arr, $i   1);
		}
		for ($i = ($page_num   1); $i < ($page_num   $show); $i  )
		{
			if ($i == ($last_page   1)) break;
			array_push($arr, $i);
		}
		return $arr;
	}
}
?>
Copier après la connexion

2. [代码]使用方法

$p = new pagination();
$arr = $p->calculate_pages(70, 10, 1);
Copier après la connexion

3. [代码]返回结果

Array
(
    [limit] => LIMIT 0,10
    [current] => 1
    [previous] => 1
    [next] => 2
    [last] => 7
    [info] => Page (1 of 7)
    [pages] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
        )
)
Copier après la connexion

           

 以上就是PHP 计算分页的类的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


       

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal