PHP Classes

File: navbar.php

Recommend this page to a friend!
  Classes of Luiz F B Escobar   navbar   navbar.php   Download  
File: navbar.php
Role: ???
Content type: text/plain
Description: Class NAVBAR
Class: navbar
Author: By
Last change:
Date: 22 years ago
Size: 7,565 bytes
 

Contents

Class file image Download
<?php // @(#) $Id: navbar.php,v 1.6 2001/10/10 11:00:00am jpm Exp $ /* Class navbar Copyright Joao Prado Maia (jpm@phpbrasil.com) UpDate Luiz Escobar (escobar@netsite.com.br) subject: NAVBAR.PHP - alterado o "previus <<< /next >>>" - corrigido o problema da falta do next na primeira pagina - adcionada a função PAGES() que monta a barra de navegação Sweet little class to build dynamic navigation links. Please notice the beautiful simplicity of the code. This code is free in any way you can imagine. If you use it on your own script, please leave the credits as it is. Also, send me an e-mail if you do, it makes me happy :) Below goes an example of how to use this class: =============================================== */ class navbar { // Default values for the navigation link bar var $numrowsperpage = 10; var $str_previous = '<img src="previous.gif" border=0>'; var $str_next = '<img src="next.gif" border=0>'; // Variables used internally var $file; var $total_records; var $row; // Class constructor. This is only used to set // the current row number so the other methods // can re-use it later on. function navbar() { global $row; $this->row = $row ? $row : 0; } // The next method runs the needed queries. // It needs to run the first time to get the total // number of rows returned, and the second one to // get the limited number of rows. // // $sql parameter : // . the actual SQL query to be performed // // $db parameter : // . the database connection link // // $type parameter : // . "mysql" - uses mysql php functions // . "pgsql" - uses pgsql php functions function execute($sql, $db, $speed = "optimized", $type = "mysql") { $start = $this->row * $this->numrowsperpage; if ($speed == "optimized") { $total_sql = preg_replace("/SELECT (.*?) FROM /sei", "'SELECT COUNT(*) FROM '", $sql); } else { $total_sql = $sql; } if ($type == "mysql") { $result = mysql_query($total_sql, $db); $this->total_records = mysql_result($result, 0, 0); $sql .= " LIMIT $start, $this->numrowsperpage"; $result = mysql_query($sql, $db); } elseif ($type == "pgsql") { $result = pg_Exec($db, $total_sql); $this->total_records = pg_Result($result, 0, 0); $sql .= " LIMIT $this->numrowsperpage, $start"; $result = pg_Exec($db, $sql); } return $result; } // This method creates a string that is going to be // added to the url string for the navigation links. // This is specially important to have dynamic links, // so if you want to add extra options to the queries, // the class is going to add it to the navigation links // dynamically. function build_geturl() { global $REQUEST_URI, $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS; @list($this->file, $voided) = @explode("?", $REQUEST_URI); //$cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS; $cgi = $HTTP_GET_VARS; reset($cgi); while (list($key, $value) = each($cgi)) { if ($key != "row") $query_string .= "&" . $key . "=" . $value; } return $query_string; } // This method creates an array of all the links for the // navigation bar. This is useful since it is completely // independent from the layout or design of the page. // The method returns the array of navigation links to the // caller php script, so it can build the layout with the // navigation links content available. // // $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") { $extra_vars = $this->build_geturl(); $file = $this->file; $number_of_pages = ceil($this->total_records / $this->numrowsperpage); $subscript = 0; for ($current = 0; $current < $number_of_pages; $current++) { if ((($option == "all") || ($option == "sides")) && ($current == 0)) { if ($this->row != 0) $array[0] = '<A HREF="' . $file . '?row=' . ($this->row - 1) . $extra_vars . '">' . $this->str_previous . '</A>'; elseif (($this->row == 0) && ($show_blank == "on")) $array[0] = $this->str_previous; } if (($option == "all") || ($option == "pages")) { if ($this->row == $current) $array[++$subscript] = '[<strong>'.($current > 0 ? ($current + 1) : 1).'</strong>]' ; else $array[++$subscript] = '<A HREF="' . $file . '?row=' . $current . $extra_vars . '">' . ($current + 1) . '</A>'; } else if ($this->row==$current) $array[++$subscript] = '[<strong>'.($current > 0 ? ($current + 1) : 1).'/'.$number_of_pages.'</strong>]' ; if ((($option == "all") || ($option == "sides")) && ($current == ($number_of_pages -1))) { if ($this->row != ($number_of_pages -1 )) $array[++$subscript] = '<A HREF="' . $file . '?row=' . ($this->row + 1) . $extra_vars . '">' . $this->str_next . '</A>'; elseif (($this->row == ($number_of_pages -1 )) && ($show_blank == "on")) $array[++$subscript] = $this->str_next; } } return $array; } // This method is an extension of the getlinks() method to // be able to set a limit of 'n' number of links on the page. // That is very useful for big record-sets which you do not // want to take a huge space in the screen to show the full // list of paginated links. // // $array parameter : // . the array returned by getlinks() // // $current parameter : // . the 'row' variable passed by navbar to other paginated pages // // $desired_size parameter : // . the number of links desired for the record-set function showPart($array, $current, $desired_size) { $size = count($array); if (($size <= 2) || ($size < $desired_size)) { $temp = $array; } else { $temp = array(); if (($current+$desired_size) > $size) { $temp = array_slice($array, $size-$desired_size); } else { $temp = array_slice($array, $current, $desired_size); if ($size >= $desired_size) { array_push($temp, $array[$size-1]); } } if ($current > 0) { array_unshift($temp, $array[0]); } } return $temp; } // ***************************************** // *** add v1.6 monta o navegador na pagina // *** escobar@netsite.com.br // *** uso: // *** $nav->pages('header'); // *** dados retornados da QUERY... // *** $nav->pages('footer'); // function Pages( $tipo = "nothing", $all='all', $on='on' ) { // build the returned array of navigation links if ($tipo == 'footer') echo "<hr>\n"; $links = $this->getlinks($all, $on); echo "<table width=100%><tr><td align=right>"; for ($y = 0; $y <= count($links); $y++) { echo $links[$y] . "&nbsp;&nbsp;"; } echo "</td></tr></table>"; if ($tipo == 'header') { echo "<hr>\n"; } } } ?>