Wednesday, March 9, 2011

Database Drupal 7 theme_table example

Database Drupal 7 theme_table example
<?php
//;$Id$

/**
 * @file
 * Enables a single blog for an individual or multiple users.
 */
// After you learn Form API in Chapter 5, you'll be able to
// make these settings configurable.

function deductible_help($section) {
      switch ($section) {
        case 'admin/help#deductible':
          return t('This module stores deductible for given make an Model');
      }
}

function deductible_menu(){
$items['deductible/calculate']=array(
'title'=>'Calculate Deductible',
'page callback'=>'drupal_get_form',
'page arguments'=>array('deductible_simple_form'),
'access arguments' => array('administer user'),
'type' => MENU_CALLBACK,
);
//watchdog('Deductibe', '1', array(), WATCHDOG_DEBUG);
$items['deductible/display_results'] = array(
        'access arguments' => array('administer user'),
        'page callback' => 'display_results',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
);
//watchdog('Deductibe', '2', array(), WATCHDOG_DEBUG);
return $items;
}

/*
function calculator_jquery_menu() {
  $items['calculator_jquery'] = array(
    'title' => "Craig's jquery calculator module",
    'description' => 'Demonstration of various JavaScript utilities',
    'page callback' => 'calculator_jquery_p',
    'access callback' => TRUE,
  );
  return $items;
}

function calculator_jquery_p(){
  // Set the path to our script.
  $script = drupal_get_path('module', 'calculator_jquery') . '/calculator_jquery.js';

  // Include a JavaScript file.
  $js = drupal_add_js($script);

 $build = array(
 //  '#type' => 'markup',
 //  '#markup' => '<p>This is an examples page.</p>',
  );

  return $build;
}*/

/*function my_color_data_entry_form_validate($node, &$form_state) {
    $user_entered_color = $form_state['values']['color'];
    $allowed_colors = array("red","blue","white");
    if (! in_array( trim($user_entered_color),$allowed_colors,false)) {
       form_set_error('color',
          "Invalid Entry, you must enter either red, white or blue"
       );
    } else {
     drupal_set_message(
      "You submitted a valid color: ".$form_state['values']['color']
     );
    }
}*/


function deductible_simple_form($form,&$form_submit){
      $form_state['redirect'] = 'deductilbe/display_results';
      $form['Make'] = array(
        '#type' => 'textfield',
        '#title' => t('Make'),
          '#size' => 30,
             '#cols' => 30,
          '#required'=>true,
        '#default_value'=>' ',
           '#rows' => 1,
        '#maxlength'=>30,
    //    '#weight'=>1,
      );
        $form['Model'] = array(
        '#type' => 'textfield',
        '#title' => t('Models'),
        '#size' => 30,
          '#cols' => 30,
        '#required'=>true,
        '#default_value'=>' ',
        //'#weight' => 7,
        '#rows' => 1,
        '#maxlength'=>30,
    //    '#weight'=>2,
      );
      $form['Deductible'] = array(
        '#type' => 'textfield',
        '#title' => t('Deductible'),
        '#size' => 30,
          '#cols' => 30,
          '#required'=>true,
        '#default_value'=>' ',
          //'#weight' => 7,
          '#rows' => 1,
        '#maxlength'=>30,
      //  '#weight'=>3,
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
          //'#weight' => 7,
        '#submit' => array('deductible_submit'),
     //  '#weight'=>4,
      );
      return $form;
}

/**
 * Implementation of hook_perm().
 */
function deductible_perm() {
  return array('use save and edit', 'administer save and edit');
}

function display_results()
{
   // $db_result = db_query( "select name from {Deductible} ");
 /*   $db_result  = db_select('Deductible')
     ->fields(array('userid', 'Make', 'Model', 'Deductible'))
     ->orderBy('Make', 'DESC')
     ->execute();*/
   
/*    $result  = db_select('Deductible', 'c')
    ->fields('c',array('Make'))
    ->execute();*/
      $result = db_query("SELECT userid,make,model,deductible FROM {Deductible}");
     $listitems=array();
     $bigitems = array();
        foreach ($result as $node) {
         watchdog('Deductibe', 'hi', array(), WATCHDOG_DEBUG);
         $listitems=array($node->make,
            $node->model,
            $node->deductible);
           $bigitems[]=$listitems;
       }
     
    //   return print_r($bigitems);
    if (count($listitems) == 0)
        $make[] = array("No Models Entered Yet");
     else{
          watchdog('Deductibe', 'a', array(), WATCHDOG_DEBUG);
         }
       
      $link_to_add_form = l('Add More Models','deductible/calculate');
        $variable=array(
             'header' =>  array(t('Make'), t('Model'),t('Deductible')),
             'rows'  =>  $bigitems,
             'attributes' => array(),
             'caption' => NULL,
             'colgroups' => NULL,
             'sticky' => NULL,
             'empty' => NULL,
         );
        // return print_r($items);
    return theme_table($variable).'<br/>'.$link_to_add_form;
}

function deductible_submit($form, &$form_state){
  global $user;
  $userid = $user->uid;
  $Make = $form_state['values']['Make'];
  $Model = $form_state['values']['Model'];
  $Deductible = $form_state['values']['Deductible'];
/*  $calcdate = strtotime($formdate);
  $timeperiod = 90*86400;
  $formdatestore = $calcdate + $timeperiod;    */

  //      watchdog('Deductibe', 'deductible', array(), WATCHDOG_DEBUG);
   
      $insertquery = db_insert('Deductible')->fields(array(
    //  'userid' => $userid,
//      'insertdate' => $formdatestore,
      'Make'=>$form_state['values']['Make'],
       'model'=>$form_state['values']['Model'],
       'deductible'=>$form_state['values']['Deductible']
      ))->execute();
      if($insertquery!==false){
           $message = 'You have submitted the '  . ' form which contains the following data:<pre>' . print_r($form_state['values']['Make'],true) . '</pre>';
         drupal_set_message(t($message));
         $form_state['redirect'] = 'deductible/display_results';
      }else{
          drupal_set_message(t('Error-Contact Administrator'));
      }
    }

5 comments:

  1. This is a very useful demo but sometime more difficulty in this demo.please solve this difficutiy

    ReplyDelete
    Replies
    1. Yes this is right

      Delete
    2. Your demo output is not proper please check it harshal.

      Delete
  2. Niсe pοѕt. I uѕed to be
    checκing continuously this wеblog and I am іnspired!

    Very helpful info spеcially the clοsing section :
    ) I maintain such info a lot. I used to be looking for this certain іnfοrmation for a long time.
    Thanks and bеst of luck.

    Mу webpage - http://awesomeindiefilms.doobious.org/groups/every-one-of-the-you-may-need-to-know-regarding-playing-texas-holdem-poker-onine/

    ReplyDelete
  3. Τhanκs to my fatheг who told me about this ωeb site, thіs webpage іs really гemarkаble.


    my blog ρoѕt; credit card debt settlement

    ReplyDelete