Pages

Monday, February 28, 2011

Drupal Theme - iPad Them

file ipad.info
name = iPad Theme
description = This is just affect style only
core = 7.x
package = Apple
engine = phptemplate
stylesheets[all][] = style.css

file style.css
/**
 * This theme just theming the drupal base system because
 * these module doesn't have any template file
 *
 * @name    : iPad Theme
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : March 1, 2011
 */


Web App - Coloring V2

<div id="page">
<div id="satu">For the last moment we get from here</div>
<table>
<tr><td>Foreground</td></tr>
<tr><td><input type="text" id="fore" size="30px" onkeyup="foring(this.value)" onfocus="this.value=''" value="Enter color name . . ." onBlur="getTip(this)"/></td></tr>
<tr><td>Background</td></tr>
<tr><td><input type="text" id="back" size="30px" onkeyup="backing(this.value)" onfocus="this.value=''" value="Enter color name . . ." onblur="getTip(this)" /></td></tr>
</table>
<p>Tips:
<br/>
> Use Tab for moving the cursor to the next box.<br/>
> Use Shift+Tab for moving the cursor to the previus box.<br/>
> Enter color name like blue or enter hex color rgb code like #f0f, #ff00ff
</p>
</div>
 
<style>
#page { -moz-box-shadow: 0 0 4px #aaa; padding: 10px; -moz-border-radius: 4px; }
#satu { -moz-border-radius: 4px; -moz-box-shadow: 0 0 4px #aaa; padding: 10px; width: 500px; height: 30px; margin: 0 auto; text-align: center; font: 12px sans-serif; background: pearl; font-weight: bold;  }
table td { font: 12px arial; }
input { -moz-box-shadow: 0 0 4px #ccc; -moz-border-radius: 4px; border: 1px solid #ccc; padding: 2px; font: 12px arial; }
input:hover { -moz-box-shadow: 0 0 4px orange; }
input:focus { -moz-box-shadow: 0 0 4px #ccc; }
p { font: 12px sans-serif; color: #444; background: ivory;  -moz-border-radius: 4px; padding: 10px; -moz-box-shadow: 0 0 4px #aaa; }

</style>

<script>
function foring(color){
 document.getElementById('satu').style.color = color;
}

function backing(color){
  document.getElementById('satu').style.backgroundColor = color;

}

function getTip(id){
 if(id.value.length==0){
  id.value = 'Enter color name . . .';
 }
}
</script>

Web App - Coloring V1

/**
 * This program will check in real time what looks like the color
 * you enter to affect foreground and background. Just copy this
 * code in http://htmledit.squarefree.com
 *
 * @name    : real time checker for color
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : March 1, 2011
 */


<div id="page">
    <div id="satu">For the last moment we get from here</div>
    <table>
        <tr>
            <td>Foreground</td>
        </tr>
        <tr>
            <td><input type="text" id="fore" size="30px"
                onkeyup="foring(this.value)"/></td>
        </tr>
        <tr>
            <td>Background</td>
        </tr>
        <tr>
            <td><input type="text" id="back" size="30px"
                onkeyup="backing(this.value)"/></td>
        </tr>
    </table>
</div>

<style>
#page { -moz-box-shadow: 0 0 4px #aaa; padding: 10px; -moz-border-radius: 4px; }
#satu { -moz-border-radius: 4px; -moz-box-shadow: 0 0 4px #aaa; padding: 10px; width: 500px; height: 30px; margin: 0 auto; text-align: center; font: 12px sans-serif; background: pearl; font-weight: bold;  }
table td { font: 12px arial; }
input { -moz-box-shadow: 0 0 4px #ccc; -moz-border-radius: 4px; border: 1px solid #ccc; padding: 2px; font: 12px arial; }
input:hover { -moz-box-shadow: 0 0 4px orange; }
input:focus { -moz-box-shadow: 0 0 4px #ccc; }
</style>

<script>
function foring(color){
    document.getElementById('satu').style.color = color;
}

function backing(color){
    document.getElementById('satu').style.backgroundColor = color;
}
</script>

OpenGL - Coordinate System

/**
 * This program will draw coordinate system line
 * x line and y line with their corresponding strips
 */
 
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define LIMIT 20.0      // limit
#define CENTER (LIMIT - LIMIT - 1)


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

void createCoordinate(){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    glEnd();

    // creating strips
    int i=0;
    for(i=0;i<20;i++){
    glColor3f(0.1, 0.0, 1.0);
    glBegin(GL_LINES);
        // horizontal strips
        glVertex2f(i, 0.10);
        glVertex2f(i, -0.10);

        // vertical strips
        glVertex2f(0.10, i);
        glVertex2f(-0.10, i);
    glEnd();
    }
}

// initialization setting
void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(CENTER, LIMIT, CENTER, LIMIT); // -x1, x2, -y1, y2
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);


    createCoordinate();
    draw();


    glFlush();
}

// drawing area
void draw(){
    // your code here
}


int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();


    return 0;

}

OpenGL - Histogram

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>
using namespace std;

#define LIMIT 20.0      // limit
#define CENTER (LIMIT - LIMIT - 1)


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object
void createBox(float, float, float, float);

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(CENTER, LIMIT, CENTER, LIMIT); // -x1, x2, -y1, y2
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);

    draw();
    createCoordinate();

    glFlush();
}


