Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Binny V A

By: a guest on Mar 21st, 2009  |  syntax: PHP  |  size: 1.52 KB  |  hits: 276  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. /**
  2.  * @author hussein
  3.  * @copyright 2009
  4.  */
  5. //$args = array("table" => array("field1", "field2", "field3"));
  6. $args = array("table1" => "field1",
  7.               "table2" => array("field1", "field2", "field3"),
  8.                           "table3" => "field",
  9.                           "table4" => "field",
  10.                           "table5" => array("fiel1", "fiel2", "fiel3"),
  11.               "table6" => "field2",
  12.                           "table7" => "field",
  13.                           "table8" => "field");
  14. //$args = "not array";
  15. echo s($args);
  16. //echo count($args);
  17. function s($vars, $where=''){
  18.         if(!empty($where)){
  19.         $where = " WHERE ".$where;
  20.         }
  21.         if(is_array($vars)){
  22.                 if(count($vars) > 1){
  23.                         foreach($vars as $table => $fields){
  24.                                 $t = substr($table, "0", "2").$x;
  25.                                 $t_arr[] = $table." AS ".$t;
  26.                                 if(is_array($fields)){
  27.                                         foreach($fields as $field){
  28.                                                 $f_arr[] .= $t.".".$field;
  29.                                         }
  30.                                         $x++;
  31.                                         continue;
  32.                                 }
  33.                                 $f_arr[] = $t.".".$fields;  
  34.                                 $x++;
  35.                         }
  36.                         $f_str = implode(", ",$f_arr);
  37.                         $t_str = implode(", ", $t_arr);
  38.                         if(!is_array($fields)){
  39.                         return $qry = "SELECT ".$f_str." FROM ".$t_str.$where;
  40.                         exit();  
  41.                         }
  42.                 }elseif(count($vars) == 1){
  43.                         foreach($vars as $table => $fields){
  44.                                 if(!is_array($fields)){
  45.                                                 $qry = "SELECT ".$fields." FROM ".$table.$where;
  46.                                 }
  47.                                 if(is_array($fields)){
  48.                                                 $fields_str = implode(", ", $fields);
  49.                                                 $qry = "SELECT ".$fields_str." FROM ".$table.$where;
  50.                                 }
  51.                         }
  52.                 }else{
  53.                         $qry = "An undefined error occured!";
  54.                 }
  55.                 return $qry;
  56.         }else{
  57.                 $error = "Is not an array!";
  58.                 return $error;
  59.         }
  60. }