Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Created October 3, 2013 07:39
Show Gist options
  • Save hidayat365/6806441 to your computer and use it in GitHub Desktop.
Save hidayat365/6806441 to your computer and use it in GitHub Desktop.
Custom drupal form with table select and paging
<?php
function referrals_search_cardlist2() {
global $user;
$output = drupal_render(drupal_get_form('referrals_form_listpin'));
return $output;
}
function referrals_form_listpin()
{
// set limit for pager
$limit = 50;
// define table header
$header = array(
array('data' => t('ID'), 'field'=>'id', 'sort'=>'asc'),
array('data' => t('Program')),
array('data' => t('Serial'), 'field'=>'serial'),
array('data' => t('PIN'), 'field'=>'pinnumber'),
array('data' => t('Status'), 'field'=>'status'),
array('data' => t('Aktifasi'), 'field'=>'activated'),
array('data' => t('Member'), 'field'=>'activated'),
);
# set the database table and initial SelectQuery options
# $select is a SelectQuery object.
# @see http://api.drupal.org/api/drupal/includes--database--select.inc/class/Se...
$select = db_select('referrals_cardlist', 'rc')
->extend('PagerDefault')
->extend('TableSort');
# get the desired fields
# orderByHeader is a TableSort method (http://api.drupal.org/api/drupal/includes--tablesort.inc/function/TableS...)
$select->condition('program',$status,'=')
->condition('upgrade',($upgrade?'1':'0'),'=')
->fields('rc')
->orderByHeader($header)
->limit($limit);
# execute the query
$results = $select->execute();
// build options
$options = array();
while($item = $results->fetchObject()) {
// each element of the array is keyed with the serial number
if (isset($item->uid))
$member=user_load($item->uid);
$options[$item->serial] = array(
0 => $item->id,
1 => $item->program,
2 => $item->serial,
3 => $item->pinnumber,
4 => ($item->status ? "Aktif" : "Pasif"),
5 => !isset($item->activated) ? '' : format_date($item->activated,'custom','d F Y H:i'),
6 => !isset($item->uid) ? '' : $member->name,
);
if (isset($member))
unset($member);
}
$form = array();
$form['table'] = array(
'#type'=>'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => TRUE,
'#weight' => 10,
);
$form['pager'] = array(
'#markup' => theme('pager'),
'#weight' => 12,
);
$form['activate'] = array(
'#type'=>'submit',
'#value' => t('Aktif'),
'#weight' => 15,
);
$form['deactivate'] = array(
'#type'=>'submit',
'#value' => t('Pasif'),
'#weight' => 15,
);
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment