I am creating a custom search block, I have created a form in the block. Here is my form_submit. I need to print a html table of some of the fields in the $result array. I am using Drupal_set_message(), I need to show the results in a table instead, so that user can print the page

$text = $form_state['values']['text'];
$query = db_select('table_1', 'n');
$query->fields('n', array('id'));
 $query->fields('n', array('phone'));
$query->fields('n', array('name')); 
$query->condition('n.p_id', $text, '=');
$column_name = $query->execute(); 
  • You might want to elaborate on what you're asking. Are you wanting to know how to print an HTML table in drupal_set_message() or parse out the results of DB query?
    – Shawn Conn
    Aug 30, 2016 at 21:57
  • I want to print a html table with drupal_set_message() or in any other way.
    – ana
    Aug 31, 2016 at 3:45

1 Answer 1


I used an if statement in the form to check if submit is pressed and gave the results in the if block.

function hook_form($form, &$form_state) {
if (!empty($form_state['values']['text'])) {//here i am checking if the text field is empty
    $header=array('one'=>t('one'),'two'=>t('two'));//your header
$column_name = $query->execute()->fetchAssoc();//fetchAssoc to get results in an associative array.
//here get the fields from $column name into rows
$row = array( 
    'two' => $column_name[""],
//your form elements after submit is pressed.  

else {
//your original form here.
 $form['text'] = array(
'#type' => 'textfield'
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Button'),

 return $form;

in you form submit add this


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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