<?php
/**
* This module provide access for viewing data from database
* in table format using ajax.
* the table structure is like this one:
* Table: data_siswa
* id int not null auto_increment key,
* no_induk int,
* nama char(50),
* gender char,
* kelas int(1),
* tanggal_lahir char(30),
* alamat char(50)
*
* @type : drupal module - db, ajax, form, menu
* @author : irfanudin ridho
* @email : irfan.ub@gmail.com
* @version : 3.0
* @date : February 25, 2011
*/
/**
* Implements hook_menu()
*/
function data_siswa_menu(){
$items = array();
$items['data/siswa'] = array(
'title' => 'Data Siswa',
'page callback' => 'drupal_get_form',
'page arguments' => array('get_siswa'),
'access callback' => TRUE,
);
return $items;
}
/**
* render ajax form
*/
function get_siswa($form, &$form_state){
$form['kelas'] = array(
'#type' => 'select',
'#title' => t('Pilih Kelas'),
'#options' => get_kelas(),
'#ajax' => array(
'callback' => 'get_siswa_ajax_callback',
'wrapper' => 'table_siswa',
),
);
$form['table'] = array(
'#type' => 'item',
'#prefix' => '<div id="table_siswa">',
'#suffix' => '</div>',
);
if(!empty($form_state['values']['kelas'])){
$render = render_table($form_state['values']['kelas']);
$form['table']['#title'] = $render;
}
return $form;
}
/**
* Getting distict kelas, and then supply it for the form
*/
function get_kelas(){
$query = db_query('SELECT DISTINCT kelas FROM data_siswa');
$data = array();
foreach($query as $key){
$data[$key->kelas] = $key->kelas;
}
return $data;
}
/**
* Ajax callback from the ajax form
*/
function get_siswa_ajax_callback($form, &$form_state){
return $form['table'];
}
/**
* Rendering the data of the data_siswa table to
/**
* This module provide access for viewing data from database
* in table format using ajax.
* the table structure is like this one:
* Table: data_siswa
* id int not null auto_increment key,
* no_induk int,
* nama char(50),
* gender char,
* kelas int(1),
* tanggal_lahir char(30),
* alamat char(50)
*
* @type : drupal module - db, ajax, form, menu
* @author : irfanudin ridho
* @email : irfan.ub@gmail.com
* @version : 3.0
* @date : February 25, 2011
*/
/**
* Implements hook_menu()
*/
function data_siswa_menu(){
$items = array();
$items['data/siswa'] = array(
'title' => 'Data Siswa',
'page callback' => 'drupal_get_form',
'page arguments' => array('get_siswa'),
'access callback' => TRUE,
);
return $items;
}
/**
* render ajax form
*/
function get_siswa($form, &$form_state){
$form['kelas'] = array(
'#type' => 'select',
'#title' => t('Pilih Kelas'),
'#options' => get_kelas(),
'#ajax' => array(
'callback' => 'get_siswa_ajax_callback',
'wrapper' => 'table_siswa',
),
);
$form['table'] = array(
'#type' => 'item',
'#prefix' => '<div id="table_siswa">',
'#suffix' => '</div>',
);
if(!empty($form_state['values']['kelas'])){
$render = render_table($form_state['values']['kelas']);
$form['table']['#title'] = $render;
}
return $form;
}
/**
* Getting distict kelas, and then supply it for the form
*/
function get_kelas(){
$query = db_query('SELECT DISTINCT kelas FROM data_siswa');
$data = array();
foreach($query as $key){
$data[$key->kelas] = $key->kelas;
}
return $data;
}
/**
* Ajax callback from the ajax form
*/
function get_siswa_ajax_callback($form, &$form_state){
return $form['table'];
}
/**
* Rendering the data of the data_siswa table to
* html table for viewing to user
*/
function render_table($kelas){
$query = db_select("data_siswa")
->fields('data_siswa')
->condition('kelas',$kelas)
->execute()
->fetchAll();
$output = '<h2>Data Siswa Pada Kelas: '.$kelas.'</h2>';
$output .= '<table><tr>';
$output .= '<th>No</th>';
$output .= '<th>No Induk</th>';
$output .= '<th>Nama</th>';
$output .= '<th>Gender</th>';
$output .= '<th>Tanggal Lahir</th>';
$output .= '<th>Alamat</th>';
$i = 1;
foreach($query as $key){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$key->no_induk.'</td>';
$output .= '<td>'.$key->nama.'</td>';
$output .= '<td>'.$key->gender.'</td>';
$output .= '<td>'.$key->tanggal_lahir.'</td>';
$output .= '<td>'.$key->alamat.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
return $output;
}
*/
function render_table($kelas){
$query = db_select("data_siswa")
->fields('data_siswa')
->condition('kelas',$kelas)
->execute()
->fetchAll();
$output = '<h2>Data Siswa Pada Kelas: '.$kelas.'</h2>';
$output .= '<table><tr>';
$output .= '<th>No</th>';
$output .= '<th>No Induk</th>';
$output .= '<th>Nama</th>';
$output .= '<th>Gender</th>';
$output .= '<th>Tanggal Lahir</th>';
$output .= '<th>Alamat</th>';
$i = 1;
foreach($query as $key){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$key->no_induk.'</td>';
$output .= '<td>'.$key->nama.'</td>';
$output .= '<td>'.$key->gender.'</td>';
$output .= '<td>'.$key->tanggal_lahir.'</td>';
$output .= '<td>'.$key->alamat.'</td>';
$output .= '</tr>';
}
$output .= '</table>';
return $output;
}
No comments:
Post a Comment