How to create a table from an array in Drupal

This is how you can create a table from an array in Drupal. First loop your data and set an new array to have the ability to set class or id to cells or rows. Then call the theme() function in Drupal and format the output.

<?php
function zimplicit_table($keyword_linked, $keywords, $row_limit, $links_count = 0) {
     
     
//Set table headers
     
$tableHeaders[] = array(
     
'data' => t('Keyword'),
     
'class' => 'alignLeft keywordlink-l_keyword',
      );
     
$tableHeaders[] = array(
     
'data' => t('Linked'),
     
'class' => 'alignLeft keywordlink-k_linked',
      );


          foreach(
$keyword_linked as $key => $link) {
          
$r++;
         
          if (
$r % 2) {
             
$kl_oddeven = "kl-odd";
          } else {
             
$kl_oddeven = "kl-even";
          }

               
$tdCells = $tdCellsCollapsed = array();

             
$tdCells[] = array(
               
'data' => '<span>'.$link[3].'</span>',
               
'title' => $link[2],
               
'class' => 'alignLeft kl-keyword ',
              );
             
$tdCells[] = array(
               
'data' => '<div class="'.($link[18] > 0 ? "kl-linked" : "kl-not-linked").'">'.'</div>',
               
'title' => "Used as link in body",
               
'class' => 'alignLeft kl-link '. ($link[14] < 1 ? "kl-red" :  ($link[14] > 20 ? "kl-red" : "kl-green")),
              );
             
             
//An extra row
             
$tdCellsCollapsed[] = array(
               
'data' => '<span>'.$some_output.'</span>',
               
'title' => $some_output_title,
               
'class' => 'alignLeft some-class ',
               
'colspan' => 7,
              );

         
//Add two rows with the same class
         
$tableRows[] = array('data' => $tdCells, 'class' => $kl_oddeven);
         
$tableRows[] = array('data' => $tdCellsCollapsed, 'class' => $kl_oddeven);

      }

     
$out .= '<div id="keywordusage" class="ui-tabs-panel ui-widget-content ui-corner-bottom">';
       
$out .= '<div class="tabsinfo">';
         
$out .= '<div class="padding">';
           
$out .= '<div class="col-wider">';
             
$out .= '<H2 class="keywordlink-title">'.t("Keyword ranking in article").'</H2>';
             
$out .= '<div class="perfow '.$scroll_class.'">'.theme('table', $tableHeaders, $tableRows, $attributes = array("class" => $scroll_class), $caption = NULL).'</div>';
           
$out .= '</div>';
           
$out .= '<div class="break"></div>' ;
         
$out .= '</div>';
       
$out .= '</div>';
     
$out .= '</div>';

      return
$out;

}
?>
Knowledge keywords: