自动生成带数组的更新表单

时间:2013-08-06 17:39:25

标签: php mysql arrays

$resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
if (!$resultUpdate) {
    self::show_error(QUERY_ERROR);
} elseif ($resultUpdate->num_rows > 0) {
    $out .= '<div class="form-desc">' . $formDesc . '</div>';
} else {
    self::show_error(QUERY_EMPTY);
}
$array = array_values($array);
print_r($array);
$out .= '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>';
while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateInputField($fieldname, $value);
        }
    }
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateTextarea($fieldname, $value, $cke);
        }
    }
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {
            $out .= generateImgField($fieldname, $value);
        }
    }
}
$arr = array("last_modified"=>"input", "published"=>"input", "content"=>"textarea");
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);

例如,当foreach部分只输出inputs时,所有字段都会在每个generateInputField输出。我知道这是因为我需要检查fieldtype(input,textarea)键是否与标记为inputtextarea的值之一匹配$fieldname的值。但我不确定如何。

我很确定我必须过滤数组,因此只有input的值进入一个单独的数组,如arrayInput,我可以将其用作in_array中的第二个参数。< / p>

2 个答案:

答案 0 :(得分:1)

如果我理解你和你的代码......

此代码

while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if (in_array($fieldname, $array)) {

将始终为您的所有行返回true,这就是您获得当前输出的原因。

相反,你应该这样做:

while ($row = $resultUpdate->fetch_assoc()) {
    foreach ($row as $fieldname => $value) {
        if ($fieldname == 'input') {
            $out .= generateInputField($fieldname, $value);
        } elseif($fieldname == 'textarea') {
            $out .= generateTextarea($fieldname, $value, $cke);
        } elseif ($fieldname == 'img') {
            $out .= generateImgField($fieldname, $value);
        }
        else{ $out = $out;}
    }
}

答案 1 :(得分:0)

有很多堆叠器的帮助......

/** 
 * Create Form With Array
 * 
 * Creates a form based on an array. If $do == update, we match fieldnames with values
 * 
 * @param string $table name of database table
 * @param string $do whether the form is an insert or update
 * @param string $formDesc form description to be echoed
 * @param array $array associative array of type (keys), and fieldnames (values)
 * @param bool $markFields whether or not to add headers for inputs, textareas .etc during insert
 * @param bool $formBrackets whether or not to prepend and append form brackets
 * @return $out html form
 *
 */
public function createArrayForm($table, $do, $formDesc = '', $id, $array, $markFields = false, $formBrackets = true) {
    if (!isset($table) && !isset($do)) {
        self::show_error('One or more parameters are missing in ' . __FUNCTION__);
    } elseif ($table == 'update' && !isset($id)) {
        self::show_error('For this form to be built, and ID must be set. Missing parameter `ID` in ' . __FUNCTION__);
    }
    if (!is_array($array)) {
        self::show_error('For this form to be built, an array must be given. Missing parameter `array` in ' . __FUNCTION__);
    }
    $result = array();
    // create two dimensional array to preserve keys that
    // otherwise would be lost with array_flip
    foreach($array as $k => $v) {
        if (array_key_exists($v, $result)) {
            $result[$v][] = $k;
        } else {
            $result[$v] = array($k);        
        }
    }
    // make sure we do not have any duplicates
    $result = super_unique($result);
    // we just need the array_values for matching with in_array
    // however we do not want to run array_values on null
    // so we check to see if the $result is a valid array first
    // if not, we just output a blank array so in_array doesn't complain
    $arrayInput = is_array($result['input']) ?  array_values($result['input']) : array();
    $arrayTextarea = is_array($result['textarea']) ?  array_values($result['textarea']) : array();
    $arrayImages = is_array($result['images']) ?  array_values($result['images']) : array();
    $out = $formBrackets == true ? '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>' : NULL;
    if($do == 'insert') {
        $out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
        $out .= $markFields && in_array('input', $array) ? '<h3>Input Fields</h3>' : NULL;
        foreach ($arrayInput as $fieldname) {
            $out .= generateInputField($fieldname);
        }
        $out .= $markFields && in_array('textarea', $array) ? '<h3>Content Fields</h3>' : NULL;
        foreach ($arrayTextarea as $fieldname) {
            $out .= generateTextarea($fieldname, $cke);
        }
        $out .= $markFields && in_array('image', $array) ? '<h3>Images Fields</h3>' : NULL;
        foreach ($arrayImages as $fieldname) {
            $out .= generateImgField($fieldname);
        }
    } elseif ($do == 'update') {
        $resultUpdate = Nemesis::select("*", $table, "id = '{$id}'");
        if (!$resultUpdate) {
            self::show_error(QUERY_ERROR);
        } elseif ($resultUpdate->num_rows > 0) {
            $out .= isset($formDesc) ? '<div class="form-desc">' . $formDesc . '</div>' : NULL;
        } else {
            self::show_error(QUERY_EMPTY);
        }
        while ($row = $resultUpdate->fetch_assoc()) {
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayInput)) {
                    $out .= generateInputField($fieldname, $value);
                }
            }
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayTextarea)) {
                    $out .= generateTextarea($fieldname, $value, $cke);
                }
            }
            foreach ($row as $fieldname => $value) {
                if (in_array($fieldname, $arrayImages)) {
                    $out .= generateImgField($fieldname, $value);
                }
            }
        }
    } else {
        self::show_error('Missing array or `do` argument in function ' . __FUNCTION__);
    }
    $out .= form_hidden('user_data', '1');
    $out .= form_hidden('id', $do == 'update' ? $id : self::generateID());
    $out .= $formBrackets == true ? form_close() : NULL;
    return $out;
}

用法:

$arr = array("last_modified"=>"input", "published"=>"input", "project_content"=>"textarea", "project_content"=>"textarea");
echo $automate->createArrayForm('projects', 'insert', 'Some form desc', '123', $arr, true);
echo $automate->createArrayForm('projects', 'update', 'Some form desc', '178514825', $arr, true);