Location: PHPKode > projects > RabbitBB - Online Web Storage Project > mywebdrive/PHP_CON/common/SQLSelect.php
<?PHP

// Version 0.1.2.2

class SQLSelect
{
	var $select_var_arr;
	var $select_other;
	var $table_arr;
	var $relation_arr;
	var $where_arr;
	var $order_arr;
	var $limit_arr;
	var $group_arr;
	var $have_arr;
	var $result;

	//var $tableprefix = array("a", "b", "c", "d", "e", "f");

	function SQLSelect()
	{
		$this->select_var_arr = array();
		$this->select_other = array();
		$this->table_arr = array();
		$this->relation_arr = array();
		$this->where_arr = array();
		$this->order_arr = array();
		$this->have_arr = array();
		$this->limit_arr = "";
		$this->group_arr = "";

		$this->result = array();
	}

	function setSelect($_selectvar, $_select_table)
	{
		// $_selectvar can be array or plain text

		$found = false;
		for ($i = 0; $i<count($this->table_arr); $i++)
		{
			if ($_select_table == $this->table_arr[$i])
			{
				$found = true;
			}
		}
		if ($found==false)
		{
			$this->table_arr[] = $_select_table;
		}
		$this->putSelect($_select_table, $_selectvar);
	}

	function setSelectOther($_select_other)
	{
		$this->select_other[] = $_select_other;
	}

	function putSelect($_tableprefix, $_select_var)
	{
		if (!is_array($_select_var))
		{
			$this->select_var_arr[] = $_tableprefix . "." . $_select_var;
		}
		else
		{
			for ($i=0; $i<count($_select_var); $i++)
			{
				$this->select_var_arr[] = $_tableprefix . "." . $_select_var[$i];
			}
		}
	}

	function setRelation($_statement)
	{
		$this->relation_arr[] = $_statement;
	}

	function setWhere($_statement)
	{
		$this->where_arr[] = $_statement;
	}

	function setOrder($_statement)
	{
		$this->order_arr[] = $_statement;
	}

	function setLimit($_start, $_end)
	{
		$this->limit_arr = "LIMIT " . $_start . "," . $_end;
	}

	function setGroup($_statement)
	{
		$this->group_arr = $_statement;
	}

	function setHave($_statement)
	{
		$this->have_arr[] = $_statement;
	}

	function SQLReturn()
	{
		$sqlq = "SELECT ";
		$sqlq .= $this->createAsString($this->select_var_arr);
		$sqlq .= $this->createOtherString($this->select_other);
		$sqlq .= "FROM ";
		$sqlq .= $this->createString($this->table_arr, ",");
		if (count($this->where_arr)>0)
		{
			$sqlq .= "WHERE ";
			$sqlq .= $this->createString($this->where_arr, " AND");
		}

		if ($this->group_arr!="")
			$sqlq .= " GROUP BY " . $this->group_arr . " ";

		if (count($this->have_arr)>0)
		{
			$sqlq .= "HAVING ";
			$sqlq .= $this->createString($this->have_arr, " AND");
		}
		
		if (count($this->order_arr)>0)
		{
			$sqlq .= "ORDER BY ";
			$sqlq .= $this->createString($this->order_arr, ",");
		}
;
		if ($this->limit_arr!="")
			$sqlq .= $this->limit_arr;
		return $sqlq;
	}

	function createOtherString($_select_other)
	{
		$sqlq = "";
		for ($i=0; $i<count($_select_other); $i++)
		{
			$sqlq .= ",";
			$sqlq .= $_select_other[$i] . " ";
		}
		return $sqlq;
	}

	function createString($_var, $_separator)
	{
		$sqlq = "";
		for ($i = 0; $i<count($_var); $i++)
		{
			$sqlq .= $_var[$i];
			if ($i< (count($_var)-1))
			{
				$sqlq .= $_separator . " ";
			}
			else
				$sqlq .= " ";	
		}
		return $sqlq;
	}

	function createAsString($_var)
	{
		$_separator = ",";
		$sqlq = "";
		for ($i = 0; $i<count($_var); $i++)
		{
			$sqlq .= $_var[$i];
			list($tbname, $tbvar) = explode(".", $_var[$i]);
			$sqlq .= " AS ". $tbname . "__" . $tbvar;
			if ($i< (count($_var)-1))
			{
				$sqlq .= $_separator . " ";
			}
			else
				$sqlq .= " ";	

		}
		return $sqlq;
	}

	function getRow($db, $sqlq)
	{
		$tres = array();
		if ($sqlq == "")
			return $tres;
		if ($db->sql_query($sqlq))
		{
			
			$trow = true;
			if ($db->sql_numfields()>0)
			{
				$row_i = 0;
				while ($trow!=false)
				{
					$trow = $db->sql_fetchrow();
					if ($trow!=false)
					{
						while (list($key, $var) = each($trow))
						{
							list ($tablename, $field) = explode("__", $key);
							
							if (isset($tres[$tablename]))
							{
								$ttres = $tres[$tablename];
							}
							else
								$ttres = array();
							if (!isset($ttres[$row_i]))
								$ttres[$row_i] = array();
							$ttres[$row_i][$field] = $var;

							$tres[$tablename] = $ttres;
						}
					}
					$row_i++;
				}
			}
			$db->sql_freeresult();
		}
		$this->result = $tres;
		return $tres;
	}

	function getErrorStr($db)
	{
		return $db->sql_error();
	}
}

?>
Return current item: RabbitBB - Online Web Storage Project