Home > Backend Development > PHP Tutorial > 两个设置分页的都出有关问题,想屎了.

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

WBOY
Release: 2016-06-13 11:55:10
Original
775 people have browsed it

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

<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 />
Copy after login


另一种思路的.....
<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 />
Copy after login


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

知道问题在哪了吧?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template