Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By ton_cut345 on
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
Submit form with table working sample...
Here is a working code for Drupal 7. I hope helps you.