Location: PHPKode > scripts > dbStringIt > dbstringit/classes/dbStringIt.php
<?php
class dbStringIt {

   /*

dbStringIt Class ver. 0.1
Copyright (c) 2007, Dennis T. Kaplan
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the finalwebsites.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

_________________________________________________________________________
If you need help check this:
http://www.phpclasses.org/browse/package/3648.html


   To constact the db fields

            Call it like this:
            $upd = new dbStringIt;
            $updatev = $upd->dbUpdate($_POST,$db);

      Small modification needed for ezSQL see 2 places below.
   */

   function objectArray($getdb){
      foreach ($getdb[0]  as $k => $v) {
         $db[$k] = $v;
      }
      return $db;
   }

   function dbUpdate($vars,$db){
     // $db = $this->objectArray($db); With ecSQL
      if($vars != $db){
         $intersectvar = $this->key_intersect($vars,$db);
            $newValues    = $this->new_value($intersectvar,$db);
         if($newValues > 0){
            $stringIt     = $this->UpdateStrings($newValues);
         }
      }
      if(isset($stringIt)){
         return $stringIt;
      }
   }

      //Returns only the Variable keys which have a corrosponding field in the db.
   function key_intersect($vars,$db) {

      //$db = $this->objectArray($db); //With ecSQL

      // now we compare our stored variables with the table fields from the db
      // and save the once which exist in both in a new variable.
      foreach ($vars as $k => $v) {
         if (array_key_exists($k, $db)){
            $intersectvar[$k] = $v;
         }
      }
      //We return interecting veriables
      if(isset($intersectvar)){
         return $intersectvar;
      }
   }

   function new_value($intersectvar,$db) {
      foreach ($intersectvar as $k => $v) {
         if ($db[$k] != $v){
            $newValues[$k] = $v;
         }
      }
      if(isset($newValues)){
         return $newValues;
      }
   }

      // Creates the Update fields for the database
   function UpdateStrings($newValues){
      // We call the function key_intersect note the $this->key_intersect to call a function with in the class
      // If we would call a function from another class we would use funcname::key_intersect
      //$this->updatev = $this->key_intersect($vars,$db);

      // We set this variable so we wont get the error variable doesn't exist
      // when the foreach hits that var the first time.
      $string = '';
      if(is_array($newValues)){
         foreach ($newValues as $k => $v) {
            $string .= $k . " = '". $v ."', " ;
         }
         //here we remove the last comma.
         $string = trim($string);
         $last = $string{strlen($string)-1};
         if (!strcmp($last,",")){
            $string = rtrim($string, ',');
         }
         return $string;
      }
   }
}

?>
Return current item: dbStringIt