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")
?>

Monday, January 31, 2011

What is the difference between Body.OnLoad and jQuery document.ready() Event?

The main differences between the two are:
Body.Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document.ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded. Hence, the functions in jQuery's ready event will get executed once the HTML structure is loaded without waiting for the resources.
We can have multiple document.ready() in a page but Body.Onload() event cannot.

Additional submit button on node forms.

Sometimes you are asked to add a second submit button above each node form.
This can be easily done with a few lines of code:

secondsubmit.info
; $Id$
name = "Second submit"
description = "Second submit button on node forms."
core = "6.x"
secondsubmit.module
// $Id$

/**
* @file
* Module definition for secondsubmit.
*/

/**
* Implement hook_form_alter().
*/
function secondsubmit_form_alter(&$form, $form_state, $form_id) {
// Add a second submit button to node forms.
if ($form['#node'] && ($form_id == $form['#node']->type . '_node_form')) {
$form['secondsubmit'] = array(
'#type' => 'submit',
'#value' => t('Save & continue'),
'#weight' => -10,
'#validate' => array('node_form_validate'),
// Use default and an additional submit handler.
'#submit' => array('node_form_submit', 'additional_node_form_submit'),
);
}
}

/**
* Implement of additional form submit
*/
function additional_node_form_submit($form, &$form_state) {
// ...
}
?>

Grouping an array based on value of the array

this example Groups the array based on color

function groupbyfirst($array)
{

foreach ($array as $row)

{

$firstkeys = array_keys($row);

$firstkey = $firstkeys[0];

$firstkey = strtolower($row[$firstkey]);

$tol = $firstkey;

$newarray[$tol][] = $row;

}

return $newarray;

}



$array =

Array(

0 => Array('color' => 'red','name' => 'apple', 'quantity' => '3'),

1 => Array('color' => 'green','name' => 'pear', 'quantity' => '2'),

2 => Array('color' => 'yellow','name' => 'corn', 'quantity' => '3'),

3 => Array('color' => 'blue','name' => 'grape', 'quantity' => '4'),

4 => Array('color' => 'yellow','name' => 'banana', 'quantity' => '13'),

5 => Array('color' => 'yellow','name' => 'banana', 'quantity' => '15'),

);



$output = groupbyfirst($array);

echo "
";

print_r($output);

echo "
";