Skip to content

Instantly share code, notes, and snippets.

@M1r1k
Created July 29, 2014 09:26
Show Gist options
  • Save M1r1k/840f4993abc3ee87d563 to your computer and use it in GitHub Desktop.
Save M1r1k/840f4993abc3ee87d563 to your computer and use it in GitHub Desktop.
Use db_select instead of db_query in the loop
@@ -45,10 +46,17 @@ function pp_frontpage_most_content_type_render($subtype, $conf, $panel_args, $co
);
$table_header = array(t('user'), t('counter'));
foreach ($types as $title => $data_type) {
- $query_results = db_query($query, array(
- ':type' => $data_type,
- ':timestamp' => $last_thee_months,
- ))->fetchAllKeyed();
+ $query = db_select('ppgetstat', 'ppst');
+ $query->rightJoin('field_data_field_user_id', 'fuid', 'fuid.field_user_id_value = ppst.doid');
+ $query->addExpression('SUM(ppst.items)', 'counter');
+ $query_results = $query->fields('ppst', array('doid'))
+ ->condition('ppst.type', $data_type)
+ ->condition('ppst.timestamp', $last_thee_months, '>')
+ ->groupBy('ppst.doid')
+ ->orderBy('counter', 'DESC')
+ ->range(0, 5)
+ ->execute()
+ ->fetchAllKeyed();
$table_data = array();
foreach ($query_results as $doid => $counter) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment