好吧,我正在編程一個網站,我需要做很多的HTML表單無處不在。我怎樣才能減少我這麼做的時間?有任何想法嗎?我想這應該用功能來完成。
我既沒有使用任何框架,也沒有使用OOP。
非常感謝。使用php如何輕鬆創建表單?
2
A
回答
3
請谷歌「禪編碼」。我認爲這是你想要的。例如:
如果你輸入div#content>h1+p
,將產生下面的HTML:
<div id="content">
<h1></h1>
<p></p>
</div>
禪編碼是由很多編輯的支持。
2
我的建議是儘早使用框架,因爲您會發現很多工作已經完成。 CodeIgniter或Zend並不錯。如果不是,我會自己寫幾個類,給定一些參數可以呈現所需的html。從長遠來看,親自獲得框架是一個更有吸引力的選擇。
3
不幸的是,目前還沒有神奇功能,所以你會花費一些時間就可以了。下面是一些FormBuilders你可以使用,而無需使用一個框架:
- http://pear.php.net/package/HTML_QuickForm2
- http://pear.php.net/package/DB_DataObject_FormBuilder
- http://framework.zend.com/manual/en/zend.form.html
注意Zend_Form
是Zend框架,但can be used standalone的一部分。儘管如此,它對其他ZF組件仍有一定的依賴性。
0
可能看看 http://pear.php.net/package/HTML_QuickForm2
越大抽象(越容易使用),則少管你將不得不在最終輸出。所以它可能取決於你的表格將會有多複雜,這將決定你可以使用的工具。
0
幾年前,我寫了一個小班來創建基於數據庫表的表單。
這裏就是抓住所有字段從數據庫表&字段類型的方法:
這裏是基於該數據生成表格的方法:
/**
* 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>";
}
毫無疑問那裏必須有更多優雅的解決方案,但是如果表單的目的是填寫數據庫表格,那麼生成表單非常容易。
相關問題
- 1. 用PHP輕鬆創建XML文檔
- 2. PHP庫輕鬆創建進程(fork)
- 3. 如何輕鬆創建xml架構
- 4. 如何輕鬆創建這個矩陣?
- 5. 如何使用Web表單輕鬆創建和編輯XML文檔
- 6. 使用Xamarin表格輕鬆創建表格 - InvalidOperationException
- 7. 如何輕鬆填寫表單字段?
- 8. 如何從PHP代碼中輕鬆創建POST?
- 9. 如何使用純Python輕鬆創建本地SSH隧道
- 10. 在Qt中輕鬆創建動態表單/控件
- 11. 如何使用螢幕加載和/或執行時間輕鬆創建圖表?
- 12. 如何使用jQuery在Phonegap中輕鬆點擊每行來創建表格
- 13. 如何使用python輕鬆過濾csv?
- 14. 如何更輕鬆地使用reCAPTCHA?
- 15. 用於輕鬆創建html日曆表的插件
- 16. ipad - 如何輕鬆創建登錄對話框?比如這個?
- 17. 使用輕靈還是不輕鬆?
- 18. 創建和使用數據庫並輕鬆安裝的GUI
- 19. 如何使用yum輕鬆地爲PHP添加posix支持?
- 20. 如何輕鬆地從PHP中使用Web服務
- 21. 如何 - 輕鬆過濾cakephp表
- 22. 如何輕鬆創建多個REMOTE SQL服務器?
- 23. 如何在iOS中輕鬆創建動畫?
- 24. 如何輕鬆地在Eclipse中創建模型/ UML?
- 25. 如何輕鬆創建出一個類的接口?
- 26. 如何在R中輕鬆創建示波器變量
- 27. 如何輕鬆地從lambda函數創建比較器?
- 28. 如何從控制器內輕鬆創建DropDownList
- 29. 如何從差異向量中輕鬆創建差異矩陣?
- 30. 如何輕鬆創建xml格式的字符串數組?