Maison  >  Article  >  développement back-end  >  两个设置分页的都出问题,想屎了.

两个设置分页的都出问题,想屎了.

WBOY
WBOYoriginal
2016-06-23 13:58:37986parcourir

<?php$offset = isset($_GET['pid'])?intval($_GET['pid']):1;if(!$offset) $offset = 1;$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 $perpage OFFSET ".($offset-1);$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($perpage, $total);//1,50,3500/////////////////////////////////////////////////////////////////function buildLink($index, $offset){	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?pid = $offset'>$index</a>";}function buildIndex($perpage, $total){	$separator = '|';	//buildLink($offset==1, '<<Prev', '')	for ($start = 1, $end = $perpage; $end < $total; $start += $perpage, $end += $perpage)	{		echo $separator;		buildLink("$start-$end", $start);	}	$end = ($total > $start) ? "$total":"";	echo $separator;	buildLink("$start-$end", $start);}?>


另一种思路的.....
<?php$offset = isset($_GET['pid'])?intval($_GET['pid']):1;if(!$offset) $offset = 1;$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.", $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 = $total%$perpage;if($total%$perpage) $numpage++;$cur = $offset;if ($cur < $numpage){	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?offset = ".($cur+1)."'> Next Page</a><br />";}if($cur > 0){	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?offset =". ($cur-1)."'> Prev Page</a><br />";}?>


问题如下:每次点击下一页,或者上一页,显示的都是同样的内容,即首页内容。 当数据表的内容有3000行...是怎么回事啊?


回复讨论(解决方案)

没有看到你处理传入的 offset

没有看到你处理传入的 offset



$offset = isset($_GET['pid'])?intval($_GET['pid']):1;if(!$offset) $offset = 1;开头就处理了啊

参考了下往上的,现在修改如下:

<?php$offset = isset($_GET['ofset']) ? intval($_GET['offset']) : 1;if(!$offset) $offset = 1;echo "the current page: $offset<br />";//测试语句$perpage = 30; //每页显示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>  ";		}	}}

当我点击显示第20页的内容时,测试信息显示的还是第一页,但是我获取的是:$_GET['offset']没有错啊,怎么不论点那一页都显示是第一页?

如下图,我点的是第20页。浏览器地址栏显示: http://127.0.0.1/php/example/setPage02.php?offset=20
但echo "the current page: $offset
"; 显示的还是第一页。 
怎么回事?

42 行有 ...setPage02.php? offset=$i'>$i....
而第 2 行是 $offset = isset($_GET[' ofset']) ? intval($_GET['offset']) : 1;

知道问题在哪了吧?

42 行有 ...setPage02.php? offset=$i'>$i....
而第 2 行是 $offset = isset($_GET[' ofset']) ? intval($_GET['offset']) : 1;

知道问题在哪了吧?





哈哈哈,,,,我已经被自己打败了,看来我还是要换编辑工具了,这么明显的拼写差异没发现,害得我好一顿纠结...THX

42 行有 ...setPage02.php? offset=$i'>$i....
而第 2 行是 $offset = isset($_GET[' ofset']) ? intval($_GET['offset']) : 1;

知道问题在哪了吧?




现在初学,我是在windows环境下,平时一直都是用EditPlus编辑工具,这么明显的差异就这么放过去了,由衷的蛋碎》。。。
Déclaration:
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