Location: PHPKode > scripts > MSSQL Pagination > mssql-pagination/my_pagina_class.php
<?php
/************************************************************************
mssql_pagina_class ver. 1.00
Use this class to handle MSSQL record sets and get page navigation links 

Copyright (C) 2008 - Fahad Waheed Khan

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

_________________________________________________________________________
Comments & suggestions: hide@address.com

*************************************************************************/



require($_SERVER['DOCUMENT_ROOT']."/mssqlClass/db_config.php");
error_reporting(E_ALL); // only for testing

class MssqlPagina {
	
	
	var $sql;
	var $result;
	
	var $get_var = QS_VAR;
	var $num_per_page = NUM_ROWS;		//Records per page
	
	var $tot_num_rows;		//Total number of rows
	var $page_no;			//Current page no
	var $num_of_pages;		//No of Pages
	var $seek;			//Position where we have to display records
	
	
	
	// constructor
	function MssqlPagina() {
		$this->connect_db();
	}
	
	
	// database connection
	function connect_db() {
		$conn_str = mssql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
		mssql_select_db(DB_NAME, $conn_str);
	}
	
	
	// sets the current page number
	function set_page() {
		$this->page_no = (isset($_REQUEST[$this->get_var]) && $_REQUEST[$this->get_var] != "") ? $_REQUEST[$this->get_var] : 0;
		return $this->page_no;
	}
	
	
	// gets the total number of records 
	function get_total_rows() {
		$tmp_result = mssql_query($this->sql);
		$this->tot_num_rows = mssql_num_rows($tmp_result);
		mssql_free_result($tmp_result);
		return $this->tot_num_rows;
	}
		
	
	// get the total number of result pages
	function get_num_pages() {
		$this->num_of_pages = ceil($this->get_total_rows() / $this->num_per_page);
		return $this->num_of_pages;
	}
	
	
	
	
	// returns the records for the current page
	function get_page_result() {
	
	if( $this->set_page() != 0 ){
		$this->seek = $this->set_page() * $this->num_per_page;
		//$seek++;
		}
		
		$this->result = mssql_query($this->sql);
		mssql_data_seek($this->result, $this->seek);
		//return $this->result;
		
		for($rv=1;$rv<=$this->num_per_page;$rv++){
		$row=mssql_fetch_row($this->result);
		$returnarray[]=$row;
		//echo "{$row[0]}-{$row[1]}<br>";
		}
		
		return $returnarray;
		
		
		
	}
	
	
		// function to handle other querystring than the page variable
	function rebuild_qs($curr_var) {
		if (!empty($_SERVER['QUERY_STRING'])) {
			$parts = explode("&", $_SERVER['QUERY_STRING']);
			$newParts = array();
			foreach ($parts as $val) {
				if (stristr($val, $curr_var) == false)  {
					array_push($newParts, $val);
				}
			}
			if (count($newParts) != 0) {
				$qs = "&".implode("&", $newParts);
			} else {
				return false;
			}
			return $qs; // this is your new created query string
		} else {
			return false;
		}
	} 
	
	
	// function to show navigation
	function show_navigation($displayname = "Page"){
	$var = $this->get_var;
	
	for($PageCount=0;$PageCount<$this->get_num_pages();$PageCount++)
		{
		echo " | ";
		echo "<a href=\"".$_SERVER['PHP_SELF']."?".$this->get_var."=".$PageCount.$this->rebuild_qs($var)."\">$displayname ".($PageCount+1)."</a>";	
		echo " | ";
		}
	}
	
	// free the database result
	function free_page_result() {
		mssql_free_result($this->result);
	}
	

}
?>
Return current item: MSSQL Pagination