<?php
class Paging {

  private $db;

  public function __construct() {
    $this->db = new Db_layer();
  }

  public function index_paging($pages, $page) {
    echo "\n\n<div class=\"paging\">";

    if (isset($_GET["topic"])) { $p1="topic"; $p2=$_GET["topic"]; }
    if (isset($_GET["forum"])) { $p1="forum"; $p2=$_GET["forum"]; }
    if ($_GET["action"]=="search") { $p1="action"; $p2="search"; }
    if (isset($_GET["w"])) { $search="&amp;w=".rawurlencode($_GET["w"]); } else { $search=""; }

    if ($pages>1 AND $page>1) {
    $i=$page-1;
    echo "<a href=\"./index.php?$p1=$p2&amp;page=$i$search\">&laquo;</a> ";
    }

    if ($pages<=9) {
    $i=0;
    while ($i<$pages) {
    $i++;
    if ($i==$page) { echo "$i&nbsp;"; } else { echo "<a href=\"./index.php?$p1=$p2&amp;page=$i$search\">$i</a>&nbsp;"; }
    }

    } else {

    if ($page==1) { echo "1&nbsp;...&nbsp;"; } else { echo "<a href=\"./index.php?$p1=$p2&amp;page=1$search\">1</a>&nbsp;...&nbsp;"; }

    $i=$page-5;
    $i2=$page+4;

    while ($i<$i2) {
    $i++;
    if ($i>1 AND $i<$pages) {
    if ($i==$page) { echo "$i "; } else { echo "<a href=\"./index.php?$p1=$p2&amp;page=$i$search\">$i</a> "; }
    }
    }

    if ($page==$pages) { echo "...&nbsp;$pages&nbsp;"; } else { echo "...&nbsp;<a href=\"./index.php?$p1=$p2&amp;page=$pages$search\">$pages</a>&nbsp;"; }

    }

    if ($pages>1 AND $page<$pages) {
    $i=$page+1;
    echo "<a href=\"./index.php?$p1=$p2&amp;page=$i$search\">&raquo;</a>";
    }

    echo "</div>";
  }

}
?>