PHP分页问题
我的代码有问题(很抱歉,有很多问题,但这是我知道的唯一向您显示页面链接的方法,基本上只是表中的select语句),它显示页面链接,但不会更改页面结果。基本上,我将其设置为每页1个结果,但它显示了所有结果,但仍然在顶部显示了一个链接以转到下一页。下一页将显示相同的内容。我是一个PHP初学者,所以任何帮助都将不胜感激 谢谢PHP分页问题,php,mysql,pagination,logic,Php,Mysql,Pagination,Logic,我的代码有问题(很抱歉,有很多问题,但这是我知道的唯一向您显示页面链接的方法,基本上只是表中的select语句),它显示页面链接,但不会更改页面结果。基本上,我将其设置为每页1个结果,但它显示了所有结果,但仍然在顶部显示了一个链接以转到下一页。下一页将显示相同的内容。我是一个PHP初学者,所以任何帮助都将不胜感激 谢谢 <?php if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
<?php
if (isset($_GET['page'])) $page = $_GET['page']; else $page = 1;
$max_results = 1;
$from = (($page * $max_results) - $max_results);
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q'"),0);
// (2) Calculate total number of pages. Round up using ceil()
$total_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$prev\"><< Prev</a> ";
}
// (4) display page numbers
for($i = 1; $i <= $total_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$i\">$i</a> ";
}
}
// (5) build Next Link
if($page < $total_pages)
{
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$next\">Next >></a>";
}
}
echo"</div></div></div>";
while($row = mysql_fetch_array($result))
{
$id=$row['id'];
$name=$row['name'];
$phone=$row['phone'];
$website=$row['website'];
$city=$row['city'];
$address=$row['address1'];
$zipcode=$row['zipcode'];
$sponsored = $row['sponsored'];
$addressmap = preg_replace('/\s/', '+',$address);
$citymap = preg_replace('/\s/', '+',$city);
//Start While Loop
echo"
<div id='listing'>
<div id='mainlisting'>";
echo"
<div class='name'>
<a href='./more.php?id=$id' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>$name</a> <div class='right'>$phone</div>
</div>
<div class='other'>
$address, $city, CO $zipcode
|<a target='_blank' href='http://maps.google.com/maps? f=q&source=s_q&hl=en&geocode=&q=$addressmap,+$city+CO&&&ie=UTF8&hq=&hnear=$address,+$city,+Colorado+$zipcode&safe=active&&&t=h&z=14&iwloc=A&output=embed' rel='lyteframe' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"' title='$name' rev='width: 500px; height: 500px; scrolling: no;'> See Map</a><br/>
<a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>";if($website != null){ echo "<a target='_blank' href='$website' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>Website</a> |";}echo" <a href='#' class='";if($sponsored != 1){echo "red";}else{echo"sponsored";}echo"'>More Info</a>
</div>
</div>
</div><!--/LISTING-->";
}
问题在于,无论当前哪个页面处于“活动”状态,您总是获取相同的结果集,然后只输出所有结果集。如果要分页,可能需要在SQL查询中使用类似于LIMIT
子句的语句(例如LIMIT 20,10
以返回从偏移量20开始的10条记录(基于零,即记录编号21))。您需要做的是添加以下内容:
$page=1
每页$results\u=10
$record_start=($page*
$results_/页)-
每页$results\u
$result=mysql\u查询(“选择*FROM
gj,其中名称类似“%$q%”或类似cat1
“%$q%”或类似cat2的“%$q”或类似cat3的
“%$q”按名称排序限制
$record\u start,$results\u per\u page”)或
触发器_错误(mysql_error())
检查上面的链接以获取代码,它可以帮助您吗我刚刚做了这个,但它不起作用??是这样吗$结果=mysql_查询(“从gj中选择*,其中名称类似“%$q%”或cat1类似“%$q%”或cat2类似“%$q”或cat3类似“%$q”按名称排序限制20,10”)或触发器_错误(mysql_错误();我刚刚编辑了我的答案…对不起,我真的是一个PHP初学者…谢谢你的帮助。看起来你忘记了第一个查询中每页$record\u start,$results\u的限制。我仍然有一个问题,但它不会像以前那样出错。它现在只显示了15行中的9行。那么如果你点击下一页,链接就不起作用了。??好的,我修复了它。如果添加If(isset($\u GET['page']))$page=$\u GET['page'];否则$page=1;由于$page包含您的代码,因此它可以完美地工作!!万分感谢!为什么不创建一个类来处理这个问题,因为你不想在每次需要页面的时候都写出来。谢谢@RobertPitt,我会的
<?php
$page = 1; $total_pages = 9; $record_start = ($page * $total_pages) - $total_pages;
REQUIRE('config.php');
$q = mysql_real_escape_string(ucfirst(trim($_REQUEST['q'])));
$result = mysql_query("SELECT * FROM gj WHERE name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q' ORDER by name LIMIT 0,9") or trigger_error(mysql_error());
$rows = mysql_num_rows($result);
if($rows == 0){
}
echo " <div id='title'>Search for "$q"<div class='righttitle'>$rows business";if($rows > 1){echo "es";}elseif($rows == "0"){echo "es";}echo" found";
echo"<div id='pagenumbers'>";
// (1) get the total number of results for your query
// modify this to match the total results for the main query
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM gj where name LIKE '%$q%' OR cat1 LIKE '%$q%' OR cat2 LIKE '%$q' OR cat3 LIKE '%$q '"),0);
// (2) Calculate total number of pages. Round up using ceil()
$alltotal_pages = ceil($total_results / $max_results);
if($total_results > $max_results)
{
// (3) build Previous link
if($page > 1)
{
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$prev\"><< Prev</a> ";
}
// (4) display page numbers
for($i = 1; $i <= $alltotal_pages; $i++)
{
if($page == $i)
{
echo $i . " ";
}
else
{
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$i\">$i</a> ";
}
}
// (5) build Next Link
if($page < $alltotal_pages)
{
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?q=$q&page=$next\">Next >></a>";
}
}