I have used variable_get()
, and variable_set()
in my form search, but when I visit another page, and come to the module again, it shows the previous results. How can I free the variable at the time of reloading the module?
/**
* @file
* The theme system, which controls the output of Drupal.
*/
function myform_help($path, $arg) {
switch ($path) {
case "admin/help#myform":
return '<p>' . t("MyForm Module") . '</p>';
break;
}
}
function myform_menu() {
$items['myform/newform'] = array(
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('myform_newform'),
'access callback' =>TRUE
);
return $items;
}
function myform_newform($form, &$form_state) {
//Create a list of headers for your Html table (see Drupal 7 docs for theme_table here
$form['search'] = array(
'#type' => 'textfield',
'#title' => t('Search for Members'),
'#size' => 60,
'#maxlength' => 128
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#submit' => array('myform_newform_submit'),
);
$header = array(
// The header gives the table the information it needs in order to make
// the query calls for ordering.
array('data' => t('ID'), 'field' => 't.id'),
array('data' => t('First Name'), 'field' => 't.fname'),
array('data' => t('Last Name'), 'field' => 't.lname'),
);
if((variable_get('results'))!="") {
$query = db_select('myform', 't')
->extend('TableSort') //Sorting Extender
->orderByHeader($header)
->extend('PagerDefault')
->condition('lname',variable_get('results'),'=')
->limit(2)
->fields('t', array('id', 'fname', 'lname'));
$results = $query->execute();
foreach($results as $node)
{
$rows[$node->id]=array('data'=>(array)$node);
}
}
else {
$rows = array();
}
$form['pager_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('Empty Rows')
);
// attach the pager theme
$form['pager_pager'] = array('#theme' => 'pager');
return $form;
}
function myform_newform_submit($form,&$form_state) {
variable_set('results',$form_state['values']['search']);
}