Location: PHPKode > scripts > GV RecordSet Class > gv-recordset-class/class_RecordSet.php
<?php 
########################################################################
# ---------- File developed by Rúben Doi - hide@address.com ---------- #
# ----------------------- Created on - 10/2007 ----------------------- #
# ------------------------ Class Version : 1.0 ----------------------- #
# -------------------------------------------------------------------- #
# --------------- Object Orientation for PHP - Classes --------------- #
######################################################################## 

# includes
include_once("conf.inc.php");
include_once(INCLUDES_PATH."messages.php");

class RecordSet{
	var $conection;
	var $fields = array();
	var $rs = array();
	var $position = 0;
	var $bof = false; # flag Begin of File
	var $eof = false; # flag End of File
	
	# constructor method of the class. Instanciate a conection object as a class atribute
	function RecordSet($conn){
		$this->conection = &$conn;
	}
	
	# main method, to open, execute and populate a rs with the sql return
	function open($sql){
		switch($this->conection->type){
			case "mysql":
				$this->conection->open_conn();
				$this->conection->executeQuery($sql);
				$this->conection->close_conn();
				# creating the array with the fields names
				$n_fields = mysql_num_fields($this->conection->result);
				for($i=0;$i<$n_fields;$i++){
					$this->fields[] = mysql_field_name($this->conection->result,$i);
				}
				
				# creating the current recordset with the current register
				$temp = array();
				while($row = mysql_fetch_array($this->conection->result)){
					for($i=0;$i<$n_fields;$i++){
						$temp[$this->fields[$i]] = $row[$i];
						
					}
					# inserting new array into the main array
					$this->rs[] = $temp;
				}
				if(sizeof($this->rs) > 0){
					$this->bof = true;
					$this->eof = false;
					$this->position = 0;
				}else{
					$this->bof = true;
					$this->eof = true;
					$this->position = 0;
				}
				break;
			case "mssql":
				# not implemented yet	
				break;
			case "access":
				# not implemented yet
				break;
			# if you have another type of db, just create a new case here.
		}	
	}
	function field($campo){
		if(is_integer($campo)){
			return $this->rs[$this->position][$this->fields[$campo]];
		}else{
			return $this->rs[$this->position][$campo];
		}
	}
	function next(){	
		if ($this->position >= sizeof($this->rs)){
			# end of file, so we don´t do anything
			PrintMessage("Warning : You have reached the end of file. The method next() will stop now.");
		}else{
			$this->position++;
			if ($this->bof){
				$this->bof = false;
			}
			if ($this->position >= sizeof($this->rs)){
				# end of file, so we set the eof flag
				$this->eof = true;
			}
		}
	}
	function previous(){
		if($this->bof){
			# if flag bof is set, don´t do anything
		}else{
			$this->position--;
			if ($this->eof){
				$this->eof = false;
			}
			if($this->position < 0){
				$this->bof = true;
				$this->position = 0;
			}
		}
	}
	function first(){
		$this->bof = true;
		$this->eof = false;
		$this->position = 0;
	}
	function last(){
		$this->eof = true;
		$this->bof = false;
		$this->position = sizeof($this->rs) - 1;
	}
	function count_registers(){
		return sizeof($this->rs);
	}
}
?>
Return current item: GV RecordSet Class