PHP Classes
elePHPant
Icontem

File: objtosql.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Cale O  >  objToSql  >  objtosql.class.php  >  Download  
File: objtosql.class.php
Role: Class source
Content type: text/plain
Description: class file
Class: objToSql
Generate SQL queries from objects or arrays
Author: By
Last change: changed release date typo
Date: 3 years ago
Size: 3,891 bytes
 

Contents

Class file image Download
<?php
/**
* @name objToSql class by Cale Orosz
* @author Cale Orosz
* @email elacdude@gmail.com
* @version 1.0
*
* You are free to use this code free of charge, modify it, and distrubute it,
* just leave this comment block at the top of this file.
*
* Go to phpclasses.org for examples on how to use this class
*
* Changes/Modifications
* 03/09/2011 - version 1.0 released
*
*/





class objToSql {
   
    private
$__tablename;
    public
$__where; //can be a string or an array
   
   

   
public function __construct($tablename) {
       
       
$this->__tablename = $tablename;
       
    }
   
   
    private function
__buildValues() {
       
       
       
$items = array();
        foreach (
$this as $var=> $val) {
           
           
//only pick out the right items from the $_REQUEST array
           
if ($var != "__tablename" && $var != "__where") {
               
$itemname = $var;
               
$itemval = $val;
               
               
//make sure the value is set right, change to null if its blank
                //if ($itemval == "") {
               
if (is_null($itemval)) {
                   
$itemval = "NULL";
                } else {
                   
$itemval = "'" . addslashes($itemval) . "'";
                }
               
               
$items[$itemname] = $itemval;
               
            }
        }
       
        return
$items;
       
    }
   
   
    public function
getValuesFrom($reqvar, $prefix) {
       
        if (!
$reqvar) {
             
$reqvar = $_GET;
        }
       
       
//capture all of the values we want to store. clean the data so its safe to insert into db
       
$items = array();
        foreach (
$reqvar as $var=> $val) {
           
           
//only pick out the right items from the $_REQUEST array
           
           
if (substr($var, 0, strlen($prefix)) == $prefix) {
               
$itemname = substr($var, strlen($prefix));
               
$itemval = $val;
               
               
//make sure the value is set right, change to null if its blank
               
if ($itemval == "") {
                   
$itemval = "";
                } else {
                   
$itemval = $itemval;
                }
               
               
$this->$itemname = $itemval;
               
            }
        }
       
       
    }
   
   
   
    public function
getUpdateSql() {
               
       
$items = $this->__buildValues();
       
       
       
$sql = "UPDATE `" . $this->__tablename . "` set ";
       
        foreach (
$items as $var=>$val) {
            if (
$var != "__tablename") {
               
$sql .= '`' . $var . "`=" . $val . ", ";
            }
        }
       
       
//chop off the extra comma at the end
       
$sql = substr($sql, 0, -2);
       
       
       
//generate where clause
       
if (is_array($this->__where)) {
            if (
count($this->__where) > 0) {
               
               
$sql .= " WHERE ";
               
                foreach (
$this->__where as $wherevar=>$whereval) {
                   
$sql .= '`' . $wherevar . "`='" . addslashes($whereval) . "'";
                   
$sql .= ' and ';
                }
               
               
$sql = substr($sql, 0, -5);
                           
            }
        } elseif (
is_string($this->__where)) {
           
$sql.= " WHERE " . $this->__where;
        }
       
       
        return
$sql;
       
    }
   
   
   
    public function
getInsertSql() {
               
       
$items = $this->__buildValues();
       
       
       
$sql = "INSERT INTO " . $this->__tablename . " (";
       
        foreach (
$items as $eachvar=>$eachval) {
            if (
$eachvar != "id") {
               
$sql .= "`" . $eachvar . "`" . ", ";
            }
        }
       
       
//chop off the extra comma at the end
       
$sql = substr($sql, 0, -2);
       
       
$sql .= ") VALUES (";
       
        foreach (
$items as $eachvar=>$eachval) {
            if (
$eachvar != "id") {
               
$sql .= $eachval . ", ";
            }
        }
       
$sql = substr($sql, 0, -2);
       
$sql .= ")";
   
        return
$sql;
       
    }
   
   
    public function
insert($hide_errors=true) {
       
       
$sql = $this->getInsertSql();
       
$rs = mysql_query($sql);
       
        if (
$hide_errors == false) {
            if (!
$rs) {
                echo
mysql_error;
            }
        }
        return
$rs;
    }
   
    public function
update($hide_errors=true) {
       
       
$sql = $this->getUpdateSql();
       
$rs = mysql_query($sql);
       
        if (
$hide_errors == false) {
            if (!
$rs) {
                echo
mysql_error();
            }
        }
        return
$rs;
    }
       
   
}



?>