我决定开始或尝试开始在 OOP 中为我的 PHP 应用程序编写代码。这是我的第一堂课。这个类的目的是创建一个表单并构建不同的元素。请让我知道它是否在正确的轨道上。这些字段已经存储在数据库中。优先级参数还没有做任何事情,但会突出问题。
//Form Class
<?php
class form {
function genQuestion ($id) {
$type = $this->getQuestionType($id);
$priority = $this->getQuestionPriority($id);
if ($field['type'] == "text") {
$html .= $this->add_text_field($id, $priority);
}
if ($field['type'] == "select") {
$html .= $this->add_select_field($id, $priority);
}
if ($this->type == 'radio') {
$html .= $this->add_radio_field($id, $priority);
}
if ($field['type'] == "checkbox") {
$html .= $this->add_checkbox_field($id, $priority);
}
return $html;
}
function getQuestionType ($id) {
$field_query = "SELECT type FROM fields WHERE id = '$id'";
$field = mysql_query($field_query);
$field = mysql_fetch_assoc($field);
$this->type = $field['type'];
}
function getQuestionPriority ($id) {
$field_query = "SELECT priority FROM fields WHERE id = '$id'";
$field = mysql_query($field_query);
$field = mysql_fetch_assoc($field);
$this->type = $field['priority'];
}
function add_text_field($id) {
$fields_query = "SELECT * FROM fields WHERE id = '$id'";
$fields = mysql_query($fields_query);
$field = mysql_fetch_assoc($fields);
$content .= '<div><label> ' . $field['field'] . ': </label>';
$content .= '<input type="text" name="' . $field['id'] . '"></div>';
return $content;
}
function add_select_field($id) {
$fields_query = "SELECT * FROM fields WHERE id = '$id'";
$fields = mysql_query($fields_query);
$field = mysql_fetch_assoc($fields);
$content .= '<div><label> ' . $field['field'] . ': </label>';
$options = explode('|', $field['options']);
$content .= '<select name="' . $field['id'] . '">';
while (list($key, $value) = each($labels)) {
$content .= '<option value="' . $key . '"> ' . $value . '</option>';
}
$content .= '</select></div>';
return $content;
}
function add_radio_field($id) {
$fields_query = "SELECT * FROM fields WHERE id = '$id'";
$fields = mysql_query($fields_query);
$field = mysql_fetch_assoc($fields);
$content .= '<div style="float: left; margin: 0 auto; width: 500px; text-align: right; clear: both;"><div style="float: left; width: 200px; margin-right: 25px;"><label> ' . $field['field'] . ': </label></div>';
$options = explode('|', $field['options']);
while (list($key, $value) = each($options)) {
$content .= '<div style="float: left; margin-right: 7px;">' . $value . '</div><div style="float: left; margin-right: 17px;"><input type="radio" name="' . $field['id'] . '" value="' . $key . '" /></div>';
}
$content .= '</div>';
return $content;
}
function add_checkbox_field($id) {
$fields_query = "SELECT * FROM fields WHERE id = '$id'";
$fields = mysql_query($fields_query);
$field = mysql_fetch_assoc($fields);
$content .= '<div><label> ' . $field['field'] . ': </label>';
$content .= '<input type="checkbox" name="' . $field['id'] . '" value="' . $field['options'] . '" /> ' . $field['labels'] . '</div>';
return $content;
}
}
?>
//Controller
$fields_quesry = "SELECT * FROM fields";
$fields = mysql_query($fields_quesry);
$form = new form;
$form->genQuestion($field['id']);