0

このウェブサイトhttp://tuts.wtfdiary.com/2012/06/simple-pagination-using-php-with-css.htmlに基づいてページネーションスクリプトを作成しました

少し変更しましたが、少し問題があります。結果が 40 を超えるエントリに分散している場合、1 ページあたり 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;

?>
4

1 に答える 1

1

ページを 1 行で数えることができます。

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

それ以外の

$oldtp=($tr/$rpp);

$tp=round($oldtp);

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

行数が異なる理由として考えられるのは、行をカウントするときと表示するときのクエリが異なることです。

于 2012-12-05T15:25:23.663 に答える