Location: PHPKode > projects > Webgenerator-X content management system > wgx_rc1.5/WG-X/class.DataLayer.php
<?
/*
* This file is part of Webgenerator-X,
* an object oriented website management engine working an top of
* Apache/PHP4/MySQL.
* http://www.webgenerator-x.com
* @2001 REGNI Giorgio
* hide@address.com
*
* Webgenerator-X 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.
*
* Webgenerator-X 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 Webgenerator-x; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*************************************************************************/
/* REGNI Giorgio
   2/05/2001
   WG-X
   Datamangement: class DataLayer
   Common way to access data in the MySQL database
   Developers don't need to write XML code but just uses thoses classes
   to retrieve and set data.
*/

/**

This class takes an array (datastruct) that explains the data in an sql table
and then ease the access to records by providing common interfaces that
automatically handle language etc...

datasctruct is like:
sql_filed_name=>type

type can be TEXT

*/
class A_DataLayer {

  var $struct;
  var $table;
  var $res; // mysql result
	
  function A_DataLayer()
    {
      $datastruct=array();
    }

  /**
   Set the table to use ( do not need a db_prefix)
   */
  function set_table($table)
    {
      $this->table=table;
    }

  /**
Explain
   */
  function set_table_struct($def)
    {
      $this->struct = $def;
    }

  /**
   Get data from the database.
   $array indicates what data to get ( array of field_names )
   while $where what to get ( array of field_name => value )
   If $lang is not set the actual languages is used. 
  
   return FALSE if an error occured, TRUE else
   The data can be acceded through get_next_result.
  */
  /* Warning: no error checking (must be fast) */
  function get_data($what,$where,$lang="")
    {
      global $language,$db_prefix;
      if ($lang=="")
	$lang=$language;

      // construct the sql request
      $sql = 'select '.implode(',',$what);
      $sql.= " from $db_prefix"."$this->table";
      
      if (is_array($where))
	if (count($where)>0)
	  {
	    $sql.=' where 1';
	    while(list($id,$key)=each($where))
	      {
		$sql.= " and $id='$key'";
	      }
	  }

      $this->res= mysql_query($sql);
      if (mysql_errno()==0)
	return TRUE;
      else
	return FALSE;
    }

  /**
  Get the next result row from a previous get call
   */
  function get_next_result()
    {
      return mysql_fetch_row($this->res);
    }

   /**
   Free a previous get result
   */
  function free_result()
    {
      return mysql_free_result($this->res);
    }
  
}
	
$DATALAYER = new A_DataLayer();
?>
Return current item: Webgenerator-X content management system