<?php

class navibar {
  // Default values for the navigation link bar
  var $str_previous = "previous page";
  var $str_next = "next page";
  // Variables used internally
  var $file;
  var $total_records;

  function execute($sql, $db, $type = "mysql") {
    global $total_records, $row, $numtoshow;

    $numtoshow = 20;
    if (!isset($row)) $row = 0;
     $start = $row * $numtoshow;
     $result = mysql_query($sql);
     $total_records = mysql_num_rows($result);
     $sql .= " LIMIT $start, $numtoshow";
     $result = mysql_query($sql);
      return $result;
  }

  
  function build_geturl()
  {
    global $REQUEST_URI, $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS;

    list($fullfile, $voided) = explode("?", $REQUEST_URI);
    $this->file = $fullfile;
    $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS;
    reset ($cgi);
    while (list($key, $value) = each($cgi)) {
      if ($key != "row")
        $query_string .= "&" . $key . "=" . $value;
    }
    return $query_string;
  }

  // $option parameter (default to "all") :
  //  . "all"   - return every navigation link
  //  . "pages" - return only the page numbering links
  //  . "sides" - return only the 'Next' and / or 'Previous' links
  //
  // $show_blank parameter (default to "off") :
  //  . "off" - don't show the "Next" or "Previous" when it is not needed
  //  . "on"  - show the "Next" or "Previous" strings as plain text when it is not needed
  function getlinks($option = "all", $show_blank = "off") {
    global $total_records, $row, $numtoshow;

    $extra_vars = $this->build_geturl();
    $file = $this->file;
    $number_of_pages = ceil($total_records / $numtoshow);
    $subscript = 0;
    for ($current = 0; $current < $number_of_pages; $current++) {
      if ((($option == "all") || ($option == "sides")) && ($current == 0)) {
        if ($row != 0)
          $array[0] = '<a href="' . $file . '?row=' . ($row - 1) . $extra_vars . '"><img src="img/prev.gif" style="border: 0 none"></a>';
        elseif (($row == 0) && ($show_blank == "on"))
          $array[0] = '<img src="img/prev1.gif" style="border: 0 none">';
      }

      if (($option == "all") || ($option == "pages")) {
        if ($row == $current)
          $array[++$subscript] = ($current > 0 ? ($current + 1) : 1);
        else
          $array[++$subscript] = '<a href="' . $file . '?row=' . $current . $extra_vars . '" class="links">' . ($current + 1) . '</a>';
      }

      if ((($option == "all") || ($option == "sides")) && ($current == ($number_of_pages - 1))) {
        if ($row != ($number_of_pages - 1))
          $array[++$subscript] = '<a href="' . $file . '?row=' . ($row + 1) . $extra_vars . '"><img src="img/next.gif" style="border: 0 none"></a>';
        elseif (($row == ($number_of_pages - 1)) && ($show_blank == "on"))
          $array[++$subscript] = '<img src="img/next1.gif" style="border: 0 none">';
      }
    }
    return $array;
  }
}
?>
