Php 从数据库生成表单
我试着自学Drupal,我发现了一些我找不到任何教程的东西 我正在尝试生成一个具有动态文本字段数的表单,以填充和编辑自定义表的内容。在常规PHP中,我将通过以下方式实现这一点:Php 从数据库生成表单,php,drupal,drupal-6,Php,Drupal,Drupal 6,我试着自学Drupal,我发现了一些我找不到任何教程的东西 我正在尝试生成一个具有动态文本字段数的表单,以填充和编辑自定义表的内容。在常规PHP中,我将通过以下方式实现这一点: $count = '0'; while ($row = mysql_fetch_array ($result) { echo "<input type='text' name='title_row".$count."' value='".$row['title']."'>" $count = $cou
$count = '0';
while ($row = mysql_fetch_array ($result) {
echo "<input type='text' name='title_row".$count."' value='".$row['title']."'>"
$count = $count +1;
}
$count='0';
while($row=mysql\u fetch\u数组($result){
回声“”
$count=$count+1;
}
有人能告诉我一些东西,告诉我如何在Drupal中做到这一点(以及如何处理提交的数据)
谢谢为此,请检查和
示例的简单版本如下所示:
/**
* Form builder function - creates definition of form
*/
function yourModule_table_edit_form($form_state) {
$form = array();
// TODO: Add code/query to populate $result, using the
// Drupal DAL functions, e.g.:
$result = db_query('SELECT * FROM {your_table}');
while ($row = db_fetch_array($result) {
// Create one textfield per row
// NOTE: assumes content of title column can be used as an array index
$form[$row['title']] = array(
'#type' => 'textfield',
'#title' => $row['title'],
'#default_value' => $row['value'], // NOTE: Just assumed the column name
'#size' => 60, // Adjust as needed
'#maxlength' => 60, // Adjust as needed
// NOTE: more options for input element definition available - check the API docs
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
/**
* Form submit function
*/
function yourModule_table_edit_form_submit($form, &$form_state) {
foreach ($form_state['values'] as $row_title => $value) {
// TODO: Do something with the submitted values
}
}
(注意:未经测试的代码,注意打字错误和其他错误)
要准备输出表单,可以调用
drupal\u get\u form('yourModule\u table\u edit\u form')
您希望如何处理数据?您可以在文本字段上使用允许多个条目的CCK吗?Drupal也有一个数据库API;Drupal代码应该使用数据库抽象API,并使用db\u query()
,db\u query\u range()
,db\u fetch\u数组,而不是使用特定于数据库的函数()
,db_fetch_object()
,等等@kiamlaluno:好的观点-我相应地调整了示例。很抱歉延迟响应。它看起来适合我正在尝试的内容,所以我会在表安装/卸载工作正常后尝试。如果您仍在观看,我将开始实现此功能