php 分页 【3种形式】

<?php
$offset = isset($_GET['pid'])?intval($_GET['pid']):1;
if(!$offset) $offset = 1;
echo "the current page: $offset<br />";
$perpage = 50;

$db = NEW PDO("mysql:host=localhost; port = 3306; dbname =library", 'root', 'cai123');
$total = $db->query('SELECT COUNT(*) FROM library.messages')->fetchColumn(0);
$sql = "SELECT * FROM library.messages LIMIT ".($offset-1).", ". $perpage ;
$result = $db->query($sql);
if ($result)
{
	foreach ($result->fetchAll() as $row)
	{
		echo "subject:    ".$row[5]."<br />";
	}
}
else
{
	$error = $db->errorInfo();
	echo "error happened..".$error[2];
	exit();
}

buildIndex($offset,$perpage, $total);//1,50,3500

/
function buildLink($active,$index, $offset)
{
	if($active)
	{
		echo "<a>$index</a>";
	}
	else
	{
		echo "<a href ='http://127.0.0.1/php/example/setPage.php?pid=". $offset."'>$index</a>";
	}
}


function buildIndex($offset,$perpage, $total)
{
	$separator = '|';
	//buildLink($offset==1, '<<Prev', '')

	for ($start = 1, $end = $perpage; 
			$end < $total; 
				$start += $perpage, $end += $perpage)
	{
		echo $separator;
		buildLink($offset == $start,"$start-$end", $start);
	}

	$end = ($total > $start) ? "$total":"";
	echo $separator;
	buildLink($offset == $start,"$start-$end", $start);
}

?>
<?php
$offset = isset($_GET['offset'])?intval($_GET['offset']):1;
if(!$offset) $offset = 1;
echo "the current page: $offset<br />";
$perpage = 50;

$start = ($offset-1)*$perpage;
$db = NEW PDO("mysql:host=localhost; port = 3306; dbname =library", 'root', 'cai123');
$total = $db->query('SELECT COUNT(*) FROM library.messages')->fetchColumn(0);
$sql = "SELECT * FROM library.messages LIMIT  $start, $perpage";
$result = $db->query($sql);

if ($result)
{
	foreach ($result->fetchAll() as $row)
	{
		echo "subject:    ".$row[5]."<br />";
	}
}
else
{
	$error = $db->errorInfo();
	echo "error happened..".$error[2];
	exit();
}

$numpage = ceil($total/$perpage);
if($total%$perpage) 
	$numpage++;

$cur = $offset;
if ($cur < $numpage)
{
	echo "<a href ='http://127.0.0.1/php/example/setPage01.php?offset=".($cur+1)."'>Next Page</a><br />";
}
if($cur > 0)
{
	echo "<a href ='http://127.0.0.1/php/example/setPage01.php?offset=".($cur-1)."'>Prev Page</a>";
}


?>

<?php
$offset = isset($_GET["offset"]) ? intval($_GET["offset"]) : 1;
if(!$offset) 
	$offset = 1;

echo "the current page: $offset<br />";//测试语句

$perpage = 50; //每页显示30条目信息

$offset = ($offset-1)*$perpage;
$db = NEW PDO("mysql:host=localhost; port = 3306; dbname =library", 'root', 'cai123');
$total = $db->query('SELECT COUNT(*) FROM library.messages')->fetchColumn(0); //获取信息的总数
$sql = "SELECT * FROM library.messages LIMIT $offset, $perpage";
$result = $db->query($sql);
if ($result)
{
	foreach ($result->fetchAll() as $row)
	{
		echo "subject:    ".$row[5]."<br />";
	}
}
else
{
	$error = $db->errorInfo();
	echo "error happened..".$error[2];
	exit();
}

$numpage = ceil($total/$perpage);
if($total%$perpage) $numpage++;//计算可以显示为numpage页

if ($numpage > 1)
{
	for ($i = 1; $i <= $numpage; $i++)
	{
		if ($i == $offset) //如果是当前页
		{
			echo "$i";
		}
		else
		{
			//echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?offset=$i'".">$i</a>  ";
			echo '<a href = "http://127.0.0.1/php/example/setPage02.php?offset='.$i.'">'.$i.'</a>  ';
		}
	}
}

其实原理都是一样的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值