/**
* @author : irfanudin ridho
* @email : irfan.ub@gmail.com
* @version : 1.0
* @date : February 20, 2011
*
*
* This module provides implementation for storing and accessing
* student data in drupal module.
*/table structure: data_siswa
id int not null auto_increment key,
name char(100)
age int,
city char(100)info file: data_siswa.info
; A module for implementing database access
name = Data Siswa
description = A module on how interact with database
version = 1.0
package = Luna
core = 7.x
name = Data Siswa
description = A module on how interact with database
version = 1.0
package = Luna
core = 7.x
module file: data_siswa.module
<?php
/**
* Implementing hook_menu()
* This is implementation of main menu in the navigation menu,
* and on top of the page - primary menu
*/
function data_siswa_menu(){
$items = array();
$items['luna/data'] = array(
'title' => 'Data Siswa',
'page callback' => 'list_data',
'access callback' => TRUE,
);
$items['luna/data/list'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'List',
'weight' => 0,
);
$items['luna/data/add'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Add',
'page callback' => 'drupal_get_form',
'page arguments' => array('data_siswa_add_form'),
'access callback' => TRUE,
'weight' => 2,
);
$items['luna/data/edit'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('data_siswa_edit_form'),
'access callback' => TRUE,
'weight' => 3,
);
return $items;
}
/**
* This function is used for rendering the page for displaying the * data that available on the database
*/function list_data(){
$output = '<table>';
$output .= '<tr><th>Name</th><th>Age</th><th>City</th><th>Edit</th><th>Delete</th></tr>';
$select = db_select('data_siswa','c')->fields('c')->execute()->fetchAll();
foreach($select as $key){
$output .= '<tr><td>'.$key->name ."</td><td>". $key->age. "</td><td>". $key->city ."</td>";
$output .= "<td><a href=\"edit/".$key->id."\">Edit</a></td><td><a href=\"delete/".$key->id."\">Delete</a></td></tr>";
}
$output .= "</table>";
return $output;
}
/**
* This function is used to render the add form on the
* add primary menu tab
*/function data_siswa_add_form($form, &$form_state){
$form['data'] = array(
'#type' => 'fieldset',
'#title' => t('Add Data'),
);
$form['data']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
);
$form['data']['age'] = array(
'#type' => 'textfield',
'#title' => t('Age'),
);
$form['data']['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
/**
* This function is used for processing data after submit button
* is pressed.
*/ function data_siswa_add_form_submit($form, &$form_state){
$age = (int) $form_state['values']['age'];
$data = array(
'name' => $form_state['values']['name'],
'age' => $age,
'city' => $form_state['values']['city'],
);
$return = db_insert('data_siswa')->fields($data)->execute();
if($return)
drupal_set_message(t('Okey For Inserting Data'));
else
drupal_set_message(t('Error For Inserting Data'));
}
/**
* This function is used for rendering the form
* for editing purpose
*/function data_siswa_edit_form($form, &$form_state){
$names = get_all(); // array
$form['edit'] = array(
'#type' => 'fieldset',
'#title' => t('Edit The Data'),
);
$form['edit']['selects'] = array(
'#type' => 'select',
'#title' => t('Select the name'),
'#options' => drupal_map_assoc($names),
);
foreach($names as $key=>$value){
$form['edit'][$value] = array(
'#type' => 'fieldset',
'#title' => t('Edit The One'),
'#states' => array(
'visible' => array(
':input[name="selects"]' => array('value' => $value),
),
),
);
$form['edit'][$value]['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#value' => $value,
'#states' => array(
'visible' => array(
':input[name="selects"]' => array('value' => $value),
),
),
);
$form['edit'][$value]['age'] = array(
'#type' => 'textfield',
'#title' => t('Age'),
'#states' => array(
'visible' => array(
':input[name="selects"]' => array('value' => $value),
),
),
);
$form['edit'][$value]['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#states' => array(
'visible' => array(
':input[name="selects"]' => array('value' => $value),
),
),
);
}
return $form;
}
/**
* This function used for getting all the names in
* the data_siswa table and return it as an array
*/function get_all(){
$select = NULL;
$select = db_select('data_siswa','c')->fields('c')
->execute()->fetchAll();
$data = array();
$i=0;
foreach($select as $key){
$data[$i] = $key->name;
$i++;
}
return $data;
}
No comments:
Post a Comment