0
$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.

4

2 回答 2

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;}
    }
}
于 2013-08-06T19:34:31.907 回答
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);
于 2013-08-06T23:49:03.253 回答