好的,我正在编写一个网站,我需要在各处做很多html表单。我怎样才能减少这样做的时间?有任何想法吗?我想应该用功能完成。
我既没有使用任何框架也没有使用OOP
非常感谢你。
答案 0 :(得分:3)
请谷歌“ Zen Coding ”。我想这就是你想要的。例如:
如果输入div#content>h1+p
,将生成以下html:
<div id="content">
<h1></h1>
<p></p>
</div>
Zen Coding得到了很多编辑的支持。
答案 1 :(得分:3)
不幸的是,还没有神奇的FormBuilder::readMyMind()
功能,因此您将花费一些时间。以下是一些您可以使用的FormBuilders,而无需使用框架:
请注意Zend_Form
是Zend Framework的一部分,但是can be used standalone。它确实对其他ZF组件有许多依赖。
答案 2 :(得分:2)
我的建议是尽快开始使用框架,因为您会发现很多工作已经为您完成。 CodeIgniter或Zend并不错。如果不是,我自己写了几个类,给定一些参数可以渲染所需的html。从长远来看,个人获得框架是一个更具吸引力的选择。
答案 3 :(得分:0)
可以看看 http://pear.php.net/package/HTML_QuickForm2
抽象越大(越容易使用),那么对最终输出的控制就越少。因此,它可能取决于您的表单的复杂程度,这将决定您可以使用的工具。
答案 4 :(得分:0)
几年前,我写了一个小类来创建一个基于数据库表的表单。
这是一种获取所有字段的方法。数据库表中的字段类型:
public function getDatabaseFields($db_name, $tbl, $ignoredFields)
{
$db = mysqli_select_db($this->connect, $db_name);
$sql = "DESCRIBE $tbl";
$result = mysqli_query($this->connect, $sql);
if ($result !== false)
{
$i = 0;
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
if (!in_array($row[Field], $ignoredFields))
{
$formItems[$i][lbl] = $row[Field];
$formItems[$i][type] = $row[Type];
$formItems[$i][nul] = $row["Null"];
}
$i++;
}
return $formItems;
}
else
{
return false;
}
}
这是一种基于该数据生成表单的方法:
/**
* This function gets the details of a table and
* creates a form to insert values in that table
* $ignoredFields is an array of fields which should not be in the form
* $specialFields contain complete fields ready to output, (useful if you need fields not in the table)
* $existingData are data that should be displayed in the fields
* $options[formTitle] displays a title above the form in a separate <div>
* $options[errors][fieldName] display the field's label in bold red letters
* $options[hidden][fieldName] set field as hidden and the value is $options[hidden][fieldName][value]
* @param <string> $db_name
* @param <string> $tbl
* @param <array> $ignoredFields
* @param <array> $specialFields
* @param <array> $existingData
* @param <array> $options
*/
function form_db_table($db_name, $tbl, $ignoredFields, $specialFields, $existingData, $options)
{
# Get all the database fields that must be filled out.
$formItems = $this->getDatabaseFields($db_name, $tbl, $ignoredFields);
# Generate the form fields and load them in variables
foreach ($formItems as $key=>$value)
{
# $fieldName is the actual field name in the database.
# $fieldLbl is the name displayed in the form before the field
$fieldName = $value[lbl];
$pattern = array('/([a-z])([A-Z])/','/[_-]/');
$replacement = array('$1 $2', ' ');
$fieldLbl = ucfirst(strtolower(preg_replace($pattern, $replacement, $fieldName)));
# if $fieldName is in $options[hidden], an hidden input is created
if (is_array($options[hidden]))
{
if (array_key_exists($fieldName, $options[hidden]))
{
$val = $options[hidden][$fieldName];
$formEntries .= "<input type='hidden' name='$fieldName' value='$val' />";
continue;
}
}
if($value[nul] == "YES")
{
$mandatory = "";
}
else
{
$mandatory = "*";
$mandatoryFields .= $value[lbl] . ";";
}
// from type, decide which form item to use: varchar = <input> ...
if (stripos($value[type],"varchar") !== false)
{
$varcharLimit = substr($value[type], 8, -1);
if ($varcharLimit < 71)
{
$inputItem = "<input type=\"text\" size=\"38\" maxlength=\"$varcharLimit\"".
" name=\"$fieldName\" value=\"$existingData[$fieldName]\" class=\"entryField\"/>";
}
else
{
$inputItem = "<textarea cols=\"35\" rows=\"3\" wrap=\"VIRTUAL\"" .
" name=\"$fieldName\" class=\"entryField\">$existingData[$fieldName]</textarea>";
}
}
else if (stripos($value[type],"text") !== false)
{
$inputItem = "<textarea cols=\"35\" rows=\"8\" wrap=\"VIRTUAL\"" .
" name=\"$fieldName\" class=\"entryField\">$existingData[$fieldName]</textarea>";
}
else if (stripos($value[type],"date") !== false)
{
$inputItem = "<input type=\"text\" size=\"38\" maxlength=\"50\"".
" name=\"$fieldName\" value=\"$existingData[$fieldName]\" class=\"entryField\"/>";
}
else if (stripos($value[type],"enum") !== false)
{
$inputItem = "<select size=\"1\" name=\"$fieldName\">\r\n";
if (isset($existingData[$fieldName]))
{
$inputItem .= "<option value=\"$existingData[$fieldName]\">$existingData[$fieldName]</option>";
}
$enumVal = explode(",",substr($value[type], 6, -1));
foreach($enumVal as $key => $value)
{
$val= trim(str_replace("'", "", $value));
$inputItem .= "<option value=\"$val\">$val</option>";
}
$inputItem .= "</select>";
}
## !!! COMPLETE THE LIST OF TYPES !!!
$error = $options[error][$fieldName];
$formEntries .= "<div class=\"entry\">\r\n";
$formEntries .= "<label class=\"lbl_regular\" style=\"$error\">\r\n";
$formEntries .= "$fieldLbl$mandatory</label>\r\n$inputItem \r\n";
$formEntries .= "</div>\r\n";
}
# Sends the list of mandatory fields
if ($mandatoryFields != "")
{
$mandatoryFields = substr($mandatoryFields, 0, -1);
//- Explode to determine which fields can't be blank -\\
$mandatoryFields = "<input type='hidden' name='mandatory' value='$mandatoryFields'>\r\n";
}
# Extract special fields - fields and labels ready for output
if (is_array($specialFields))
{
foreach ($specialFields as $key=>$value)
{
if($value[where]="before")
{
$specFieldsBefore .= "$value[openField] $value[lbl] $value[field]\r\n $value[closeField] \r\n";
}
else
{
$specFieldsAfter .= "$value[openField] $value[lbl] $value[field]\r\n $value[closeField] \r\n";
}
}
}
# Error message
if (isset($options[errMsg]))
{
echo "<div class=\"errorMsg\">$options[errMsg]</div>";
}
# Output the top of the form
echo $this->formTag;
if (isset($options[formTitle]))
{
echo "\r\n<div class=\"formTitle\">$options[formTitle]</div>\r\n";
}
echo "<fieldset class=\"formFieldSet\">\r\n";
#output the the actual fields
echo $mandatoryFields;
echo $specFieldsBefore;
echo $formEntries;
echo $specFieldsAfter;
# Close fieldset, add a validate button and close the form
echo "</fieldset>";
echo "<center><input type=\"submit\" value=\"Submit\" name=\"submit\" /></center>";
echo "</form>";
}
毫无疑问,必须有更优雅的解决方案,但如果表单的目的是填写数据库表,那么生成表单非常容易。