Login   Register  
PHP Classes
elePHPant
Icontem

File: recordset.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Felver Yepez  >  Recordset  >  recordset.php  >  Download  
File: recordset.php
Role: ???
Content type: text/plain
Description: .php file
Class: Recordset
Author: By
Last change:
Date: 2001-03-19 13:53
Size: 6,201 bytes
 

Contents

Class file image Download
<?
	  class Recordset 
	  { 
			var $result;     // result id 
			var $rowcount;   // number of rows in result 
			var $curpos;     // index of current row (begin=1, end=rowcount) 
			var $fieldcount; // number of fields in result 
			var $fn;         // Array of fields names 
			var $rowset;     // Array of fields with keys on field name
			var $connection; // connection id 
			var $sql;        // sql query 

			var $error;      // error variable
			var $maxrow;     // total number of rows displayed in a page
			var $totalpages; // total number of pages


			//Constructor 
			function Recordset($Conn, $Sql) 
			{ 
				$this->result=0;
				$this->maxrow=5; // number of maxrows by default
				$this->connection=$Conn;
				$this->sql=$Sql;
				$this->fn=array();
				$this->rowset=array();
				$this->Query();
			}

			// Execute query 
			function Query()
			{ 			
				$this->Close();

				$this->result = odbc_exec($this->connection, $this->sql);
				
				if (!$this->result) 
				  return(0); 												
				
				// some ODBC drivers return -1 so we count it(very slow) 
				$rows = odbc_num_rows($this->result); 
				if ($rows == -1) { 
					$rows = 0; 
					while (odbc_fetch_row($this->result)) { 
						$rows++; 
					} 
				} 				
				$this->rowcount = $rows; 
				$this->fieldcount = odbc_num_fields($this->result); 
				
				for ( $i = 1; $i <= $this->fieldcount; $i++ ) 
				{ 
				   // Fill fields names array 
				   $this->fn[$i] = odbc_field_name($this->result, $i);
				}

				$this->curpos = 0; 

				$this->totalpages=(int)(($this->rowcount - 1)/$this->maxrow + 1);
			} 

			function MoveTo($number)
			{
				if($number < 0 || $number > $this->rowcount)
				{
					$this->error="MoveTo error";
					return(0);
				}
				else
					$this->curpos = $number; 	
			}

			//Move to first record 
			function MoveFirst()
			{ 
				$this->curpos = 1; 
			} 

			//Return current row elements and move to next record 
			function MoveNext()
			{ 
				if (!$this->result) {
					return(0); 
				}

				if ($this->curpos == $this->rowcount)  {
					return(0); 
				}
				
				if ( odbc_fetch_row( $this->result, $this->curpos + 1) )
				{
					for($i = 1; $i <= $this->fieldcount; $i++) 
					  $this->rowset[$this->fn[$i]] = odbc_result( $this->result, $this->fn[$i] ); 

					$this->curpos++; 

					return($this->rowset); 
				}
				else
					return(0);
			} 

			//Return true if last record 
			function Eof()
			{ 
				if ($this->curpos == $this->rowcount) 
					return(1); 
				return(0); 
			} 

			//Return true if first record 
			function Bof() 
			{ 
				if (!$this->curpos) 
					return(1); 
				return(0); 
			} 

			// Free result if exist 
			function Close()
			{ 
				if ($this->result && $this->rowcount)  
					odbc_free_result($this->result); 

				$this->result=0; 
				$this->fn=array(); 
				$this->rowset=array(); 
				$this->rowcount=0; 
				$this->fieldcount=0; 
				$this->table=""; 
			} 

			// number of records that return the Query()
			function RecordCount()
			{
				if (!$this->result)
					$count = -1; 
				else
					$count = $this->rowcount;

				return $count;
			}

			// Function that split the results in to an array
			function Split($ppage)
			{
				if (!$this->result) {
					return(0); 
				}

				if ($this->curpos == $this->rowcount)  {
					return(0); 
				}

				//in this method only the present page to be displayed will be passed
				//if present page is null the first $maxrows will be printed
				if(($ppage=="")|| ($ppage <=0))
				{
					//if the ppage passed is null or zero or less than zero we
					//show the first page
					$min=0;
					//printf("ppage is null or 0 or less than 0<br>");
				}
				else
				{				
					if($ppage > $this->totalpages)
					{
						//if the ppage passed is more than the total pages
						//we display the last page
						$min=$this->maxrow * ($this->totalpages-1);
						//printf("ppage is > total pages<br>");				
					}
					else
					{
					//if present page is passed the max and min limit is calculated
					//eg:-if ppage is 2 and maxrow=10 then records from 10 to 20 will be displayed.
						$ppage=$ppage-1;
						$min=$ppage*$this->maxrow;
						$max=$this->maxrow;													
					}
				}

				$i = 0;
				$recordnr = $min + 1;
				while( odbc_fetch_row($this->result, $recordnr) && $i < $this->maxrow )
				{
					for($j = 1; $j <= $this->fieldcount; $j++) 
						$outputdisplay[$i][$this->fn[$j]] = odbc_result( $this->result, $this->fn[$j] );

					$i++;
					$recordnr++;
				}
							 
				if($this->rowcount == 0)
				{
					$this->error="No results";
					exit;
				}
				else
				{
					return $outputdisplay;				
				}						
			}

			function Elems($nval)
			{
				$this->maxrow = $nval;
				$this->totalpages=(int)(($this->rowcount - 1)/$this->maxrow + 1);
			}

			// return total number of pages
			function TotalPages()
			{			
				return $this->totalpages;
			}

			// display the back, next links and position links
			function DisplayGuide($href_page, $ppage)
			{
				$pages = $this->TotalPages();

				echo "<table border=\"0\">\r";
				echo "<tr>\r";
				if(($ppage=="")||($ppage<="0") )
				{
					$ppage=1;	//as default if the requested page is less than 0 the first page is printed
				}	
				if($ppage > $pages)
				{
					$ppage=$pages;  //if the requested page is greater than total pages the last page is shown
				}	
				if($ppage>1)
				{
					$backpage=$ppage-1;	
						printf("<td><a href=$href_page?page=$backpage>Back</a></td>\r");

				}
				//echo $pages;
				for($j=1;$j<=$pages;$j++)
				{
					if($j==$ppage)
					{
						echo "<td align=\"center\"><b>" . $j ."</b></td>\r";
					}
					else
					{
								printf("<td align=\"center\"><a href=$href_page?page=$j>$j</a></td>\r");
					}		
				}
				if($ppage<$pages)
				{
					$nextpage=$ppage+1;	
						printf("<td><a href=$href_page?page=$nextpage>Next</a></td>\r");

				}
				echo "</tr>\r";
				echo "</table>\r";
			}
	} 
?>