0

I have created a form to allow users to enter some details but I also need to display some mysql data into that form. The contents and fields of the form are displayed in a tabular format. I just want to know the right way to display mysql data in one of the table columns along with the form fields. How can i display select options in selectbox using form-api ?

2 Answers 2

2

You can make use of theme_table to make a table view. You can use form-api inside the table as you wish.

Sample code for creating table.

$header = array();
$rows = array();
$rows[] = array(
    array('data' => 'Head1', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head2', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head3', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head4', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head5', 'rowspan'=> 2, 'header' => 1),
    array('data' => 'Head6', 'colspan'=> 6, 'header' => 1),
);
$rows[] = array(
    array('data' => 'SubHead6-1', 'header' => 1),
    array('data' => 'SubHead6-2', 'header' => 1),
    array('data' => 'SubHead6-3', 'header' => 1),
    array('data' => 'SubHead6-4', 'header' => 1),
    array('data' => 'SubHead6-5', 'header' => 1),
    array('data' => 'SubHead6-6', 'header' => 1),
);

return $output .= theme('table', $header, $rows);

To make a select box with db values you can try this

$query = "SELECT * FROM {tablemanme}";
$query_result =  db_query($query);
while (($data = db_fetch_object($query_result )) !== FALSE) {
  $opt[$data->id] = $data->name;
}

$form['select'] = array(
    '#type' => 'select',
    '#options' => $opt,
);

Good luck.

4
  • Hi my from fields are displaying in a tabular format but I am not able to display mysql data in the form May 14, 2012 at 5:29
  • @SomyaGupta, What type of mysql data? Do you want to display it inside the form fields?
    – niksmac
    May 14, 2012 at 5:35
  • yes I want college names to be displayed as a form field within a table cell So the student can make a selection May 14, 2012 at 5:48
  • @SomyaGupta, See my edit.
    – niksmac
    May 14, 2012 at 6:00
-2

You can check on this link

2
  • OP is talking about D6
    – niksmac
    May 14, 2012 at 5:33
  • that's true, but in fact form API in D7 is 95% the same as the one in D6. May 14, 2012 at 6:04

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.