2010-07-17 40 views
2

好吧,我正在編程一個網站,我需要做很多的HTML表單無處不在。我怎樣才能減少我這麼做的時間?有任何想法嗎?我想這應該用功能來完成。
我既沒有使用任何框架,也沒有使用OOP。
非常感謝。使用php如何輕鬆創建表單?

回答

3

請谷歌「禪編碼」。我認爲這是你想要的。例如:

如果你輸入div#content>h1+p,將產生下面的HTML:

<div id="content"> 
<h1></h1> 
<p></p> 
</div> 

禪編碼是由很多編輯的支持。

2

我的建議是儘早使用框架,因爲您會發現很多工作已經完成。 CodeIgniterZend並不錯。如果不是,我會自己寫幾個類,給定一些參數可以呈現所需的html。從長遠來看,親自獲得框架是一個更有吸引力的選擇。

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>"; 

} 

毫無疑問那裏必須有更多優雅的解決方案,但是如果表單的目的是填寫數據庫表格,那麼生成表單非常容易。

相關問題