Hi,

I'm creating a module using drupal 7.

I have a problem outputing some data in a function.

my problem is in function evaluation_section() how do i output form and table at the same time.

Thank you in advance for your help. very much appreciated. :D

/**
 * Implements hook_menu().
 */
function evaluation_menu() {
  $items['evaluate'] = array(
    'title' => 'Student Evaluation',
    'page callback' => 'evaluation_section',
    'access arguments' => array('access evaluation'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}


/**
 * Menu page callback function
 */
function evaluation_section(){
  
  $section = arg(1);
  if (empty($section)){
	$section = 'search';
  }
  
  if ($section == 'result'){
  	$firstname = arg(2);
	$lastname = arg(3);
	
        /**
           * My problem is here. I don't know how to output form and table at the same time.
           */

	$output  = drupal_get_form('evaluation_search_student_form',$firstname,$lastname);
	$output .= evaluation_get_student_regisration($firstname,$lastname);
	return $output;
		
  }elseif ($section == 'registration'){
	// code for getting student registration
  }elseif($section == 'search'){	
	return drupal_get_form('evaluation_search_student_form',NULL,NULL);	
  }	
}


/**
 * Generate student form.
 */
function evaluation_search_student_form($form, $form_state,$firstname = NULL,$lastname = NULL){
  $form['evaluate'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search Student'),
    '#weight' => 5,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['evaluate']['firstname'] = array(
    '#type' => 'textfield',
    '#title' => t('First Name'),
    '#default_value' => $firstname,
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
  );
  $form['evaluate']['lastname'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name'),
    '#default_value' => $lastname,    
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
  );
  $form['evaluate']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search')
  );
  return $form;	
}

/**
 * Validate search student form
 */
function evaluation_search_student_form_validate($form, &$form_state) {
  $first_name = $form_state['values']['firstname'];
  $last_name = $form_state['values']['lastname'];
  
  if (empty($first_name)) {
    form_set_error('firstname', 'Please enter student\'s first name.');
  }elseif (empty($last_name)){
  	form_set_error('lastname', 'Please enter student\'s last name.');	
  }
}

/**
 * Process form submit
 * Get the entered first name or last name
 */
function evaluation_search_student_form_submit($form_id, &$form_state) {
  drupal_goto('evaluate/result/'.$form_state['values']['firstname'].'/'.$form_state['values']['lastname']);
}

function evaluation_get_student_regisration($firstname,$lastname){
  
  $firstname="%".$firstname."%";
  $lastname="%".$lastname."%";
  
  $query="SELECT CODE,ACADEMIC.DBO.fnFullname(LASTNAME,FIRSTNAME,MIDDLENAME) AS STUDENTNAME,ACADEMIC.DBO.fnGetLastCourseTaken(CODE) AS LASTCOURSE,ACADEMIC.DBO.fnGetStudentLastSchoolyear(CODE) as SCHOOLYEAR,ACADEMIC.DBO.fnGetStudentLastSemester(CODE) as SEMESTER FROM ACADEMIC.dbo.STUDENTMASTER WHERE LASTNAME LIKE :lastname AND FIRSTNAME LIKE :firstname";
  $result = db_query($query, array(':firstname' => $firstname,':lastname' => $lastname));
  

  $header = array(t('Code'),t('Student Name'),t('Current'),NULL);
  $rows=array();

  foreach ($result as $record){
    $row    = array();	
    $row[]  = $record->CODE;
    $row[]  = $record->STUDENTNAME;
    $row[]  = $record->LASTCOURSE;
    $row[]  = l('View Registration','evaluate/student/'.$record->CODE.'/'.$record->SCHOOLYEAR.'/'.$record->SEMESTER);;
    $rows[] = $row;
  }

  $output = theme_table(array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => NULL,
    'caption' => NULL,
    'colgroups' => null,
    'sticky' => TRUE,
    'empty' => t('No registration found.')));
	  
  return $output; 
}

Comments

bakyildiz’s picture

Here is a working code for Drupal 7. I hope helps you.

function biletlegelsin_permission() {
  return array(
      'access reports' => array(
            'title' => t('Access any report'),
            'description' => t('View any report'),
    ),
  );
}
/**
* Implements hook_menu().
*/
function biletlegelsin_menu() {
  $items = array(); 
   /*
   $items['salesreportform'] = array(
    'title' => t('Daily Sales Report'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('sales_report_form'),
    'access callback' => TRUE,
    'description' => 'Biletle Gelsin Sales Report',
  );
  */
  $items['salesreport'] = array(
        'title' => 'Sales Report',
        'description' => 'Biletle Gelsin Sales Report.',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('sales_report_form'),
        'access arguments' => array('access reports'),
    );
    $items['salesreportpage/%report_date'] = array(
        'title' => 'Sales Report',
        'description' => 'Biletle Gelsin Sales Report.',
        'page callback' => 'sales_report_page',
        'page arguments' => array(1),
        'access arguments' => array('access reports'),
    );
  return $items;
}


function sales_report_page($report_date) {
    $build = array();
    $rows = array();
    $header = array('PNR', 'Order Id', 'Authorization','Amount','Date-Time',);
   
    global $user;

    $results = db_query(
          "SELECT * FROM sales WHERE uid = :uid and DATE_FORMAT(timestamp,'%d-%m-%Y') = :timestamp",
          array(':uid' => $user->uid, ':timestamp' => $report_date)
         );
   
    foreach ($results as $row) {
        $rows[] = array(
          $row->pnr,
          $row->order_id,
          $row->auth_code,
          $row->amount,
          $row->timestamp,
        );
    }
   
    $variables = array('header' => $header, 'rows' => $rows);
    $content = theme('table', $variables, array());
   
    $build['content'] = array(
        'this_does_not_matter_too_much2' => drupal_get_form('sales_report_form'),        
        'this_does_not_matter_too_much1' => array(
                    '#markup' => $content,
                ),
    );
   
    $build['pager'] = array(
        '#theme' => 'pager',
        '#weight' => 5,
    );
 
  return $build;
}

function sales_report_form($form, &$form_state) {
   $form = array();
       $form['report_date'] = array(
        '#type' => 'textfield',
        '#title' => t('Report Date:'),
        '#description' => t('Format:23-05-2011'),
        '#size' => 12,
      );

      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
      );
    return $form;
}

function sales_report_form_validate($form, &$form_state) {
}

function sales_report_form_submit($form, &$form_state) {
    $form_state['rebuild']=TRUE;
    drupal_goto('salesreportpage/'.$form_state['values']['report_date']);
}