2012-12-05 105 views
0

我已經建立了基於本網站http://tuts.wtfdiary.com/2012/06/simple-pagination-using-php-with-css.html分頁返回不正確的數量

我稍微改變它,但它給我一點點麻煩在分頁腳本。如果結果分散在40多個條目中,則它應該是每頁10個,因此理論上它應該顯示5個頁面?但很多時候它會展示更多。

有人會直接指出我的意思嗎?我的代碼是下面....

謝謝你們:)

<?php 

//include ("db.php"); 
include_once 'header.php'; 

$ddoption=''; 

if (isset($_GET['searchid'])){ 
$searchid = $_GET['searchid']; 
} 
//$result=mysql_query("select count(*) from product"); 
$result=mysql_query("select count(*) from product WHERE (title OR description LIKE '%".$searchid."%')OR(title ='".$searchid."') OR(description='".$searchid."')"); 
$row=mysql_fetch_row($result); 
$tr=$row[0]; 
$rpp=10; 
$pn=1; 
$searchid = ''; 


if (isset($_GET['searchid'])){ 
$searchid = $_GET['searchid']; 
} 

if(isset($_GET['pn'])) 
{ 
    $pn=$_GET['pn']; 
} 

$oldtp=($tr/$rpp); 

$tp=round($oldtp); 

if($tr%$rpp>0) 
{ 
    $tp++; 
} 
$from=(($pn-1)*$rpp)/*+1*/; 
$to=($pn)*($rpp)-1; 
//$result=mysql_query("SELECT * from product WHERE productid between $from AND $to"); 


//$result=mysql_query("SELECT * FROM product WHERE productid between $from AND $to AND title OR description LIKE '%".$id."%'"); 
echo "<h1> Results from ". $from." to ".$to. "</h1>"; 

if(isset($_GET['option'])){ 
$ddoption = $_GET['option']; 
if($ddoption=="both"){ 
$result=mysql_query("SELECT * FROM product WHERE (title LIKE '%".$searchid."%')OR (description LIKE '%".$searchid."%') OR(title ='".$searchid."') OR(description='".$searchid."') ORDER BY productid DESC LIMIT $from,10"); 
} 
else if($ddoption=="title"){ 
$result=mysql_query("SELECT * FROM product WHERE (title LIKE '%".$searchid."%')OR(title ='".$searchid."') ORDER BY productid DESC LIMIT $from,10"); 
} 
else{ 
$result=mysql_query("SELECT * FROM product WHERE (description LIKE '%".$searchid."%')OR(description='".$searchid."') ORDER BY productid DESC LIMIT $from,10"); 
} 
} 
echo "<table>"; 


while($row=mysql_fetch_array($result)) 
{ 

echo '<tr><td><a href="item.php?id='. $row['productid'].'">'.'<img src="'.$row['image1'].'" alt="'.$row['title'].'" height="100" width="100"/></a></td>'; 
     echo '<td><a href="item.php?id='. $row['productid'].'">'.$row['title'].'</a></td></tr>'; 

} 

echo "</table>"; 
echo "<ul id='pages'>"; 
for($i=1;$i<=$tp;$i++) 
{ 
echo "<li><a href='search.php?pn=$i&searchid=$searchid&amp;option=$ddoption'>Page $i</a></li>"; 
} 
echo "</ul>"; 


echo <<<_END 

</body> 
</html> 


_END; 

?> 
+0

offtopic:不使用'mysql_'。它已被棄用。使用**'PDO' **並準備好陳述。讓這個世界變得更好:) – Stranger

回答

1

您可以在一個行數頁面:

$tp = ceil($tr/$rpp); 

,而不是

$oldtp=($tr/$rpp); 

$tp=round($oldtp); 

if($tr%$rpp>0) 
{ 
    $tp++; 
} 

的可能原因不同數量的行是您計算行數和替換它們時查詢不同的行數。

+0

Woops忘記編輯這一輪了。試圖找出它爲什麼這樣做... – kidshenlong

+0

就是這樣!非常感謝。我忘了更改上面的查詢!謝謝 – kidshenlong