Create Table Structure with Pagination in Drupal Using theme table

For creating tables in drupal, you don’t need to write html table tag structure.
It is very easy to implement this in drupal.

Here I am giving the step by step explanation with code:-

1:-First create a menu in hook_menu() function.
In this menu we will display the table using theme table.
function hook_menu() {
    $items = array();
    $items['theme_table'] = array(
            'title' => t('Theme Table'),
            //calling custom test_theme_table() function on this menu
            'page callback' => 'test_theme_table',
            'access arguments' => array('access content'),
            'type' => MENU_CALLBACK,
    return $items;
2:- Code for test_theme_table function:-
function test_theme_table(){
    $html = '';
    //this $header array contains table header name,can be changed as per the requirement
    $header = array(t('BID'),t('Module'),t('Delta'),t('THEME'),t('Status'));
    //$count is the number of rows you want to display per page
    $count = 5;                   
    //fetching records from blocks tabble
    $res = "SELECT * FROM {blocks}";
   //passing sql query and count value in drupal pager_query function
    $query = pager_query($res, $count);
    $data = array();
    while ($row = db_fetch_array($query)) {
      $data[] = array(
    //adding id to the table(Not required)
    $table_attributes = array('id' => 'example');
    $output = theme('table', $header, $data, $table_attributes);
    //returning the resultant table with $count=5
return $output.theme('pager', $count);
Instead of displaying records from database,you can display any records using them table.

No comments:

Post a Comment