UPDATE: I think I found the answer, see in the Comments section.

Summary: when rendering a table into a block, nothing is displayed. What am I doing wrong?

I've been researching the answer to my question for the past 48 hours, and have held off writing a comment or new forum question so as to not duplicate a question, or ask about something that is clearly documented elsewhere.

I am writing a module to display the results of a very simple query:

'SELECT fname, superlname, title, organization, contactid FROM {TABLE1} WHERE contactid > 0'

(I know the query works, as I am dpr()'ing this information and getting values back.)

I then put values into a header array, and foreach() the query results into another array. I then attempt to save this into $block['content'] and return this, but I get nothing on the screen.

I have dpr() the $block['content'] variable and I am seeing just :
<table></table>

So, clearly, I am sending in the header and rows information incorrectly, or it would be rendering properly in my block.
Also, if I change the query to:
'SELECT fname, superlname, title, organization, contactid FROM {TABLE1} WHERE contactid = 0'
(equal zero), then I fork over to the other part of the if statement, and the block properly displays "no contacts available."

function show_import_table_block_view($delta = '') {
  switch ($delta) {
    case 'show_import_table':
      $block['subject'] = t('All finalimport table rows.');
      if (user_access('access content')) {
        // Use our custom function to retrieve data.
        $result = show_import_table_contents();
        // Array to contain items for the block to render.
        // Iterate over the resultset and format as links.
       
       $header = array('fname', 'superlname', 'title', 'organization');
       $datarow = array();
       $allrows = array();
        foreach ($result as $row) {
          $title1 = substr($row->title, 0, 10);
          $organization1 = substr($row->organization, 0, 10);
          $datarow = array($row->fname, $row->superlname, 'title', 'organization');
          array_push($allrows, $datarow);
        }

    // No content in the last week.
        if (empty($allrows)) {
          $block['content'] = t('No contacts available.');  
        } 
        else {
          // Pass data through theme function.
          $superrows = array(
        array('row1col1', 'row1col2'),
        array('row2col1', 'row2col2'));
          $block['content'] = theme('table', $header, $superrows);
          dpr($header, $return = FALSE, $name = NULL);
          dpr($allrows, $return = FALSE, $name = NULL);
        }
      }
    return $block;
    $apple = 'Steve Jobs';
    dpr($apple, $return = FALSE, $name = NULL);
  }
  
}

I have been using the following two sources as syntax guidelines:
https://api.drupal.org/api/drupal/modules%21system%21theme.api.php/group...
https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_ta...

Comments

wileysegovia’s picture

I will attempt to write up some additional and more exact syntax examples for beginner and intermediate Drupal developers.

You have to explicitly use an ARRAY as the second part of the:

$block['content']=theme('table', array('header' -> $headers, 'rows' -> $rows));

variable declaration. If you have used any other syntax (such as:

$block['content']=theme('table', $headers, $rows);

or similar) the code will not work. At least that is what I believe was happening with my code. After making only this change, my code worked. If someone knows this to be incorrect, please comment accordingly.

Thanks!

Jaypan’s picture

You're correct for D7. D6 you actually had to pass each value in, rather than an array.