0

i am creating a custom module **

form_example.module

**

<?php
function form_example_permission() {
  return array(
    'submit form_example' => array(
      'title' => t('Submit form_example'),
      'description' => t('Submit the form_example form'),
    ),
    'access form_example submissions' => array(
      'title' => t('Access form_example submissions'),
      'description' => t('Access the form_example submissions'),
    ),
  );
}
function form_example_menu() {
  $items = array();

  $items['form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Employess Entry Form', //page title
    'description' => "A form to submit the employee's data.",
    'type' => MENU_NORMAL_ITEM,
    'access arguments' => array('submit form_example'),
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'),

      );

 $items['form-submissions'] = array(
    'title' => t('List'),
    'page callback' => 'form_example_submissions',
    'access callback' => TRUE,
    'access arguments' => array('access form_example submissions'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function form_example_form($form, &$form_state) {

$form['empcode'] = array(
  '#weight' => '0',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Code'),
);
$form['empname'] = array(
  '#weight' => '1',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Name'),
);
$form['empemail'] = array(
  '#weight' => '2',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Email'),
);
$form['empdesignation'] = array(
  '#weight' => '3',
  '#required' => '1',
  '#type' => 'textfield',
  '#title' => t('Employee Designation'),
);
$form['file'] = array(
  '#weight' => '4',
  '#type' => 'file',
  '#size' => '30',
  '#title' => t('Employee Photo'),
);
 $form['submit'] = array(
     '#weight' => '5',
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

return $form;

}

function form_example_form_validate($form, &$form_state) {
  /*if (!($form_state['values']['price'] > 0)){
    form_set_error('price', t('Price must be a positive number.'));
  }*/
 // print('<pre>'.print_r($form_state['values'],1).'</pre>');
  //die();
 $file = file_save_upload('file', array(
    'file_validate_is_image' => array(),
    'file_validate_extensions' => array('png gif jpg jpeg'),
  ));
  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $form_state['values']['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }
}

function form_example_form_submit($form, &$form_state) {
   $file=$form_state['values']['file'];
  unset($form_state['values']['file']);
  $file->status = FILE_STATUS_PERMANENT;
  file_save($file);
 // drupal_set_message(t('The form has been submitted and the image has been saved, filename: @filename.', array('@filename' => $file->filename)));


 $fe_id = db_insert('form_example')
        ->fields(array(

          'empcode' => $form_state['values']['empcode'],
          'empname' => $form_state['values']['empname'],
          'empemail' => $form_state['values']['empemail'],
          'empdesignation' => $form_state['values']['empdesignation'],
          'empphoto' => $file->filename,
        ))
        ->execute();
        drupal_set_message(t('Successfully added'));

}
function form_example_theme($existing, $type, $theme, $path) {
  return array(
    'form_example_function' => array(
      'variables' => array('rows' => NULL),
    ),
    'form_example_template' => array(
      'variables' => array('rows' => NULL),
      'template' => 'form_example_template',
    ),


  );
}

/* view the form submissions*/
function form_example_submissions()
{
  $results =  db_query("Select * from {form_example}");
   $rows = array();
  foreach($results as $result)
  {
    $rows[] = array(
        $result->fe_id,
        $result->empcode,
        $result->empname,
        $result->empphoto,
        $result->empemail,
         $result->empdesignation,
      );

  }
  return theme('form_example_function',array('rows' => $rows));
}
/*function theme_form_example_function($variables)
{
  print_r($variables);
//$output ="<h2>".print_r$variables."</pre></h2>";
/*  foreach ($variables as $result)
  {

    $output = "<h2><pre>".print_r($result)."</pre></h2>";
  }
return $output;
 $output = '';

    foreach($variables['rows'] as $row) {
        foreach($row as $values) {

          print_r($values);
         $output .= "Empcode: " . $values . "</br> ";
         // $output .= "EmpName: " . $values[1] . " </br>";
        //$output .= "Emphot: " . $values[2] . "\n";
        }
    }
    return $output;

}*/
function theme_form_example_function($variables)
{
    $output = '';
print_r($variables);
    foreach($variables['rows'] as $row) {
      //  echo $path;
      // $output .= "Empcode: " . (isset($row[1]) ? $row[1] : "--") . " ";
      //  $output .= "EmpName: " . (isset($row[2]) ? $row[2] : "--") . " ";
      //  $output .= "Emphot: " . (isset($row[3]) ? $row[3] : "--") . "<br>";   

$img_url = 'public://'.(isset($row[3]) ? $row[3] : "--");  // the orig image uri
$style = 'medium';  // or any other custom image style you've created via /admin/config/media/image-styles



     $output .= '<div class="views-row"><div class="do_featured">
  <div style="position: relative;" class="do_image"> 
            <div class="field field-name-field-thumb-image field-type-image field-label-hidden"><div class="field-items"><div class="field-item "><img  src="'.image_style_url($style, $img_url).'" ></div></div></div></div>
  <div class="do_title">
    <span>    <div class="field"><div class="field-items"><b>'.(isset($row[2]) ? $row[2] : "--") .'</b></div></div>
 <div class="field"><div class="field-items">Emp Code: &nbsp;</div><div class="field-items">'.(isset($row[1]) ? $row[1] : "--") .'</div></div> 
 <div class="field"><div class="field-items">'.(isset($row[4]) ? $row[4] : "--") .'</div></div> 
 <div class="field"><div class="field-items">'.(isset($row[5]) ? $row[5] : "--") .'</div></div> 



      </span> 
   </div></div></div>';



    }

    return $output;
}


?>

I want to show only filters on form-submissions like below:

enter image description here

After filter the data screen will shown like below

enter image description here

i want to know how can i add these filter on page and search the result on database and shown on page

-1

I am adding an example for this. Hope this will helps you.

function form_example_list(){
    $search_form =drupal_get_form('form_example_search_form');
    $content = '<div class="block" >'.drupal_render($search_form).'</div>';
    $content .= '<div style="clear:both" ></div>';

    $query = db_select('form_example', 'pd');
    if(isset($_GET['empname']) && $_GET['empname'] != ''){
        $query->condition('pd.empname', $_GET['empname'],'=');
    }
    if(!empty($_GET['empcode'])){
        $query->condition('pd.empcode', $_GET['empcode'],'=');
    }
    if(isset($_GET['empdesignation']) && $_GET['empdesignation'] != ''){
        $query->condition('pd.empdesignation', $_GET['empdesignation'],'=');
    }
    $query->fields('pd');
    //$table_sort = $query->extend('TableSort')->orderByHeader($header);
    //$pager = $table_sort->extend('PagerDefault')->limit(10);

    $result = $query->execute();
 return $content;
}
function form_example_search_form($form, &$form_state){

    $form['#method'] = 'get';
    $form['#token'] = FALSE;
    $form['#form_build_id'] = FALSE;
    $form['#form_id'] = FALSE;
    $form['empname'] = array(
        '#type' => 'textfield',
        '#title' => t('empname'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empname'])?$_GET['empname']:'',
    );
    $form['empcode'] = array(
        '#type' => 'textfield',
        '#title' => t('empcode'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empcode'])?$_GET['empcode']:'',
    );
    $form['empdesignation'] = array(
        '#type' => 'textfield',
        '#title' => t('empdesignation'),
        '#attributes' => array( 'maxlength' => '128', 'size'=> '30'),
        '#prefix' => '<div class="form-inline views-exposed-form"> <div class="views-exposed-widget">',
        '#suffix' => '</div>',
        '#default_value' => isset($_GET['empdesignation'])?$_GET['empdesignation']:'',
    );
    //views-exposed-widget

    $form['submit'] = array(
        '#type' => 'submit',
        '#attributes' => array( 'name' => ''),
        '#prefix' => '<div class="views-exposed-widget">',
        '#suffix' => '</div></div>',
        '#value' => 'Apply',
     );  

    return $form;
}

// Fetch data as per you requirement means you may need like operator. Depend on situations.

After getting the result you can design as per your requirement.

  • its give me error undefined header and one more thing how can i call through url, list function is call through url? – Sumit Aggarwal Sep 22 '16 at 11:20
  • I have updated my answer. You do not need header because your result output is different from table. and yes you need to call through url. – DRUPWAY Sep 22 '16 at 11:29
  • thanks buddy its working fine now and i have to study it to learn it :) – Sumit Aggarwal Sep 26 '16 at 5:04
  • @smaluition: can you please tell me how can i add pagination in it – Sumit Aggarwal Sep 26 '16 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.