Thursday, February 10, 2011

Drupal 7 theme table creation with pagination & sorting

<?php

$header = array(array('data'=>t('Module'),'field'=>'module','sort'=>'asc'), t('Delta'), t('Theme'));

$sql_query = db_select('block', 's');

$sql_query->fields('s', array('module', 'delta', 'theme'));
$sql_query = $sql_query->extend('TableSort')->extend('PagerDefault')->limit(5);
$result = $sql_query->orderByHeader($header)->execute();

foreach($result as $res){
$rows[] = array($res->module,$res->delta,$res->theme);
}

$output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'my-module-table')));
$output .= theme('pager');

print $output;

?>

Create views for the custom table

1. Create an custom module "customviews"

2. Paste the below code

Here debug_user is the table name

debug_id,uid,uname are the field names present in that table

change this code according to your convenient


<?php

function customviews_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'customviews') . '/modules',
);
}

function customviews_views_data() {
$data['debug_user']['table']['group'] = t('Custom View');

$data['debug_user']['table']['base'] = array(
'field' => 'debug_id',
'title' => t('Custom table'),
'help' => t('Stores information about custom table.'),
'weight' => 10,
);

// Following are the fields of the table 'custom_views'
// Field:custom_id
$data['debug_user']['debug_id'] = array(
'title' => t('ID'),
'help' => t('ID of the custom table.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);

$data['debug_user']['uid'] = array(
'title' => t('UID'),
'help' => t('UID of the custom table.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
);


// Field:custom_title
$data['debug_user']['uname'] = array(
'title' => t('Name'),
'help' => t('Name of the field.'),
'field' => array(
'handler' => 'views_handler_field',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);

return $data;
}

?>

Drupal 6 Theme table creation with Pagination & Sorting

<?php

  $result = pager_query("SELECT module, delta, theme FROM {blocks}",5);

$header = array(array('data'=>t('Module'),'field'=>'module','sort'=>asc), t('Delta'), t('Theme'));

while ($res = db_fetch_object($result)) {
$rows[] = array($res->module,$res->delta,$res->theme);
}

$output .= theme('table', $header, $rows);
$output .= theme('pager',5,NULL);

print $output;

?>

Create xml using php

$employees = array();
$employees [] = array(
'name' => 'Albert',
'age' => '34',
'salary' => "$10000"
);
$employees [] = array(
'name' => 'Claud',
'age' => '20',
'salary' => "$2000"
);

$doc = new DOMDocument();
$doc->formatOutput = true;

$r = $doc->createElement( "employees" );
$doc->appendChild( $r );

foreach( $employees as $employee )
{
$b = $doc->createElement( "employee" );

$name = $doc->createElement( "name" );
$name->appendChild(
$doc->createTextNode( $employee['name'] )
);
$b->appendChild( $name );

$age = $doc->createElement( "age" );
$age->appendChild(
$doc->createTextNode( $employee['age'] )
);
$b->appendChild( $age );

$salary = $doc->createElement( "salary" );
$salary->appendChild(
$doc->createTextNode( $employee['salary'] )
);
$b->appendChild( $salary );

$r->appendChild( $b );
}

echo $doc->saveXML();
$doc->save("write.xml")
?>