How to Create table in drupal, most simple and perfect way

June 06, 2012


This is a series of drupals amazing tutorial (3 parts).

Part One is a step by step tutorials on how to create custom module in drupal in amazing way
Part two depends on first part. It uses first part module. This part describes how to create custom form in drupal in simplest and most easiest way
Third part depends on second part. This part describes how to create drupal table in correct, perfect and most descriptive way

Creating drupal table is easy. Here i discuss a-to-z about drupal table. Firstly, i discuss how to create a drupal table in most simplest way. Secondly, i extend it by adding new functionalities of above simple table. After this tutorial, i hope you call yourself a drupal table master !

We create a table with the following options:

  • Sortable Column
  • Adding extra links like edit , delete
  • Adding extra attributes to these links (No 2) e.g target, class, id
  • pagination
  • Add Table caption

Final output

Functions necessery to complete this tutorials

  • theme_table($header, $rows, $attributes = array(), $caption = NULL) = To create a table;
  • tablesort_sql($header, $before = '') = To sort table column
  • pager_query($query, $limit = 10, $element = 0, $count_query = NULL) = Add pagination to a table
  • l($text, $path, $options = array()) = Create extra links, for example edit/delete
  • l($text, $path, $options = array()) = Create extra attributes for link .i.e wheter the link open in new window or not, add class , id etc.

Table creation function

theme_table($header, $rows, $attributes = array(), $caption = NULL) has number of useful parameters. Before creating table we need to know about these parameters.

  • $header: An array containing the table headers.
  • $rows: An array of table rows.
  • $attributes: An array of HTML attributes to apply to the table tag.
  • $caption: A localized string to use for the tag.

Create Menu item for view person

Our final person_menu() function code will be looks like

<?php/** * implements hook_menu() */ function person_menu(){ $items = array(); // declare variable to avoid unexpected things $items['person'] = array( 'title' =&gt; "Person", 'page callback' =&gt; "person_personal_info", // after visit drupal6/person, person_personal_info() function is called 'access callback' =&gt; true, // must return true, otherwise it will not visible as menu item 'type' =&gt; MENU_NORMAL_ITEM, // drupal's default menu type 'weight' =&gt; '10', // we want to display person link below in our nav menu ); $items['personview'] = array( 'title' =&gt; "Person View", 'page callback' =&gt; "person_view", // after visit person view, person_view() function is called 'access callback' =&gt; true, // must return true, otherwise it will not visible as menu item 'type' =&gt; MENU_NORMAL_ITEM, // drupal's default menu type 'weight' =&gt; '11', // we want to display person view link below in our nav menu ); return $items; // finally, do not forget to return $items array } ?>

Callback function for person view

Now create a callback function for person view with the following header and table rows

<?php/** * menu callback; person_view */ function person_view(){ // header $header = array("Name","City","Country"); // some sample table rows $rows = array( array("Noyon",'Kolkata','India'), array("Sumon",'Dhaka','Bangladesh'), array("Sikdar",'London','UK'), array("Albert",'Newyork','USA'), ); // create table $output = theme_table($header,$rows); return $output; } ?>

Rebuild Menu and Click Person view link, you will get the following table

Defining headers: the correct way

Above header example has some disadvantages

  • We can not make it sortable and also default sort
  • We can not apply colspan or rowspan or any other table attributes

In order to do above we have to define header as

<?php$header = array( array('data'=&gt;'Name', 'field'=&gt;, 'sort'=&gt;'asc'), // make name column sortable and Name column is default array('data'=&gt;'City', 'field'=&gt;, // make city column sortable array('data'=&gt;'Country') , // Country column is not sortable ); ?>

Introducing tablesort_sql function

Now, to sort column after clicking on it, passing modified header to tablesort_sql() function. So our modified codes like the below:

<?php/** * menu callback; person_view */ function person_view(){ // header $header = array( array('data'=&gt;'Name', 'field'=&gt;'','sort'=&gt;'asc'), // Name column will be sort by default array('data'=&gt;'City', 'field'=&gt;''), array('data'=&gt;'Country'), ); // get all data from person table $sql = "SELECT * FROM person as p"; $sql .= tablesort_sql($header); // tablesort_sql for sortable table fields $result = db_query($sql); while ($row = db_fetch_object($result)){ $rows[] = array( $row-&gt;name, $row-&gt;city, $row-&gt;country, ); } // create table $output = theme_table($header,$rows); return $output; } ?>

Now refresh the page or visit person view link, the output of the above code will be

Introducting pager_query function for Pagination

Table pager is necessery, when there were more rows on a table. Just replace db_query with pager_query with additional limit parameters. Limit parameters determines how many rows displayed at a time.

<?php$result = pager_query($sql,$limit=5); // display 5 rows at a time ?>

Introducting theme_pager function to display pager

Now just add theme_pager() function just after table creation.

<?php$output .= theme_pager(null,$limit=5); // table pagination ?>

So, our final code will be looks like

<?php/** * menu callback; personview */ function person_view(){ // header $header = array( array('data'=&gt;'Name', 'field'=&gt;'','sort'=&gt;'asc'), // Name column will be sort by default array('data'=&gt;'City', 'field'=&gt;''), array('data'=&gt;'Country', 'field'=&gt;''), ); // get all data from person table $sql = "SELECT * FROM person as p"; $sql .= tablesort_sql($header); // tablesort_sql for sortable table fields //$result = db_query($sql); $result = pager_query($sql,$limit=5); // display 5 rows at a time while ($row = db_fetch_object($result)){ $rows[] = array( $row-&gt;name, $row-&gt;city, $row-&gt;country, ); } // create table $output = theme_table($header,$rows); $output .= theme_pager(null,$limit=5); // table pagination return $output; } ?>

Now refresh the page or visit person view link. Your new table will now both sortable and have pagination