Location: PHPKode > scripts > UtilsClass > utilsClass.inc
<?php
/**
 * utilsClass.inc version 0.1 copyright(C) 2002 Fiorenza Caetani
 * 
 * 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. 
 * 
 * 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. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA 
 * 
 * Fiorenza Caetani [webmaster at larosmarina.it]
 * 
 * Utils class for phpLib, provide 4 methods to select, insert, update and delete 
 * data from database. It requires phpLib properly installed. Refer to the 
 * documentation with this file for further instructions (but it's so simple!). 
 * 
 * For more information about phpLib and how to use it, 
 * you can look at http://sourceforge.net/projects/phplib/ 
 * 
 */


class Utils {
/**  
 * Utils::Utils()
 * 
 * @param $dbclass string required, name of your extension of the db class used by phpLib
 * @param $debug int optional, if == 0, all queries are executed and results returned, if == 1 no queries are executed, the methods return the query they built
 *  
 * Ini method of the class, you use this to pass the needed parameters to the 
 * class. 
 * The Utils class want the name of your extension of the db_sql class 
 * (var $dbclass string) to use it for the queries of each method. 
 * You can set $debug to 1: in this case, no queries will be 
 * executed, the invoked method simply will returns the query 
 */
	function Utils($dbclass, $debug = 0)
		{
		$this->db = new $dbclass;
		$this->debug = $debug;
		}

/**
 * Utils::do_select() 
 * 
 * @param $tb_name array with name(s) of table(s)
 * @param $fields array with name(s) of field(s)
 * @param $cond string with the condition of the select query (optional - default: "where 1")
 * @return array $my_array ($this->debug = 0) or string $query ($this->debug = 1)
 */
	
	function do_select($tb_name = array(), $fields = array(), $cond = 'where 1'){
	$my_array = array();
	$num_fields = count($fields);
	$num_tables = count($tb_name);
	for($i = 0; $i < $num_fields; $i++)
		{
		$field.=$fields[$i];
		if($i < $num_fields-1)
			{
			$field.=", ";
			}
		}
	for($i = 0; $i < $num_tables; $i++)
		{
		$table.=$tb_name[$i];
		if($i < $num_tables-1)
			{
			$table.=", ";
			}
		}
	$query = "select ".$field." from ".$table." ".$cond;
	$this->db->query($query);
	while($this->db->next_record())
		{
		for($i = 0; $i < $num_fields; $i++)
			{
			$name = $fields[$i];
			$my_var = stripslashes($this->db->f("$name"));
			$do = array_push($my_array,$my_var);
			}
		}
	if($this->debug == 0)
		{
		return $my_array;
		}
	else if($this->debug == 1)
		{	
		return $query;
		}
	}
	

/**
 * Utils::do_update()
 * 
 * @param $tb_name string required, name of table on wich the function do the update
 * @param $fields array required, fields to update
 * @param $values array required, values to put in the fields, need to have the same order of the array $fields. ATTENTION: you need to properly use apixes here!!
 * @param $cond string required, the where condition of the query
 * @return ($this->debug = 0) $my_res, int, if == 1, query succeded, if == 2, query failed
 * @return ($this->debug = 1) $query, string, the query composed by the function
 * 
 * 
 */
	function do_update($tb_name, $fields = array(), $values = array(), $cond){
	$my_res = '';
	$num_fields = count($fields);
	for($i = 0; $i < $num_fields; $i++)
		{
		$field = $fields[$i];
		$value = $values[$i];
		 
		if($i < $num_fields-1)
			{
			$value.=", ";
			}
		$sub_query .= $field." = ".$value;
		}
	$query ="update ".$tb_name." set ".$sub_query." ".$cond;
	
	if($this->debug == 0)
		{
		$this->db->query($query);
		if($this->db->affected_rows() > 0)
			{
			$my_res = 1;
			}
		else
			{
			$my_res = 2;
			}
		return $my_res;
		}
	else if($this->debug == 1)
		{
		return $query;
		}
	
	}

/**
 * Utils::do_insert() 
 * 
 * @param $tb_name, string required, name of the table do the insert 
 * @param $fields, array required, fields where to insert the values 
 * @param $values, array required, values to insert in the table, need to have the same order of the array $fields. ATTENTION: you need to properly use apixes here!!
 * @return ($this->debug = 0) $my_res, int, if == 1, query succeded, if == 2, query failed
 * @return ($this->debug = 1) $query, string, the query composed by the function 
 */
	function do_insert($tb_name, $fields = array(), $values = array()){
	$my_res = '';
	$num_fields = count($fields);
	$num_values = count($values);
	for($i = 0; $i < $num_fields; $i++)
		{
		$field .= $fields[$i];
		if($i < $num_fields-1)
			{
			$field.= ", ";
			}
		}
	for($i = 0; $i < $num_values; $i++)
		{
		$value .= $values[$i];
		if($i < $num_values-1)
			{
			$value.= ", ";
			}
		}
	$query = "insert into ".$tb_name." (".$field.") values (".$value.")";
	if($this->debug == 0)
		{
		$this->db->query($query);
		if($this->db->affected_rows() > 0)
			{
			$my_res = 1;
			}
		else
			{
			$my_res = 2;
			}
		return $my_res;	
		}
	else if($this->debug == 1)
		{
		return $query;
		}
	}

/**
 * Utils::do_delete() 
 * 
 * @param $tb_name string required, name of the table on wich the function do the delete
 * @param $cond string required, the where condition of the query
 * @return ($this->debug = 0) $my_res int, if == 1: the query succeded; if == 2: the query failed; 
 * @return ($this->debug = 1) $query string, the query composed by the function
 */
	function do_delete($tb_name, $cond){
	$my_res = '';
	$query = "delete from ".$tb_name." ".$cond;
	if($this->debug == 0)
		{
		$this->db->query($query);
		if($this->db->affected_rows() > 0)
			{
			$this->db->query("optimize table ".$tb_name);
			$my_res = 1;
			}
		else
			{
			$my_res = 2;
			}
		return $my_res;
		}
	else if($this->debug == 1)
		{
		return $query;
		}
	}
			
}
?>
Return current item: UtilsClass