我有以下代码,它是一个更大应用程序中分页的基础。然而,它不起作用,因为虽然我应该从url获取pg的值,但pg从未超过2。由于某种原因,next = pg + 1;似乎总是将pg视为1,而不管url传递什么。这是last的类似问题。我认为我正在覆盖从GET获取的值,但我不确定在哪里。
问题似乎在于我如何计算$max和极限,因为传递的不是0、10、-10、10。此外,$max之前的if代码似乎无法成功阻止$pg变为0。
<?php
if (isset($_GET["pg"])) {
$pg = $_GET["pg"];
} else $pg = 1;
$con = mysql_connect("localhost","","");
if(!$con) {
die( Connection failed because of .mysql_error());
}
mysql_select_db("ebay",$con);
if ($pg < 1) {
$pg = 1;
} elseif ($pg > $last) {
$pg = $last;
}
$table = AUCTIONS ;
$page_rows = 10;
$max = limit .($pg - 1) * $page_rows . , .$page_rows;
$rows = getRowsByArticleSearch($query, $table, $max);
$rowcount = count($rows);
echo $rowcount;
$last = ceil($rowcount/$page_rows);
$page_rows = 10;
$rowcount = 2;
// Would normally obtain the number of rows returned, but database stuff is snipped for brevity
$last = ceil($rowcount/$page_rows);
if ($pg < 1) {
$pg = 1;
} elseif ($pg > $last) {
$pg = $last;
}
$self = htmlspecialchars($_SERVER[ PHP_SELF ],ENT_QUOTES, utf-8 );
echo " <a href= $self?pg=1 > <<-First</a> ";
$previous = $pg-1;
echo " <a href= $self?pg=$previous > <-Previous</a> ";
echo "---------------------------";
$next = $pg+1;
echo " <a href= $self?pg=$next >Next -></a> ";
echo " <a href= $self?pg=$last >Last ->></a> ";