0

I have created a form and it takes in 3 values which are Brand, Item and Zipcode. The values are queried and the results are fetched the database table. I want the results to be shown in a table

function form_example_form_submit($form, &$form_state) {
$sql3 = /*my select query*/

$result = db_query($sql3);
$records = $result->fetchAll();

//code to display table

 echo "<table border=1>";
   echo"<tr>";
    echo"<th>Retail Accounts</th>";
    echo"<th>Address</th>";
    echo"<th>City</th>";
    echo"<th>State</th>";
   echo "</tr>";
   foreach ($records as $record) {
    $record     = get_object_vars($record);
    echo "<tr>";
      echo "<td>";
        echo $record['Retail Accounts'];
      echo "</td>";
      echo "<td>";
        echo $record['ADDR'];
      echo "</td>";
      echo "<td>";
        echo $record['CITY'];
      echo "</td>";
      echo "<td>";
        echo $record['STATE'];
      echo "</td>";
    echo"</tr>";    
}
echo "</table>";
}

The result is properly fetched but not displayed on the page.

I tried $form_state['rebuild'] = TRUE; but it displays the result on top of the page.

I want the result to be visible just below my form. How can I achieve that?

Any help is very much appreciated. Thanks in advance.

  • Don't do the query in the form submit. When you request a rebuild in a form submit the form is built again, but then $form_state is filled with submitted values with which you can query the database and add the result at the end of $form. The absence of submitted values indicate you have a fresh form build and then you don't add the results. – 4k4 Oct 19 at 10:59
  • @4k4 can you help me rebuild the code? – Rony Samuel Oct 21 at 5:54
  • 1
    It's simple, the only thing you do in form submit is set rebuild. Then at the end of your form build: if (isset($form_state['values']) ) { $form['results'] = ... } is the place where you query the database and display the results. – 4k4 Oct 21 at 6:33
  • Thank you @4k4. Let me try that and post my findings here – Rony Samuel Oct 21 at 6:36
  • 1
    Did you put the result in $form? For example $form['results'] = ['#markup' => '<table> ... </table>'];. – 4k4 Oct 21 at 7:30
1

As suggested in the comment the SQl query were made on the hook_form and the hook_form_submit contains only the $form_state['rebuild'] = TRUE;. Detailed code is as follows:

function form_example_form($form, &$form_state) {

//your form fields or items

$form['submit_button'] = array(
 '#type' => 'submit',
 '#prefix' => '<div class="button-wrapper">',
 '#suffix' => '</div>',
 '#value' => t('Find my store'),
 '#attributes' => array('class' => array('btn btn-primary')),
);


/*-----Query database code Start--------*/

if(isset($form_state['values']['item-name'])){
  //get the values to variable
}

if(isset($zip_code)){


//setup the sql query


if(isset($sql3)){
  $result = db_query($sql3);
}
$records = $result->fetchAll();
$tot_records = count($records);
}

if(isset($tot_records)){
 if($tot_records>0){ 

  $form['result_table'] = array(
    '#type' => 'markup',
    '#prefix' => '<table border=0 class="table table-bordered">',
    '#markup' => '<tr><th>Store Name</th><th>Address</th><th>City</th><th>State</th></tr>',
  );

  $i=0; $j=0;  
  foreach ($records as $record) {

    $record_in_array = json_decode(json_encode($records[$i]), true); //Convert object to array

    $form[$j]['result_table1']= array(
      '#type' => 'markup',
      '#markup' => '<tr>',
    );      

    $form[$j]['result_table2']= array(
      '#type' => 'markup',
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#markup' => $record_in_array['sql column name1'],
    );
    $form[$j]['result_table3']= array(
      '#type' => 'markup',
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#markup' => $record_in_array['sql column name2'],
    );
    $form[$j]['result_table4']= array(
      '#type' => 'markup',
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#markup' => $record_in_array['sql column name3'],
    );
    $form[$j]['result_table5']= array(
      '#type' => 'markup',
      '#prefix' => '<td>',
      '#suffix' => '</td>',
      '#markup' => $record_in_array['sql column name4'],
    );


    $form[$j]['result_table6']= array(
      '#type' => 'markup',
      '#markup' => '</tr>',
    );

    $i++;
    $j++;
  }

  $form['result_table7']= array(
    '#type' => 'markup',
    '#suffix' => '</table>',
  );

  }      
 }

/*-----End Query database code--------*/

return $form;

 }

 function form_example_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
 }

Hope this helps!

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.