>  기사  >  백엔드 개발  >  两个设置分页的都出有关问题,想屎了.

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

WBOY
WBOY원래의
2016-06-13 11:55:10754검색

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

<br /><?php<br />$offset = isset($_GET['pid'])?intval($_GET['pid']):1;<br />if(!$offset) $offset = 1;<br />$perpage = 50;<br /><br /><br />$db = NEW PDO("mysql:host=localhost; port = 3306; dbname =library", 'root', 'cai123');<br />$total = $db->query('SELECT COUNT(*) FROM library.messages')->fetchColumn(0);<br /><br />$sql = 'SELECT * FROM library.messages'." LIMIT $perpage OFFSET ".($offset-1);<br />$result = $db->query($sql);<br />if ($result)<br />{<br />	foreach ($result->fetchAll() as $row)<br />	{<br />		echo "subject:    ".$row[5]."<br />";<br />	}<br />}<br />else<br />{<br />	$error = $db->errorInfo();<br />	echo "error happened..".$error[2];<br />	exit();<br />}<br /><br />buildIndex($perpage, $total);//1,50,3500<br /><br />/////////////////////////////////////////////////////////////////<br />function buildLink($index, $offset)<br />{<br />	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?pid = $offset'>$index</a>";<br />}<br /><br /><br />function buildIndex($perpage, $total)<br />{<br />	$separator = '|';<br />	//buildLink($offset==1, '<<Prev', '')<br /><br />	for ($start = 1, $end = $perpage; $end < $total; $start += $perpage, $end += $perpage)<br />	{<br />		echo $separator;<br />		buildLink("$start-$end", $start);<br />	}<br /><br />	$end = ($total > $start) ? "$total":"";<br />	echo $separator;<br />	buildLink("$start-$end", $start);<br />}<br /><br /><br /><br /><br />?><br />


另一种思路的.....
<br /><?php<br />$offset = isset($_GET['pid'])?intval($_GET['pid']):1;<br />if(!$offset) $offset = 1;<br />$perpage = 50;<br /><br /><br />$db = NEW PDO("mysql:host=localhost; port = 3306; dbname =library", 'root', 'cai123');<br />$total = $db->query('SELECT COUNT(*) FROM library.messages')->fetchColumn(0);<br /><br />$sql = 'SELECT * FROM library.messages LIMIT '. ($offset -1)*$perpage.", $perpage ";<br />$result = $db->query($sql);<br />if ($result)<br />{<br />	foreach ($result->fetchAll() as $row)<br />	{<br />		echo "subject:    ".$row[5]."<br />";<br />	}<br />}<br />else<br />{<br />	$error = $db->errorInfo();<br />	echo "error happened..".$error[2];<br />	exit();<br />}<br /><br />$numpage = $total%$perpage;<br />if($total%$perpage) $numpage++;<br />$cur = $offset;<br />if ($cur < $numpage)<br />{<br />	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?offset = ".($cur+1)."'> Next Page</a><br />";<br />}<br />if($cur > 0)<br />{<br />	echo "<a href ='". htmlentities($_SERVER['PHP_SELF'])."?offset =". ($cur-1)."'> Prev Page</a><br />";<br /><br />}<br /><br /><br />?><br />


问题如下:每次点击下一页,或者上一页,显示的都是同样的内容,即首页内容。 当数据表的内容有3000行...是怎么回事啊?
------解决方案--------------------
42 行有 ...setPage02.php?offset=$i'>$i....
而第 2 行是 $offset = isset($_GET['ofset']) ? intval($_GET['offset']) : 1;

知道问题在哪了吧?

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.