分页当前页面颜色

时间:2013-04-08 17:38:41

标签: php pagination

下面是我的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}\">&laquo;</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}\">&raquo;</a> ";
    }
    else {
      $pagination. = " ";
    }
  }
  if (empty($pagination)) {
    $pagination = " <a href=\"{$targetpage}1\">1</a> ";
  }
  return $pagination;
}

1 个答案:

答案 0 :(得分:0)

你需要替换

if ($counter == $page) {
    $pagination. = " <a href=\"{$targetpage}{$counter}\">{$counter}</a> ";
}

if ($counter == $page) {
    $pagination. = " <a href=\"{$targetpage}{$counter}\" class=\"current\">{$counter}</a> ";
}

在您的css中添加以下规则

a.current{background: #f00; color:#fff; border: 1px solid #000} /*change it however you like*/