Advertisement
duck__boy1981

Wordpress - create query for multiple inserts

Jul 31st, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.23 KB | None | 0 0
  1. /**
  2.  * Updatess the invitees database table
  3.  */
  4. function do_add_invitees_in_database(){
  5.    
  6.     /** Make sure there are invitees to add */
  7.     if(empty($this->invitees['new'])) :
  8.         return $_POST['add_successfull'] = true;
  9.     endif;
  10.    
  11.     global $wpdb;
  12.    
  13.     /** Set the names of the fields to use */
  14.     $fields = array(
  15.         'invitee_created_by',
  16.         'invitee_created_date',
  17.         'invitee_last_edited_by',
  18.         'invitee_last_edited_date',
  19.         'event_id',
  20.         'email',
  21.         'first_name',
  22.         'surname',
  23.         'custom',
  24.         'invited'
  25.     );     
  26.    
  27.     /** Get the creator ID */
  28.     $creator_id = get_current_user_id();
  29.    
  30.     /** Make the chart creation date */
  31.     $date_added = date('Y-m-d H:i:s', time());
  32.    
  33.     /** Insert the new invitees in to the database */
  34.     $all_data = array();
  35.     $values = array();
  36.     foreach($this->invitees['new'] as $invitee) :
  37.    
  38.         $row = array(
  39.             $creator_id,
  40.             $date_added,
  41.             '0',
  42.             '0000-00-00 00:00:00',
  43.             $_POST['event_id'],
  44.             $invitee['email'],
  45.             $invitee['first_name'],
  46.             $invitee['surname'],
  47.             $invitee['custom'],
  48.             0
  49.         );
  50.        
  51.         /** Make the data array */
  52.         $data = array_combine($fields, $row);
  53.        
  54.         /** Update the array containing all data passed to the query (for debugging purposes) */
  55.         $all_data[] = $data;
  56.        
  57.         /** Grab the VALUES() portion of the query (for this invitee) */
  58.         $values[] = $this->_insert_replace_invitee_values($data);
  59.        
  60.     endforeach;
  61.    
  62.     /** Insert the new invitees in to the database */
  63.     $result = $this->_insert_replace_invitee($wpdb->event_invitees, $values, $fields);
  64.        
  65.     /** Debug the query run by $wpdb->query() */
  66.     $this->debug($all_data);
  67.    
  68.     /** If there were no errors, mark the invitaion additions as successfull */
  69.     return $_POST['add_successfull'] = (!$this->error_found) ? true : false;
  70.    
  71. }
  72.  
  73. /**
  74.  * Runs a query to either insert new rows into the database, or to replace existing rows (if ID is specified)
  75.  *
  76.  * @param required string $table    The name of the table that is being queried
  77.  * @param required array $values    The values to insert or replace in the table
  78.  * @param required array $fields    The fields that are to be populated by the $values
  79.  * @param string $type              The type of job that is taking place ('INSERT' or 'REPLACE')
  80.  * @return integer                  The number of rows affected by the query
  81.  */
  82. function _insert_replace_invitee($table, $values, $fields, $type = 'INSERT'){
  83.    
  84.     /** Make sure that we should be using this helper */
  85.     if(!in_array(strtoupper($type), array('INSERT', 'REPLACE'))) :
  86.         return false;
  87.     endif;
  88.    
  89.     global $wpdb;
  90.    
  91.     $query = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES " . join(','."\n", $values);
  92.    
  93.     $query = $wpdb->prepare('%1$s INTO %2$s'."\n\t".'(`%3$s`)'."\n".'VALUES'."\n\t", $type, $table, implode('`,`', $fields)).join(','."\n\t", $values);
  94.     return $wpdb->query($query);
  95.    
  96. }
  97.  
  98. /**
  99.  * Creates a string of values that should be inserted in to database for an invitee, or to replace existing rows (if ID is specified)
  100.  *
  101.  * @param required array $data      The data to use for the insert ('$key => $value' muse be defined)
  102.  * @param required array $format    The formats to use for specific data
  103.  * @param string $type              The type of job that is taking place ('INSERT' or 'REPLACE')
  104.  * @return string                   The values for this invitee
  105.  */
  106. function _insert_replace_invitee_values($data, $format = null, $type = 'INSERT'){
  107.    
  108.     /** Make sure that we should be using this helper */
  109.     if(!in_array(strtoupper($type), array('INSERT', 'REPLACE'))) :
  110.         return false;
  111.     endif;
  112.    
  113.     global $wpdb;
  114.    
  115.     /** Set up the formtas array */
  116.     $formats = $format = (array)$format;    // Setup a $formats array (and ensure $format is also an array)
  117.     $fields = array_keys($data);            // Grab the fields that are to be used from $data
  118.    
  119.     /** Work out which fields are to be formatted (and add default fromating to those not defined) */
  120.     $formatted_fields = array();
  121.     foreach($fields as $field) :
  122.    
  123.         if(!empty($format)) : // If $format is defined, check
  124.             $filed_format = ($filed_format = array_shift($formats)) ? $filed_format : $format[0];
  125.         else : // Set the format as defult to string
  126.             $filed_format = '%s';
  127.         endif;
  128.        
  129.         $formatted_fields[] = $filed_format;
  130.        
  131.     endforeach;
  132.    
  133.     return $wpdb->prepare(sprintf('(%1$s)', implode(",", $formatted_fields)), $data);
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement