下面是我的 php 分页代码。我想在分页布局中为当前页码添加颜色。有人请看下面的代码,因为我对 php 不太了解,并帮助我编写当前页码颜色代码。除了没有突出显示当前页码外,此代码一切正常。我添加了当前页面颜色,然后我意识到它与分页代码有关。
public function getBasicPagination($total_pages, $page, $limit, $targetpage) {
$adjacents = 3;
if ($page == 0) {
$page = 1;
}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages / $limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if (1 < $lastpage) {
if (1 < $page) {
$pagination. = " <a href=\"{$targetpage}{$prev}\">«</a> ";
}
else {
$pagination. = " ";
}
if ($lastpage < 7 + $adjacents * 2) {
$counter = 1;
while ($counter <= $lastpage) {
if ($counter == $page) {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
else {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
if ($counter < $lastpage) {
$pagination. = " ";
}
++$counter;
}
}
else if (5 + $adjacents * 2 < $lastpage) {
if ($page < 1 + $adjacents * 2) {
$counter = 1;
while ($counter < 4 + $adjacents * 2) {
if ($counter == $page) {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
else {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
if ($counter < 3 + $adjacents * 2) {
$pagination. = " ";
}
++$counter;
}
$pagination. = " ... ";
$pagination. = " <a href=\"{$targetpage}{$lpm1}\">{$lpm1}</a> ";
$pagination. = " <a href=\"{$targetpage}{$lastpage}\">{$lastpage}</a> ";
}
else if ($page < $lastpage - $adjacents * 2 && $adjacents * 2 < $page) {
$pagination. = " <a href=\"{$targetpage}1\">1</a > ";
$pagination. = " <a href=\"{$targetpage}2\">2</a> ";
$pagination. = " ... ";
$counter = $page - $adjacents;
while ($counter <= $page + $adjacents) {
if ($counter == $page) {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
else {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
if ($counter < $page + $adjacents) {
$pagination. = " ";
}
++$counter;
}
$pagination. = " ... ";
$pagination. = " <a href=\"{$targetpage}{$lpm1}\">{$lpm1}</a> ";
$pagination. = " <a href=\"{$targetpage}{$lastpage}\">{$lastpage}</a> ";
}
else {
$pagination. = " <a href=\"{$targetpage}1\">1</a> ";
$pagination. = " <a href=\"{$targetpage}2\">2</a> ";
$pagination. = " ... ";
$counter = $lastpage - (2 + $adjacents * 2);
while ($counter <= $lastpage) {
if ($counter == $page) {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
else {
$pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}
if ($counter < $lastpage) {
$pagination. = " ";
}
++$counter;
}
}
}
if ($page < $counter - 1) {
$pagination. = " <a href=\"{$targetpage}{$next}\">»</a> ";
}
else {
$pagination. = " ";
}
}
if (empty($pagination)) {
$pagination = " <a href=\"{$targetpage}1\">1</a> ";
}
return $pagination;
}