Login   Register  
PHP Classes
elePHPant
Icontem

File: System.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Richard Bartholf  >  ecSQL  >  System.php  >  Download  
File: System.php
Role: ???
Content type: text/plain
Description: The Data tier of the class.
Class: ecSQL
Author: By
Last change:
Date: 2001-09-09 15:51
Size: 5,222 bytes
 

Contents

Class file image Download
<?php
/*
Copyright 2001 Rikard Bartholf <rikard@bartholf.nu>

This file is part of ecSQL.

ecSQL 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.

ecSQL 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.

You should have received a copy of the GNU General Public License
along with ecSQL; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
?>
<?php
/**
 * This is two example classes that should be altered in order to fit another
 * table structure. Only contained here for reference.
 * This is a sort of stored procedures that is found on many other database
 * servers but not mySQL.
 */
class SysRecipe extends sysGlobals {
	var $cColl;
	function prepareByID($inID) {
		$aQuery = "	SELECT *
					FROM RECIPES
					WHERE RECIPE_ID = $inID";
		$aRS = mysql_query($aQuery);
		$this->cColl = $this->makeCollectionOfTypes($aRS, "Recipe");
	}
	function prepareAll() {
		$aQuery = "	SELECT *
					FROM RECIPES
					ORDER BY NAME";
		$aRS = mysql_query($aQuery);
		$this->cColl = $this->makeCollectionOfTypes($aRS, "Recipe");
	}
}

class SysIngredient extends sysGlobals {
	var $cColl;
	function prepareByID($inID) {
		$aQuery = "	SELECT *
					FROM INGREDIENTS
					WHERE INGREDIENT_ID = $inID";
		$aRS = mysql_query($aQuery);
		$this->cColl = $this->makeCollectionOfTypes($aRS, "Ingredient");
	}
	function prepareByRecipeId($inRecipeId) {
		$aQuery = "	SELECT *
					FROM INGREDIENTS
					WHERE RECIPE_ID = '" . $inRecipeId ."'
					ORDER BY INGREDIENT_ID";
		$aRS = mysql_query($aQuery);
		$this->cColl = $this->makeCollectionOfTypes($aRS, "Ingredient");
	}
}




/**
 * This is a class which allows "browsing" an entire Coll with standard methods
 * such as ->moveNext().
 * Example:
 *
 *  $cRecipeObj = new sysRecipe();
 *  $cRecipeObj->prepareAll();
 *  $cBrowser = $cRecipeObj->getBrowser();
 *
 * This code prepares all the recipes we have in our database. We call the method
 * ->getBrowser() which returns a sysBrowser, loaded with the Coll that was last
 * created. Use it like this later on in the code:
 *
 * 	while ( $cBrowser->moveNext() ) {
 *		echo $cBrowser->getValue("NAME") . "<br>";
 *	}
 *
 * @author	Rikard Bartholf <rikard@bartholf.nu>
 */
class sysBrowser {
	var $cBrwsColl;
	var $cIndex;
	var $cIndex2;
	
	/**
	 * Returns true or false if the index is at the end of the Coll or not.
	 */
	function eof() {
		return ($this->cIndex > $this->cnt()) ? 1 : 0;
	}
	
	/**
	 * Sets $cBrwsColl to supplied $inColl.
	 *
	 * @param	$inColl	This is the collection the class will be working with.
	 */
	function setColl($inColl) {
		$this->cBrwsColl = $inColl;
		$this->cIndex = 0;
		$this->cIndex2 = 0;
	}
	function moveNext() {
		if ( !$this->eof() ) {
			$this->cIndex2 = $this->cIndex;
			$this->cIndex++;
			return true;
		} else {
			return false;
		}
	}
	function moveFirst() {
		$this->cIndex = 0;
		$this->cIndex2 = 0;
	}
	function cnt() {
		return count($this->cBrwsColl)-1;
	}
	
	/**
	 * Returns the value for the given key. In order for the ->moveNext() function
	 * to behave as it should we need two index variables.
	 *
	 * @param	$inKey
	 */
	function getValue($inKey) {
		return $this->cBrwsColl[$this->cIndex2]->getValue($inKey);
	}
	
	/**
	 * A very important function that returns the whole object instead of just the value
	 * of a given key. Required before a call to both the ->store() and ->remove()
	 * methods of the type in question.
	 *
	 * @return	A Type
	 */
	function getObject() {
		return $this->cBrwsColl[$this->cIndex2];
	}
}




/**
 * Generic functions that is used in all "Typemanagers" created above.
 *
 * @author	Rikard Bartholf <rikard@bartholf.nu>
 */
class sysGlobals extends sysBrowser {
	/**
	 * This is one of the most important functions of the class. It converts a
	 * recordset into a Collection of types.
	 *
	 * @param	$inRS	The recordset that is to be converted into a Collection.
	 * @param	$inType	Typename of the class that is to be created from each row
	 *					of the recordset.
	 */
	function makeCollectionOfTypes($inRS, $inType) {
		$aColl = array();
		while($out = mysql_fetch_assoc($inRS)) {
			eval("\$cObj = new $inType;");
			$cObj->init();
			while(list($key,$val) = each($out)) {
				$cObj->add($key,$val);
			}
			array_push($aColl, $cObj);
		}
		return $aColl;
	}

	function cnt() {
		return count($this->cColl);
	}
	
	function getColl(){
		if( count($this->cColl) ) {
			return $this->cColl;
		} else {
			return 0;
		}
	}
	
	/**
	 * Returns a sysBrowser that is prepared with the most recent Coll.
	 */
	function getBrowser() {
		$aBrowser = New sysBrowser();
		$aBrowser->setColl($this->cColl);
		return $aBrowser;
	}
}




?>