<?php
/**
* This module provide access for viewing data from database
* in table format using ajax. In this new version, you can
/**
* This module provide access for viewing data from database
* in table format using ajax. In this new version, you can
* choose all the siswa, not just a specified kelas
* 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 : 4.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'])){
// just select the selected kelas
$render = render_table($form_state['values']['kelas']);
$form['table']['#title'] = $render;
}else{
// select all kelas
$render = render_table(0);
$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();
$data['All'] = 'All';
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
* 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 : 4.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'])){
// just select the selected kelas
$render = render_table($form_state['values']['kelas']);
$form['table']['#title'] = $render;
}else{
// select all kelas
$render = render_table(0);
$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();
$data['All'] = 'All';
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){
$output = '<h2>Data Siswa Pada ';
$query = db_select("data_siswa")
->fields('data_siswa');
// if kelas == 0, it's the all kelas selected
if($kelas==0){
$output .= 'Seluruh Kelas</h2>';
}else{
$query = $query->condition('kelas',$kelas);
$output .= 'Kelas: '.$kelas.'</h2>';
}
$query = $query->execute()->fetchAll();
$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){
$output = '<h2>Data Siswa Pada ';
$query = db_select("data_siswa")
->fields('data_siswa');
// if kelas == 0, it's the all kelas selected
if($kelas==0){
$output .= 'Seluruh Kelas</h2>';
}else{
$query = $query->condition('kelas',$kelas);
$output .= 'Kelas: '.$kelas.'</h2>';
}
$query = $query->execute()->fetchAll();
$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