GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, February 25, 2011

Ajax Auth For Login

This is a code on how to use ajax as handler for login authentication like google login page. There are you see that when you done wrong with your username or password, Google won't reload the page till your username and password are correct.

HTML file:
<form action="some.php" method="post" onsubmit="return isAuth()">
<input type="text" name="username" id="username"/>
<input type="password" name="password" id="password"/>
<p id="msg"></p>
<input type="submit" value="Log In"/>


Javascript file:
<script>
function isAuth(){
   var username = document.getElementById('username').value;
   var password = document.getElementById('password').value;
   var msg = document.getElementById('msg');

   var ajax = new XMLHttpRequest();
   ajax.open("POST", "auth/"+username+"/"+password, false);
   ajax.send();
   var response = eval(ajax.responseText);

   if(response == 1){
      return true;
   }else if(response == 0){
      msg.innerHTML = 'Access Denied."
                 +"Please recheck your username and password";
      return false;
    }  
}
</script>

PHP file:
<?php
/**
 * check if the username and password match over what 
 * if YES
 * echo 1
 * if NO
 * echo 0
 */


Share/Bookmark

Thursday, February 24, 2011

Drupal - Database Handling

SELECT * FROM data
drupal>>db_select('data','d')
         ->fields('d')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition('name','Lady Gaga')
         ->execute()
         ->fetchAll()

SELECT * FROM data WHERE age >= 30
drupal>> $query = db_select('data','d')->fields('d')->condition('age',30,'>=')->execute()->fetchAll()

SELECT * FROM data WHERE age >=30 OR name = 'Lady Gaga'
drupal>> db_select('data','d')
         ->fields('d')
         ->condition(db_or()
                     ->condition('age',30,'>=')
                     ->condition('name','Lady Gaga'))
         ->execute()
         ->fetchAll();

SELECT * FROM data WHERE age>=30 AND id>5
drupal>> db_select('data','d')->fields('d')->condition(db_and()->
          
Share/Bookmark

Drupal - Show Result Of Tables

<?php

function ipod_menu(){
    $items = array();
   
    $items['ipod'] = array(
        'title' => 'iPod',
        'page callback' => 'show_data',
        'access callback' => TRUE,
    );
   
    return $items;
}

function show_data(){
    $query = db_select('data','d')->fields('d')->execute()->fetchAll();
    $output = '<table><th>No</th><th>Name</th><th>Age</th><th>City</th></tr>';
    $i = 1;
    foreach($query as $key){
        $output .= '<tr>';
        $output .= '<td>'.$i++.'</td>';
        $output .= '<td>'.$key->name.'</td>';
        $output .= '<td>'.$key->age.'</td>';
        $output .= '<td>'.$key->city.'</td>';
        $output .= '</tr>';
    }
    $output .= '</table>';
   
    return $output;
}

Share/Bookmark

SQL - Complicated Queries

The table structure is like this below:
1. teacher table:
   a. id
   b. name
2. subject table:
  a. id
  b. major
3. handler table: who teacher handle the major
  a. id
  b. tid   // teacher id
  c. sid   // subject id

The problem is how to get a display of the third table with naming by using just one select statement. The answer is below:
SELECT t.name, s.major FROM teacher t, subject s, handler
WHERE handler.tid = t.id AND handler.sid = s.id
Share/Bookmark

Wednesday, February 23, 2011

Drupal - Theming The Table

To render a table in Drupal, use
function get_table(){
    $header = array(t('Name'),t('City'),t('Country'));
    $rows = array(
        array(t('Bill Gates'), t('Redmond'), t('Americas')),
        array(t('Luna Maya'), t('Denpasar'), t('Indonesia')),
        array(t('Aura Kasih'), t('Jakarta'), t('Indonesia')),
    );
    $output = theme('table',array('header' => $header,
                                  'rows'   => $rows));
    return $output;
}
Share/Bookmark

Tuesday, February 22, 2011

Drupal - Theming The List

Below is the code on how to use theme for list

<?php

/**
 * Implements hook_menu()
 */

function luna_menu(){

    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'intro',
        'access callback' => TRUE,
        'expanded' => TRUE,
    );
   
    $items['luna/main/list'] = array(
        'title' => 'List',
        'page callback' => 'get_list',
        'access callback' => TRUE,
    );
   
    return $items;

}

function intro(){
    return t('This menu demos to you on how to handle with the theme hook menu');
}

function get_list(){

    $list = array();

    $list[] = t('Passing The University');
    $list[] = t('Getting The Job');
    $list[] = t('Moving To Americas');
    $list[] = t('Building The Business');
   
    $render_array['get_list'] = array(
        '#theme' => 'item_list',       // calling item list
        '#title' => t('Title Demos'),
        '#items' => $list,
        '#type' => 'ol',               // or "ul"
    );
   
    return $render_array;
}


Share/Bookmark

Drupal Page Callback With Arguments

If you want to get the value of  the arguments, please look like this code

 <?php

/**
 * Implements hook_menu()
 */
 
function luna_menu(){
    $items = array();
   
    $items['luna/main'] = array(
        'title' => 'Main Menu',
        'page callback' => 'description',
        'access callback' => TRUE,
    );
   
    $items['luna/po/%'] = array(
        'title' => 'Hello For',
        'page callback' => 'four',
        'page arguments' => array(2),
        'type' => MENU_CALLBACK,
        'access arguments' => array('access arguments page'),
    );
   
    return $items;

}

function description(){
    return array('#markup' => t('The link for this one: '
         .'<a href="@link">About Us</a>',
          array('@link'=>url('luna/po/5'),
          array('absolute'=>TRUE))       ));
}

function four($first){
    return t('You get the info with this number: @number', 
           array('@number'=>$first+10));
}
Share/Bookmark