我创建了以下脚本,用于确定要在我的网站上显示哪些页码以进行导航。
// query the database
$statement = $connect->prepare("SELECT COUNT(report_number) FROM reports");
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
$rowCount = $result["COUNT(report_number)"];
$pgCount = ceil($rowCount / 10);
// fetch the corresponding entries from the database
if (!isset($pgParamArray["page"]) || $pgParamArray["page"] === $pgCount) {
$pgParamArray["page"] = $pgCount;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT 10";
} elseif (isset($pgParamArray["page"])) {
$offset = ($pgCount - $pgParamArray["page"]) * 10;
$sql = "SELECT * FROM reports ORDER BY report_number DESC LIMIT " . $offset . ",10";
}
// calculate and display the appropriate page numbers
$upperDiff = $pgCount - $pgParamArray["page"];
$lowerDiff = $pgParamArray["page"] - 1;
for ($i = 0; $i < 5; $i++) {
$pgNo = 0;
if ($pgParamArray["page"]+2 <= $pgCount && $pgParamArray["page"]-2 >= 1) {
$pgNo = $pgParamArray["page"] + ($i - 2);
} elseif ($pgCount < 5) {
$pgNo = $i+1 <= $pgCount ? $i+1 : 0;
} elseif ($upperDiff < $lowerDiff && $upperDiff < 2 && $upperDiff >= 0) {
$pgNo = ($pgCount - 4) + $i;
} elseif ($lowerDiff < $upperDiff && $lowerDiff < 2 && $lowerDiff >= 0) {
$pgNo = $i + 1;
} else {
$pgNo = ($pgCount - 4) + $i;
}
if ($pgNo > 0) {
if ($pgNo !== $pgParamArray["page"]) {
$pgNoArray[] = ""
. "<li><div><a href='http://www.somewebsite.com/index.php/?page="
. $pgNo
. "'>"
. $pgNo
. "</a></div></li>";
} else {
$pgNoArray[] = ""
. "<li><div style='background:#333'>"
. $pgNo
. "</div></li>";
}
}
}
当我设置$pgParamArray["page"] = $pgCount = ceil($rowCount/10)
时,我注意到最前面的变量仍被视为浮点数。我的问题是为什么$pgParamArray["page"]
类型为float,即使ceil($rowCount/10)
显然返回一个整数?
答案 0 :(得分:4)