Working With Table And Pagination In Drupal 7
When developing a module for Drupal 7 you can display the query result into a table and then display it along with the pagination. You can do it relatively easy with functions provided by Drupal. Look at the code below to see how to create a table with pagination in Drupal 7.
The example of table with paging for Drupal 8 is available at http://w3shaman.com/article/drupal-8-sortable-table-pager.
- Sample Module
-
<?php
/**
* A Drupal 7 module for displaying table with pagination
*/
/**
* Implementation of hook_menu()
*/
function testing_menu(){
$items=array();
'title' => 'Testing Table',
'page callback' => '_testing_table',
'access arguments' => array("access content"),
'type' => MENU_CALLBACK
);return
$items;
} /**
* Function for displaying table
*/
function _testing_table(){
$output = "";
$query = db_select("registry", "r");
// Select fields
$query->fields("r", array("name", "type", "filename", "module", "weight"));
// For pagination
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(50);
$result = $query->execute();
$header = array('Name', 'Type', 'Filename', 'Module', 'Weight');
while($data = $result->fetchObject()){
// Fill the table rows
$rows[] = array(
$data->name,
$data->type,
$data->filename,
$data->module,
$data->weight
);
}
$output = theme_table(
array(
"header" => $header,
"rows" => $rows,
"attributes" => array(),
"sticky" => true, // Table header will be sticky
"caption" => "",
"colgroups" => array(),
"empty" => t("Table has no row!") // The message to be displayed if table is empty
)
).theme("pager");return
$output;
}
?>
- Make It Sortable
-
You can also make the table to be sortable if you click its header. Just do the following modifications to your current code.
- Add this code to the beginning of _testing_table() function.
<?php
if(isset($_GET['sort']) && isset($_GET['order'])){
// Sort it Ascending or Descending?
if($_GET['sort'] == 'asc')
$sort = 'ASC';
else
$sort = 'DESC';
switch($_GET['order']){
case 'Name':
$order = 'name';
break;
case 'Type':
$order = 'type';
break;
case 'Filename':
$order = 'filename';
break;
case 'Module':
$order = 'module';
break;
default:
$order = 'name';
}
}
else{
// Default sort
$sort = 'ASC';
$order = ' name';
}
?> - Then add these line after the $query->fields() command.
<?php
// Set order by
$query->orderBy($order, $sort);
?> - For the last, prepare the table header like this.
<?php
// Prepare table header
$header = array(
array(
"data" => t('Name'),
"field" => "name"
),
array(
"data" => t('Type'),
"field" => "type"
),
array(
"data" => t('Filename'),
"field" => "filename"
),
array(
"data" => t('Module'),
"field" => "module"
),
array(
"data" => t('Weight'),
"field" => "weight"
)
);
?> - And, this is the complete modified function.
<?php
/**
* Function for displaying table
*/
function _testing_table(){
// Check if there is sorting request
if(isset($_GET['sort']) && isset($_GET['order'])){
// Sort it Ascending or Descending?
if($_GET['sort'] == 'asc')
$sort = 'ASC';
else
$sort = 'DESC';
switch($_GET['order']){
case 'Name':
$order = 'name';
break;
case 'Type':
$order = 'type';
break;
case 'Filename':
$order = 'filename';
break;
case 'Module':
$order = 'module';
break;
default:
$order = 'name';
}
}
else{
// Default sort
$sort = 'ASC';
$order = ' name';
}
$query = db_select("registry", "r");
// Selected fields
$query->fields("r", array("name", "type", "filename", "module", "weight"));
// Set order by
$query->orderBy($order, $sort);
// Pagination
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(50);
$result = $query->execute();
$header = array(
array(
"data" => t('Name'),
"field" => "name"
),
array(
"data" => t('Type'),
"field" => "type"
),
array(
"data" => t('Filename'),
"field" => "filename"
),
array(
"data" => t('Module'),
"field" => "module"
),
array(
"data" => t('Weight'),
"field" => "weight"
)
);
while($data = $result->fetchObject()){
// Fill the table rows
$rows[] = array(
$data->name,
$data->type,
$data->filename,
$data->module,
$data->weight
);
}
$output = theme_table(
array(
"header" => $header,
"rows" => $rows,
"attributes" => array("width"=>"100%"),
"sticky" => true, // Table header will be sticky
"caption" => "",
"colgroups" => array(),
"empty" => t("Table has no row!") // The message to be displayed if table is empty
)
).theme("pager");return
$output;
}
?>
- Add this code to the beginning of _testing_table() function.
This is the sample code of a Drupal module that implements table with pagination.
Tags:
Comments
Anonymous (not verified)
Wed, 07/13/2011 - 17:22
Permalink
Thanks
Thanks a lot
Anonymous (not verified)
Fri, 10/14/2011 - 17:57
Permalink
Farooq Ahmad
Thanks,
Very useful content.
Anonymous (not verified)
Mon, 03/05/2012 - 17:01
Permalink
terimakasih banyak
terimakasih banyak karena anda sangat membantu sekali
p3tt3r (not verified)
Tue, 03/27/2012 - 03:59
Permalink
thank you so much.
thank you so much.
Josh Williams (not verified)
Tue, 04/03/2012 - 08:32
Permalink
Warning
Please note, this will not work if you use chaining on the extend() method.
Meaning this:
$query->extend('TableSort')->extend('PagerDefault')->limit(50);
will not work. But this:
$query = $query->extend('TableSort')->extend('PagerDefault')->limit(50);
Does work.
Lesley E (not verified)
Fri, 04/12/2013 - 11:33
Permalink
Chaining w/TableSort
Josh, is it true you cannot simply chain ->extend on the single query? Why is that?
thanks.
I Wayan Wirka (not verified)
Sat, 09/14/2013 - 10:59
Permalink
Works fine
This works for me, Thanks Josh for saving my time.
Manu (not verified)
Tue, 04/10/2012 - 18:14
Permalink
Thanks
It is very useful content..Thank you so much..
Anonymous (not verified)
Sat, 04/14/2012 - 01:16
Permalink
Why do I get this message?
Why do I get this message? Can you tell me what's wrong? Thank You!
Fatal error: Cannot redeclare testing_menu() (previously declared in C:\wamp\www\drupal-7.12\modules\php\php.module(80) : eval()'d code:9) in C:\wamp\www\drupal-7.12\modules\php\php.module(80) : eval()'d code on line 20
admin
Fri, 04/20/2012 - 12:24
Permalink
Change The Module Name
It seems like the module name in this example conflicted with one in Drupal core. You just need to change the module name in this example.
VK (not verified)
Mon, 04/16/2012 - 12:53
Permalink
Show All
Is there a way to add show-all link next to pager?
Thanks
admin
Fri, 04/20/2012 - 12:27
Permalink
I think it's possible
Yes, I think it's possible. But you have to write additional code.
saturnino (not verified)
Thu, 05/03/2012 - 09:25
Permalink
Thank you
So perfect tip,
Thank you very much !
Jagadees (not verified)
Wed, 05/30/2012 - 21:21
Permalink
Thank you
Nice Article
sze (not verified)
Wed, 08/01/2012 - 10:51
Permalink
thank you!
thank you!
eliosh (not verified)
Fri, 10/19/2012 - 18:24
Permalink
Great
Just a comment:
i prefer to use
$output = theme("table", ....
instead of use
$output = theme_table( ....
Anonymous (not verified)
Fri, 10/26/2012 - 10:14
Permalink
excellent and clear
excellent and clear
xyz (not verified)
Tue, 12/25/2012 - 19:00
Permalink
how to use when tables are created from WYSIWYG editor
how to use above code when tables are created from WYSIWYG editor
Madhav Vyas (not verified)
Fri, 04/12/2013 - 21:40
Permalink
orderBy($order, $sort) should be orderByHeader($header)
In your example, orderby cause should be replace with orderByHeader so code would be....
// Set order by
$query->orderByHeader($header)
and in that case $header should be declare before this statement.
If we implement that than we can remove below code..
// Check if there is sorting request
if(isset($_GET['sort']) && isset($_GET['order'])){
// Sort it Ascending or Descending?
if($_GET['sort'] == 'asc')
$sort = 'ASC';
else
$sort = 'DESC';
// Which column will be sorted
switch($_GET['order']){
case 'Name':
$order = 'name';
break;
case 'Type':
$order = 'type';
break;
case 'Filename':
$order = 'filename';
break;
case 'Module':
$order = 'module';
break;
default:
$order = 'name';
}
}
else{
// Default sort
$sort = 'ASC';
$order = ' name';
}
Nut (not verified)
Wed, 06/05/2013 - 14:43
Permalink
It's awesome!
That's save a lot of my time.
Thanks Dude.
I Wayan Wirka (not verified)
Wed, 06/12/2013 - 12:19
Permalink
Initial Order
Below code:
}
else{
// Default sort
$sort = 'ASC';
$order = ' name';
}
This OK if want to ascending order for initial condition but for descending one, must be:
}
else{
// Default sort
$sort = 'DESC';
$order = ' name';
$_GET['sort'] = $sort;
$_GET['order'] = $order;
}
sidd (not verified)
Wed, 07/17/2013 - 22:08
Permalink
query
i wanna to know how to display my query results as a view
Fairoz (not verified)
Fri, 07/26/2013 - 14:37
Permalink
Awesome
Thanks alot it's very helpfull for me.......................
narendra sharma (not verified)
Fri, 09/13/2013 - 20:34
Permalink
very helpul tutorial.........
very helpul tutorial..............
vicky (not verified)
Tue, 06/17/2014 - 19:19
Permalink
thanks
When I have tried to implement the script in my PHP application it was showing many errors. It would be grateful if you could share with us the ways on how to rectify those errors in this site.
as (not verified)
Tue, 06/17/2014 - 19:55
Permalink
as
as
Add new comment