2

I’m trying to create a custom module that implements a block to display something I haven’t been able to do with the Views 2 module.
I’ve tried to look at some guides and tutorials, but I haven’t got any result yet.

I can print out the first SELECT attribute in the first row. The first row make sense since I don’t loop through it but still I’m confused on how to do the rest.

function sn_most_contributed_block($op='list', $delta=0) {
  if ($op == "list") {
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Most Contribution this week (custom module');
    return $block;
  } 
  elseif ($op == 'view') {    
    $mce = db_result(
      db_query("SELECT node.uid, COUNT(nid), users.picture AS users_picture, users.uid AS users_uid, users.name AS users_name, users.mail AS users_mail FROM node node INNER JOIN users users ON node.uid = users.uid WHERE (node.type IN ('leads',  'qa',  'qa_comments',  'til_salg',  'tips_tricks')) AND node.created >= UNIX_TIMESTAMP( CURDATE( ) - INTERVAL 1 WEEK ) AND node.created <= UNIX_TIMESTAMP( CURDATE( ) + INTERVAL 1 DAY ) GROUP BY uid ORDER BY COUNT(nid) DESC LIMIT 0 , 5")
    );

    $block_content .= "Most contributed members are: " . $mce';
    $block_content .= "<div class='author picture profile_image'>In here should be the user image</div>";
    $block_content .= "<div id='username'>In here should be the username</div>";

    if ($block_content == '') {   
      $block['subject'] = 'contribution test';
      $block['content'] = 'yay';
      return $block;
    }
    else {
      // set up the block  
      $block = array();
      $block['subject'] = 'Most contribution this week';  
      $block['content'] = $block_content;
      return $block;
    }
  }
}

I’ve tested my query in the database environment for my Drupal site and it works and print everything it should out correctly.

For looping the query, I tried this code with no result.

while ($contriUser = db_fetch_object($mce)) {
  $contriUser [] = $contriUser->uid;
}

I have also tried the following code.

while ($contriUser = db_fetch_object($mce)) {
  var_dump($contriUser);
}

Any help on how to solve this or if someone has an alternative solution to get the most contributing members last seven days it would be much appreciated.

2
  • 1
    Hi, there is a syntax error inside "Most contributed members are: " . $mce';. The ' at the end is wrong. But I think that's not the actual problem.
    – nonsenz
    Commented May 17, 2011 at 9:09
  • Is the block even being created at all? You seem to be using the Drupal 6 version of hook_block but the post is tagged 7?
    – Chapabu
    Commented Nov 30, 2011 at 9:34

3 Answers 3

2

For most contributing members you may want to look at the user points modules, they give you a lot of very nice features.

A couple of quick pointers with your code as you are asking.

Your query should use parameters and be formatted according to drupal coding standards, this helps for security and portability. The coder module can help you with this.

To get the results you will probably want something like db_fetch_array in a loop

 $result_set = db_query("SELECT ...");
 while ($result = db_fetch_array($result_set)) {
   $block_content .= ...;
 }

I hope this helps a little.

1
  • 1
    To further add, db_result() should be used when the query is returning a single field, such as SELECT nid FROM {node} WHERE …. If you need to get only a result but the query is not selecting a single field, then you should use db_query_range().
    – avpaderno
    Commented May 17, 2011 at 15:40
1

I made some changes to the interesting part. Check out this untested code:

$block_content = "";
$result_set = db_query("SELECT node.uid, 
  COUNT(nid), 
  users.picture AS users_picture, 
  users.uid AS users_uid, 
  users.name AS users_name, 
  users.mail AS users_mail 
  FROM {node} 
  INNER JOIN {users} ON node.uid = users.uid 
  WHERE (node.type IN ('leads',  'qa',  'qa_comments',  'til_salg',  'tips_tricks')) 
  AND node.created >= UNIX_TIMESTAMP( CURDATE( ) - INTERVAL 1 WEEK ) 
  AND node.created <= UNIX_TIMESTAMP( CURDATE( ) + INTERVAL 1 DAY ) 
  GROUP BY uid 
  ORDER BY COUNT( nid ) DESC 
  LIMIT 0 , 5"
);

$block_content .= t("Most contributed members are: ");
while ($result = db_fetch_array($result_set)) {
  $block_content .= '<div class="author picture profile_image"><img src="'.$result['users_picture'].'"/></div>';      
  $block_content .= '<div id="username">'.$result['users_name'].'</div>';
}
1
  • db_query_range() should be used when you want to limit the number of returned table rows.
    – avpaderno
    Commented May 17, 2011 at 15:42
0

If you have views 3, dereine has kindly posted an import for this http://drupal.org/node/1186598#comment-4593972

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.