Q:

How to display fetched data in the form of html table?

I have created a custom module & used below code to display data from Database. But I want to show that data in the form of html table. Please help.
<?php
function custom_table_menu(){
 $items = array();
 $items['custom_table'] = array(
  'title' => 'Custom Table',
  'page callback' => 'custom_table_page_display',
  'access callback' => TRUE,
);
 return $items;
}

function custom_table_page_display() {
$result = db_query('SELECT * FROM {users}');
  $records = $result->fetchAll();

  $content = '<pre>' . print_r($records, TRUE) . '</pre>';

  return $content;
}
?>

A:

Try this example to output your data in table format with Pager (ie. table with page. 1,2,3.. )
function custom_table_page_display() {

  $header = array(
    array('data' => t('Name'), 'field' => 'id', 'sort' => 'desc'),
    array('data' => t('Address'), 'colspan' => 3),
  );

  $query = db_select('users', 'u');

  $query->fields('u', array('name','address'));  // select your required fields here

  $table_sort = $query->extend('TableSort') // Add table sort extender.

                      ->orderByHeader($header); // Add order by headers.

  $pager = $table_sort->extend('PagerDefault')

                      ->limit(20);  // here you can limit the number of data per page.

  $result = $pager->execute();

  $rows = array();
  foreach($result as $res) {
    $rows[] = array($res->id, 
                    $res-> title,);
  }
  // If rows are not empty theme and display the rows.

  $output = '';

  if (!empty($rows)) {

    $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sort-table')));

    $output .= theme('pager');

  }

  else {

    $output .= t("No results found.");

  }

  return $output;

}

