所以我设法在php中建立了一个可正常使用的分页系统。但是我希望用户默认使用所述分页的第一个页码,因此例如,如果有人单击类别1,则URL将更改为此website.php?category=1
,我希望将其作为website.php?category=1&pagenumber=1
作为默认URL。
我想要这个的原因是这样,我不必写2个不同的查询:如果设置了类别但没有页面编号,则为1;如果同时设置了页面编号,则为1。
function getNews()
{
// check if category is get via URL
if (isset($_GET["category"]) && is_numeric($_GET["category"]) && intval($_GET["category"])) {
// bind get category to categoryId
$categoryId = intval($_GET["category"]);
// if category is 1
if ($categoryId == 1) {
try {
$db = new Connection();
$database = $db->openConnection();
if (isset($_GET["pagenumber"]) && is_numeric($_GET["pagenumber"]) && intval($_GET["pagenumber"])) {
$pageNumber = intval($_GET["pagenumber"]);
$itemsPerPage = 9;
$offset = ($pageNumber - 1) * $itemsPerPage;
$stm = $database->query("SELECT * FROM blog where blog.Categoryid = $categoryId LIMIT 9 OFFSET $offset ");
$stm->execute();
$rowNews = $stm->fetchAll(PDO::FETCH_ASSOC);
$totalItems = $database->query("SELECT COUNT(*) FROM blog where blog.Categoryid = $categoryId")->fetchColumn();
$totalPages = intval(ceil($totalItems / $itemsPerPage));
$previousButton = ($pageNumber + 1) - $totalPages;
$nextButton = ($pageNumber) * $totalPages;
$pageIndex = 0;
这是我目前拥有的(我已经切断了分页系统,可以在需要时将其粘贴)。
//
这就是我生成类别选择的方式
<?php
if (isset($_GET["category"])){
$db = new Connection();
$categoryId = $_GET["category"];
$database = $db->openConnection();
$stm = $database->query("SELECT * FROM category");
$stm->execute();
$fetchCategory = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($fetchCategory as $cats) {
?>
<a class="dropdown-item" href="blog.php?category=<?php echo $cats["categoryid"]; ?>"><?php echo $cats["categorytitle"]; ?></a>
<?php
}
?>
<?php
}else{
$categoryId = 1;
}
?>
</div>
</div>
<?php
$categoryId = -1;
if (isset($_GET["category"]) && intval($_GET["category"])) {
$categoryId = intval($_GET["category"]);
switch ($categoryId) {
case 1:
// do query for category id 1
getNews();
break;
case 2:
// do query for category id 2
getEwarehouse();
break;
case 3:
// do query for category id 3
getPartners();
break;
default:
ifnotNumeric();
break;
}
} else {
ifnotSet();
}
?>
更多一般信息:
如果您需要更多的代码示例,可以将其粘贴。