-1

I'm working with Drupal Commerce, and I am currently developing a new module that displays the best customers that have a maximum points in a single block.

I want to do a join between the two tables (users and userpoints_total)

$query = db_select('users', 'u')
  ->fields('u',array('name','uid'));
$query->join('userpoints_total', 't', 't.uid = u.uid');
$results = $query->execute();

That works but in the block views I can't see anything. This is the code:

    function best_client_block_view($delta = '') {
      switch ($delta) {
        case 'best_client':
          $block['subject'] = t('Les meilleures Clients Chez nous');
          if (user_access('access content')) {
            // Use our custom function to retrieve data.
            $result = best_client_contents();
            // Array to contain items for the block to render.
            $items = array();
            // Iterate over the resultset and format as links.
        if (is_array($result) || is_object($result))
            {
            foreach ($result as $node) {

              $items[] = array(
                'data' => t($node->name),

              ); 
            }
        }
           // No content in the last week.
            if (empty($items)) {
              $block['content'] = t('No user available.');  
            } 
            else {
              // Pass data through theme function.
              $block['content'] = theme('item_list', array(
                'items' => $items));
            }
          }
        return $block;
      }

    }

How can I fix that?

1
  • Would you be interested in a Views based answer to create such block? I.e. based on user points (ranking), and similar to the block you can see here, in the left column with block title "Top Users". Aug 16, 2016 at 15:28

1 Answer 1

1

You don't call a result set method on your query. Try $results = $query->execute()->fetchAll(); and have a look at the results via devel or something similar.

I believe that block['content'] defaults to markup but theme functions return HTML strings not renderable arrays so you could try to use the #markup key to make them render.

$block['content']['#markup'] = theme('item_list', array('items' => $items));

Or use the theme as a part of the render array.

$block['content'] = array(
  '#theme' => 'item_list', 
  '#type' => 'ul', 
  '#items' => $items
)
16
  • but he tell always << no users avaible >> i can't understand , my query is correct, can you give me a solution?
    – SeifBh
    Jun 7, 2015 at 20:33
  • 1
    Hmm, updated answer to adres another possible issue. Could you post your entire query @SeifBh and and show us the content of the result? Jun 8, 2015 at 9:40
  • @SurangaPanagamuwaGamage Thankyou for your answer, the same thing , i always see << no users avaible >> , and this is my query
    – SeifBh
    Jun 8, 2015 at 9:44
  • function best_client_contents() { $max_num = variable_get('best_client_max', 3); $query=db_select('users','us'); $query->join('userpoints_total','up','us.uid = up.uid'); $query->condition('us.uid','1') ->fields('us',array('name','uid')) ->fields('up',array('uid','points')); $results = $query->execute()->fetchAll(); }
    – SeifBh
    Jun 8, 2015 at 9:45
  • Ah I see, if you're not going to use the items as render arrays you don't need to use data (wich I believe expects a render array), try using $items[] = t($node->name) directly Jun 8, 2015 at 9:48

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.