Advertisement
designbymerovingi

Get Total by Reference

Jun 24th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. function mycred_total_per_ref( $ref = '', $user_id = NULL )
  2. {
  3.     // Reference is required
  4.     if ( empty( $ref ) ) return false;
  5.    
  6.     global $wpdb;
  7.    
  8.     // The myCRED Log
  9.     $db = $wpdb->prefix . 'myCRED_log';
  10.    
  11.     // Get myCRED
  12.     $mycred = mycred_get_settings();
  13.    
  14.     // Get all points given for this reference to a specific user
  15.     if ( $user_id !== NULL ) {
  16.         $sql = "SELECT creds FROM {$db} WHERE ref = %s AND user_id = %d";
  17.         $query = $wpdb->get_results( $wpdb->prepare( $sql, $ref, $user_id ) );
  18.     }
  19.     // Get all points given for this reference
  20.     else {
  21.         $sql = "SELECT creds FROM {$db} WHERE ref = %s";
  22.         $query = $wpdb->get_results( $wpdb->prepare( $sql, $ref ) );
  23.     }
  24.    
  25.     $count = 0;
  26.     // if we have results we add creds up
  27.     if ( !empty( $query ) ) {
  28.         foreach ( $query as $entry ) {
  29.             $count = $count+$entry->creds;
  30.         }
  31.     }
  32.    
  33.     return $mycred->number( $count );
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement