Beispiel #1
0
 /**
  * @return mixed
  */
 function quickQuery()
 {
     //get the number of function arguments
     $numberOfArgs = func_num_args();
     //get all items in function
     $argsList = func_get_args();
     $items = null;
     $itemsForBinding = null;
     // define what each index of an array is
     $statementOptions = array('db' => '', 'table' => '', 'fields' => '', 'values' => '');
     //merge define array with user input to have array indexes and values
     $statementItems = array_merge($statementOptions, end($argsList));
     self::$dbInfo = $statementItems['db'];
     $table = $statementItems['table'];
     //fields used for database fields
     $fields = $statementItems['fields'];
     $start = "<div style='width: 100%; margin: 0 auto; padding: 2%;box-shadow: 0 0 5px #ccc; height: auto; color:#ff4722'>";
     $end = "</div>";
     if ($numberOfArgs > 0) {
         for ($i = 0; $i < $numberOfArgs; $i++) {
             //              $items .= ":".str_replace(' ','x_AcvSp_x',$argsList[$i]).',';
             if ($i > 0) {
                 $items .= ":" . $this->generateRandomString() . ',';
             }
             $itemsForBinding .= $argsList[$i] . "~";
         }
     }
     //remove the word array from the parameter, it's not needed as it is been passed
     $item_modified = str_replace(',:Array,', "", $items);
     $itemsForBinding_modified = str_replace('Array', "", $itemsForBinding);
     //add a comer to the list to be used as a binding value
     $imploded_Items = implode(', ', explode(',', substr(implode(', ', explode(' ', $item_modified)), 0, -1)));
     $itemsForBinding_modified_exploaded = explode('~', substr($itemsForBinding_modified, 0, -2));
     $connect = $this->connect();
     $stmt = $connect->prepare("INSERT INTO {$table} ({$fields})\n                            VALUES ({$imploded_Items})");
     //        array(3) { [0]=> string(6) "ite_m1" [1]=> string(7) "it__em2" [2]=> string(6) "it_em3" }
     $fieldsForBidning = explode(",", substr(implode("", explode(":", $item_modified)), 0, -1));
     //join both the the fields and value(field will be used as the key and value as the value)
     $combined_arrays = array_combine($fieldsForBidning, $itemsForBinding_modified_exploaded);
     $i = 0;
     $final_combine = '';
     foreach ($combined_arrays as $combined_keys => $combined_values) {
         $final_combine .= "\n" . "\$" . $combined_keys . "='" . $combined_values . "';" . "\n";
         $i++;
     }
     $bindPara = "";
     foreach ($combined_arrays as $k => $v) {
         //bind the values and fields here
         $bindedItem = ":" . $k;
         $bindedValue = "\$" . $k;
         $bindPara .= '$stmt->bindParam(' . "'{$bindedItem}'" . ',' . $bindedValue . ')' . ";" . "\n";
     }
     $file = 'php/classes/db/db.bindParam.php';
     if (!empty($final_combine)) {
         //open file to write to
         $openFileToWrite = fopen($file, 'w+');
         fwrite($openFileToWrite, "<?php " . "\n" . $final_combine . "\n\n" . $bindPara . " ?>");
         fclose($openFileToWrite);
     }
     if (file_exists($file)) {
         include $file;
         try {
             $stmt->execute();
             //delete content of the file
             file_put_contents($file, "");
             return true;
         } catch (PDOException $e) {
             // if any error messages, display a nice little message to the user
             echo $start . $e->getMessage() . $end;
         }
     }
 }