[英]Problem with PHP Pagination
我的代碼有問題(非常抱歉,但這是我知道向您顯示的唯一方法,它基本上只是一個表中的select語句),它顯示頁面鏈接,但不會更改頁面結果。 基本上,我將其設置為每頁1個結果,但它顯示了所有結果,但仍在頂部顯示一個鏈接,可轉到下一頁。 下一頁僅顯示相同內容。 我是PHP的初學者,所以任何幫助將不勝感激。
謝謝!
<?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-->";
}
邁克爾,我嘗試了您的操作,但可能做錯了...這是where語句之前的代碼
<?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>";
}
}
回聲””;
問題在於,無論哪個頁面當前處於“活動”狀態,您總是會獲取相同的結果集,然后僅輸出所有結果集。 如果要分頁,則可能要在SQL查詢中使用LIMIT
子句(例如LIMIT 20,10
從偏移量20(從零開始,即記錄編號21))返回10條記錄。
您需要做的是添加如下內容:
$ page = 1;
$ results_per_page = 10;
$ record_start =($ page * $ results_per_page)-$ results_per_page;
$ result = mysql_query(“ SELECT * FROM gj,其中名稱類似'%$ q%'或cat1類似'%$ q%'或cat2類似'%$ q'或cat3類似'%$ q'的名稱按名稱LIMIT $ record_start ,$ results_per_page“)或trigger_error(mysql_error());
檢查上方鏈接以獲取代碼,可能對您有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.