void draw(){

    createBox(1.0, 1.0, 2.0, 10.0);
    createBox(4.0, 1.0, 2.0, 15.0);
    createBox(7.0, 1.0, 2.0, 13.0);
    createBox(10.0, 1.0, 2.0, 9.0);
    createBox(13.0, 1.0, 2.0, 7.0);
    createBox(16.0, 1.0, 2.0, 10.0);
   
}

void createBox(float x, float y, float width, float height){
    glBegin(GL_POLYGON);
        glVertex2f(x, y);
        glVertex2f(x, y+height);
        glVertex2f(x+width, y+height);
        glVertex2f(x+width, y);
    glEnd();
}

void createCoordinate(){
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    glEnd();
}

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();



    return 0;

}

Friday, February 25, 2011

Drupal Module - User View V1

<?php
/**
 * This module provide as viewer for drupal user in a block
 *
 * @type     : drupal module - block, db
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : February 26, 2011
 */
 
/**
 * Implements hook_block_info()
 */
function user_view_block_info(){
    $block['view_user'] = array(
        'info' => t('User View'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'sidebar_first',
        'visibility' => 1,
        'pages' => '*',
        'status' => NULL,
    );
   
    return $block;
}

Drupal Module - Dosen Mata Kuliah

<?php

/**
 * This module show how to impelements JOIN operation. 
 * There's two table
 * Table dmk_dosen:
 *         id int not null auto_increment key,
 *        dosen char(50)
 * Table dmk_mata_kuliah:
 *        id int not null auto_increment key,
 *         mata_kuliah char(20)
 *
 * @type     : drupal module - db, menu
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : February 25, 2011
 */
 
/**
 * Implements hook_menu()
 */
function dosen_matkul_menu(){
    $items = array();
    $items['dosen'] = array(
        'title' => 'Dosen Mata Kuliah',
        'page callback' => 'get_dosen_matkul',
        'access callback' => TRUE,
    );
   
    return $items;
}


/**
 * render the dosen and mata kuliah data to table
 */
function get_dosen_matkul(){
    $output = '';
   
    $dosen = db_select('dmk_dosen')->fields('dmk_dosen')
            ->execute()->fetchAll();
    $matkul = db_select('dmk_mata_kuliah','mk')->fields('mk')
            ->execute()->fetchAll();
   
    $output .= '<div>';
   
    // table of dosen
    $output .= '<div><h2>Daftar Dosen</h2>';
    $output .= '<table><tr><th>No</th><th>Dosen</th></tr>';
   
    $i = 1;
    foreach($dosen as $key){
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$key->dosen.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table></div>';
   
    // table of mata kuliah
    $output .= '<div><h2>Daftar Mata Kuliah</h2>';
    $output .= '<table><tr><th>No</th><th>Mata Kuliah</th></tr>';
   
    $j = 1;
    foreach($matkul as $key){
        $output .= '<tr>';
        $output .= '<td>'.$j++.'</td>';
        $output .= '<td>'.$key->mata_kuliah.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table></div>';
   
    $output .= '</div>';
   
    return $output;
}

Drupal Module - Data Siswa V4

<?php
/**
 * 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;
}


Drupal Module - Data Siswa V3

<?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;
}


Thursday, February 24, 2011

jQuery Plugin - Button V2

/**
 * This plugin provide service to render a button
 * so that it will look more appealing
 *
 * @type    : jQuery Plugin
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 2.0
 * @date    : February 25, 2011
 */

(function($){

$.fn.button = function(){

    $(this).addClass('button');
   
    $(this).mouseover(function(){
        $(this).addClass('button-hover');
    });
   
    $(this).mouseout(function(){
        $(this).removeClass('button-hover');
    });
   
    $(this).mousedown(function(){
        $(this).addClass('button-down');
    });

    $(this).mouseup(function(){
        $(this).removeClass('button-down');
    });
   
    $(this).mouseout(function(){
        $(this).removeClass('button-down');
    });
   
 }
})(jQuery);



The css file:

.button { background: #aaf; padding: 5 10px; 
          font: 14px sans-serif; font-weight: bold; 
          color: white; -moz-border-radius: 5px; 
          border: 1px solid #aaf; }
.button-hover { background: #88f; cursor: pointer; }
.button-down { background: #51f; } 

jQuery Plugin - Button

/**
 * This plugin render the button looks like more appealing
 *
 * @type    : jQuery pluggin
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 25, 2011
 */

(function($){

$.fn.button = function(){

    $(this).addClass('button');
   
    $(this).mouseover(function(){
        $(this).addClass('button-hover');
    });
   
    $(this).mouseout(function(){
        $(this).removeClass('button-hover');
    });
   
 }
})(jQuery);


This is the css file:
.button { background: #aaf; padding: 5 10px; 
          font: 14px sans-serif; font-weight: bold; 
          color: white; -moz-border-radius: 5px; 
          border: 1px solid #aaf; }
.button-hover { background: #88f; cursor: pointer; }

Drupal Module - Ajax Sample

<?php

/**
 * This module provide example for ajax moduling
 *
 * @name    : ajax db access
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 25, 2011
 * @type    : drupal module - ajax database
 */

/**
 * Implements hook_menu()
 */
function mac_menu(){
    $items['mac'] = array(
        'title' => 'Mac Ajax',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_ajax'),
        'access callback' => TRUE,
    );
   
    return $items;
}

Drupal Module - Data View In Block

<?php

/**
 * This module showing on how to show the data on table
 * It querying data in the database and then viewing it as 
 * table to user at module front end
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 24, 2011
 * @type    : drupal module - block data view
 */

/**
 * This defines general setting for the module
 */
function ipad_block_info(){
    $block['ipad_apple'] = array(
        'info' => t('iPad Module'),        // this viewed in admin page
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'header',
        'visibility' => 1,
        'pages' => '*',
        'status' => TRUE,
    );
   
    return $block;
}

/**
 * This act as roles like an interface, then implemented in
 * the ipad_content
 */
function ipad_block_view($delta=''){
    switch($delta){
        case 'ipad_apple':
            $block['subject'] = t('Hello World');        // viewed in front top module block
            $block['content'] = ipad_content($delta);
            break;
    }
   
    return $block;
}

/**
 * This is what displayed in the content of block
 */
function ipad_content($switch_delta){
    $render = get_name_city();
   
    switch($switch_delta){
        case 'ipad_apple':
            return $render;
    }   
}

/**
 * This functin has task for accessing the data in the table
 * and then present it as table.
 * the structure of the table is like this one:
 * name of table: data
 * id int not null auto_increment key,
 * name char(20),
 * city char(20)
 */
function get_name_city(){
    $query = db_select('data')->fields('data')->execute()->fetchAll();
    $output = '<table><tr><th>Name</th><th>City</th></tr>';
   
    foreach($query as $key){
        $output .= '<tr>';
        $output .= '<td>'.$key->name.'</td>';
        $output .= '<td>'.$key->city.'</td>';
        $output .= '</tr>';
    }
   
    $output .= '</table>';
   
    return $output;
}

Drupal Module - iPad Block

<?php

/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 24, 2011
 * @type    : drupal module - block
 */
 

/**
 * This defines general setting for the module
 */
function ipad_block_info(){
    $block['ipad_apple'] = array(
        'info' => t('This is iPad module'),
        'cache' => DRUPAL_CACHE_PER_ROLE,
        'region' => 'sidebar_first',
        'visibility' => 1,
        'pages' => '*',
        'status' => TRUE,
    );
   
    return $block;
}

/**
 * This act as roles like an interface,
 * then implemented in the ipad_content
 */
function ipad_block_view($delta=''){
    switch($delta){
        case 'ipad_apple':
            $block['subject'] = t('Hello World');
            $block['content'] = ipad_content($delta);
            break;
    }
   
    return $block;
}

/**
 * This is what displayed in the content of block
 */
function ipad_content($switch_delta){

    $form['indonesia'] = array(
        '#type' => 'textfield',
        '#title' => 'Enter A Name',
        '#size' => 20,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Search',
    );
   
    switch($switch_delta){
        case 'ipad_apple':
            return $form;
    }
}

Wednesday, February 23, 2011

Drupal Module - Mata Kuliah

<?php
/**
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 23, 2011
 * @name    : iphone
 */

/**
 * This module provide implementation of the access to the table
 *
 * There are available of the 4 table:
 * a. user        -> listing available of user
 * b. mk        -> listing of all offered mata kuliah(subject)
 * c. pilihan    -> listing of all selected mata kuliah
 * d. ava        -> listing of all non-selected mata kuliah
 *
 * The structures is like below:
 * user table:
 *         id int not null auto_increment key,
 *         username char(50)
 * mk table:
 *         id int not null auto_increment key,
 *         mata_kuliah char(50),
 *         hari char(10)
 * pilihan table:
 *         id int not null auto_incrment key,
 *         uid int,
 *         mkid int,
 * ava table:
 *         id int not null auto_increment key,
 *         uid int,
 *         mkid int,
 */

Monday, February 21, 2011

Drupal Module - Data Siswa V2

/**
 * @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 access
name = Data Siswa
description = Data siswa is a module that provide access data to database
version = 2.0
package = Schools
core = 7.x


Saturday, February 19, 2011

CodeIgniter App - Login Page

/**
 * Implementation of login page using CodeIgniter
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : 1.0
 * @date    : February 20, 2010
 */

<?php

class Blog extends CI_Controller{
   
    function index(){
        header("Location: blog/page");
    }
   
    // login form
    function page(){
        $this->load->view('blog/front');
    }
   
    // check the login validation  
    function check(){
        $username = $_POST['username'];
        $pass = $_POST['password'];
        // add more data
        $data = array(
            'username' => $username,
            'log_in' => TRUE,   // for the next checking
        );
        if(strcmp($username,"admin") == 0  && strcmp($pass,"bali") == 0){
            $this->load->library('session');
            $this->session->set_userdata($data);
            header("Location: hello");
        }else{
            header("Location: page");
        }
    }
   
    // authenticated page
    function hello(){
        $this->load->library('session');
        if(!$this->session->userdata('log_in')){
            header("Location: page");
        }
       
        echo "<h1>You are log in</h1>";
       
        echo "<a href=\"logout\">Go Logout</a>";
    }
   
    // logout page
    function logout(){
        $this->load->library('session');
        $this->session->unset_userdata('log_in');
        header("Location: page");
    }
}


blog/front.php - view files
<div id="login">
  <h1>Login</h1>
  <table>
   <form action="check" method="post">
    <tr><td>Username</td>
        <td>:</td>
        <td><input type="text" name="username"/></td></tr>
    <tr><td>Password</td>
        <td>:</td>
        <td><input type="password" name="password"/></td></tr>
    <tr><td colspan="3">
        <input type="submit" value="Log In"/></td></tr>
   </form>
  </table>
</div>

Drupal Module - Data Siswa V1

/**
 * @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




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;
}

Thursday, February 17, 2011

Drupal Module - Inset Data

This module provide implementations for insert data to database
0. structure of table data
mysql>> CREATE TABLE data
     >> (id INT NOT NULL AUTO_INCREMENT KEY,
     >> name CHAR(50),
     >> city CHAR(50));

1. data.info
name = Module Data Insert
description = Module for inserting data to database
package = Data Access
version = 1.0
core = 7.x

2. data.module
<?php
/**
 * Implements hook_menu()
 */
function data_menu(){
    $items = array()

    $items['data/main'] = array(
        'title' => 'Insert Data',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('data_insert_form'),
        'access callback' => TRUE,
    );

    return $items;
}

/**
 * Form to get the data input
 */
function data_insert_form( $form, &$form_state){
    $form['name'] = array(
         '#type' => 'textfield',
         '#title' => t('Name'),
    ),
    $form['city'] = array(
         '#type' => 'textfield',
         '#title' => t('City'),
    ),
    $form['submit'] = array(
        '#type' => 'textfield',
        '#value' => t('Insert To Database'),
    );

    return $form;
}


/**
 * Handling submit form
 */
function data_insert_form_submit($form, &$form_state){

    // get the value from field submitting
    $entry = array(
        'name' => $form_state['values']['name'],
        'city' => $form_state['values']['city'],
    );

    // insert to database
    $return = db_insert('data')->fields($entry)->execute();

    if($return){
        drupal_set_message("Your data has been insert successfully");
    }else{
        drupal_set_message("Oops. Unable to save");
    }
}