/**
* @name : Data Siswa Module
* @author : Irfanudin Ridho
* @email : irfan.ub@gmail.com
* @date : February 22, 2011
* @version : 2.0
*
*/
1. Database structure: data_siswa
id int not null auto_increment key,
no_induk int,
nama char(50),
tanggal_lahir char(50),
kelas char(2),
alamat char(50)
2. info file: data_siswa.info
; This module provide implementation for data siswa accessname = Data Siswa
description = Data siswa is a module that provide access data to database
version = 2.0
package = Schools
core = 7.x
3. module file: data_siswa.module
<?php
function data_siswa_menu(){
$items = array();
$items['sekolah/siswa'] = array(
'title' => 'Data Siswa',
'page callback' => 'list_siswa',
'access callback' => TRUE,
);
$items['sekolah/siswa/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
// kelas satu secondary menu tab
$items['sekolah/siswa/list/satu'] = array(
'title' => 'Kelas 1',
'type' => MENU_LOCAL_TASK,
'page callback' => 'list_satu',
'access callback' => TRUE,
'weight' => 1,
);
// kelas dua secondary menu tab
$items['sekolah/siswa/list/dua'] = array(
'title' => 'Kelas 2',
'type' => MENU_LOCAL_TASK,
'page callback' => 'list_dua',
'access callback' => TRUE,
'weight' => 2,
);
// kelas satu secondary menu tab
$items['sekolah/siswa/list/tiga'] = array(
'title' => 'Kelas 3',
'type' => MENU_LOCAL_TASK,
'page callback' => 'list_tiga',
'access callback' => TRUE,
'weight' => 3,
);
$items['sekolah/siswa/add'] = array(
'title' => 'Add data',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('add_form'),
'access callback' => TRUE,
'weight' => 2,
);
$items['sekolah/siswa/edit'] = array(
'title' => 'Edit Data',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('edit_form'),
'access callback' => TRUE,
'weight' => 3.
);
return $items;
}
function list_siswa(){
$i = 1;
$output = '';
$output .= '<table>';
$output .= '<tr><th>No</th><th>No Induk</th><th>Nama</th><th>Tanggal Lahir</th><th>Kelas</th><th>Alamat</th></tr>';
$select = db_select('data_siswa','c')->fields('c')->execute()->fetchAll();
foreach($select as $value){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$value->no_induk.'</td>';
$output .= '<td>'.$value->nama.'</td>';
$output .= '<td>'.$value->tanggal_lahir.'</td>';
$output .= '<td>'.$value->kelas.'</td>';
$output .= '<td>'.$value->alamat.'</td>';
$output .= '</tr>';
}
return $output.'</table>';
}
function list_satu(){
$i = 1;
$output = '';
$output .= '<table>';
$output .= '<tr><th>No</th><th>No Induk</th><th>Nama</th><th>Tanggal Lahir</th><th>Kelas</th><th>Alamat</th></tr>';
// selecting both kelas 1A and kelas 1B
$select = db_select('data_siswa','c')->fields('c')->condition('kelas',db_like('1').'%','LIKE')->execute()->fetchAll();
foreach($select as $value){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$value->no_induk.'</td>';
$output .= '<td>'.$value->nama.'</td>';
$output .= '<td>'.$value->tanggal_lahir.'</td>';
$output .= '<td>'.$value->kelas.'</td>';
$output .= '<td>'.$value->alamat.'</td>';
$output .= '</tr>';
}
return $output.'</table>';
}
function list_dua(){
$i = 1;
$output = '';
$output .= '<table>';
$output .= '<tr><th>No</th><th>No Induk</th><th>Nama</th><th>Tanggal Lahir</th><th>Kelas</th><th>Alamat</th></tr>';
// selecting both kelas 2A and kelas 2B
$select = db_select('data_siswa','c')->fields('c')->condition('kelas',db_like('2').'%','LIKE')->execute()->fetchAll();
foreach($select as $value){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$value->no_induk.'</td>';
$output .= '<td>'.$value->nama.'</td>';
$output .= '<td>'.$value->tanggal_lahir.'</td>';
$output .= '<td>'.$value->kelas.'</td>';
$output .= '<td>'.$value->alamat.'</td>';
$output .= '</tr>';
}
return $output.'</table>';
}
function list_tiga(){
$i = 1;
$output = '';
$output .= '<table>';
$output .= '<tr><th>No</th><th>No Induk</th><th>Nama</th><th>Tanggal Lahir</th><th>Kelas</th><th>Alamat</th></tr>';
// selecting both kelas 3A and kelas 3B
$select = db_select('data_siswa','c')->fields('c')->condition('kelas',db_like('3').'%','LIKE')->execute()->fetchAll();
foreach($select as $value){
$output .= '<tr>';
$output .= '<td>'.$i++.'</td>';
$output .= '<td>'.$value->no_induk.'</td>';
$output .= '<td>'.$value->nama.'</td>';
$output .= '<td>'.$value->tanggal_lahir.'</td>';
$output .= '<td>'.$value->kelas.'</td>';
$output .= '<td>'.$value->alamat.'</td>';
$output .= '</tr>';
}
return $output.'</table>';
}
function add_form($form, &$form_state){
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Add new data'),
);
$form['fieldset']['no_induk'] = array(
'#type' => 'textfield',
'#title' => t('No Induk'),
);
$form['fieldset']['nama'] = array(
'#type' => 'textfield',
'#title' => t('Nama'),
);
$form['fieldset']['tanggal_lahir'] = array(
'#type' => 'textfield',
'#title' => t('Tanggal Lahir'),
);
$form['fieldset']['kelas'] = array(
'#type' => 'select',
'#title' => t('Kelas'),
'#options' => drupal_map_assoc(array('1A','1B','2A','2B','3A','3B')),
);
$form['fieldset']['alamat'] = array(
'#type' => 'textfield',
'#size' => 60,
'#title' => t('Alamat'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function add_form_submit($form, &$form_state){
$no_induk = $form_state['values']['no_induk'];
$nama = $form_state['values']['nama'];
$tanggal_lahir = $form_state['values']['tanggal_lahir'];
$kelas = $form_state['values']['kelas'];
$alamat = $form_state['values']['alamat'];
$fields = array(
'no_induk' => $no_induk,
'nama' => $nama,
'tanggal_lahir' => $tanggal_lahir,
'kelas' => $kelas,
'alamat' => $alamat,
);
$return = db_insert('data_siswa')->fields($fields)->execute();
if($return){
drupal_set_message('New added data successfully inserted:<br/>No Induk: '.$no_induk.'<br/>Nama: '.$nama.'<br/>Tanggal Lahir: '.$tanggal_lahir.'<br/>Kelas: '.$kelas.'<br/>Alamat: '.$alamat);
}else{
drupal_set_message('Error to add data!');
}
}
function edit_form($form, &$form_state){
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Edit Data'),
);
return $form;
}
No comments:
Post a Comment