Location: PHPKode > scripts > PHP 4 Applications > php-4-applications/p4a_datasource.php
<?php

class p4a_DataSource
{
	var $db;
	var $dbname;
	var $user;
	var $pass;
	var $table;
	var $filter;
	var $order;
	var $pointer;
	var $primary_key;

	function p4a_DataSource( $_db, $_us, $_pa, $_ta)
	{
		$this->dbname = $_db;
		$this->user = $_us;
		$this->pass = $_pa;

		$this->table = $_ta;

		$this->pointer = 0;
		$this->order = '';
		$this->filter = '';

		$this->primary_key = $this->getPrimaryKey();
	}

	function reconnect()
	{
		$this->db = mysql_connect('localhost',$this->user,$this->pass);
		if (!$this->db)
		{
			echo '<h2>¬°¬°Error en la conexion a la base de datos!!</h2><p/>';
			echo mysql_error();
			exit;
		}
		mysql_select_db($this->dbname, $this->db);
	}

	function getPrimaryKey()
	{
		$this->reconnect();

		$sql = "show fields from ".$this->table;
		$result = mysql_query( $sql);
		$num_results = mysql_num_rows($result);

		$matrix='';
		for($i=0;$i<$num_results;$i++)
		{
			$record = mysql_fetch_array( $result, MYSQL_ASSOC);
			if ($record['Key']=='PRI')
				$matrix[$record['Field']] = 1;
		}

		return $matrix;
	
	}

	function getDBConnection()
	{
		return $this->db;
	}

	function movePointer( $_p)
	{
		$this->pointer = $_p;
	}

	function getPointer()
	{
		return $this->pointer;
	}

	function setOrder( $_o)
	{
		$this->order = $_o;
	}

	function setFilter( $_fil)
	{
		$this->filter = $_fil;
		return $this->getRecords(0);
	}

	function update( $_matrix_asoc)
	{
		$this->reconnect();

		$data = "";

		$data = $this->getRecords( $this->pointer-1);

		$sql = "update ".$this->table." set ";
		foreach( $_matrix_asoc as $k => $v)
			$sql .= $k."='".addslashes($v)."',";
		$sql = substr( $sql, 0, strlen( $sql)-1);

		$sql .= " where ";
		foreach( $this->primary_key as $c => $v)
			$sql .= $c."='".addslashes($data[0][$c])."' AND ";
		$sql = substr( $sql, 0, strlen( $sql)-4);

		$result = mysql_query( $sql);
		$num_results = mysql_num_rows($result);

		if ($num_results>0)
			return 1;
		else
			return 0;
		
	}

	function insert( $_matrix_asoc)
	{
		$this->reconnect();

		$sql = "insert into ".$this->table." (";
		foreach( $_matrix_asoc as $k => $v)
			$sql .= $k.",";
		$sql = substr( $sql, 0, strlen( $sql)-1);
		$sql .= ") values(";
		foreach( $_matrix_asoc as $k => $v)
			$sql .= "'".addslashes($v)."',";
		$sql = substr( $sql, 0, strlen( $sql)-1);
		$sql .= ")";

		$result = mysql_query( $sql);
		$num_results = mysql_num_rows($result);

		if ($num_results>0)
			return 1;
		else
			return 0;
		
	}

	function delete()
	{
		if ($this->pointer==0)
			return 0;

		$this->reconnect();

		$data = "";

		$data = $this->getRecords( $this->pointer-1);
		if (!empty($data))
		{
			$sql = "delete from ".$this->table;
			$sql .= " where ";

			foreach( $this->primary_key as $c => $v)
				$sql .= $c."='".addslashes($data[0][$c])."' AND ";
			$sql = substr( $sql, 0, strlen( $sql)-4);

			$result = mysql_query( $sql);
			$num_results = mysql_num_rows($result);
		}

		if ($num_results>0)
			return 1;
		else
			return 0;
	}

	function getRecords( $_ini, $_num=1)
	{
		$this->reconnect();

		if ($_ini<0)
			$_ini = 0;

		$sql = "select * from ".$this->table;

		if (!empty($this->filter))
			$sql .= " where ".$this->filter;
		if (!empty($this->order))
			$sql .= " order by ".$this->order;
					
		$sql .= " limit ".$_ini.",".$_num;

		$result = mysql_query( $sql);
		$num_results = mysql_num_rows($result);

		$this->pointer = $_ini + $num_results;

		$asoc_matrix='';
		for($i=0;$i<$num_results;$i++)
		{
			$record = mysql_fetch_array( $result, MYSQL_ASSOC);
			foreach( $record as $k => $v)
				$asoc_matrix[$i][$k] = stripslashes($v);
		}

		return $asoc_matrix;
	}

}

?>
Return current item: PHP 4 Applications