我正在尝试使用数组输出具有以下函数的表单:
public function createArrayForm($table, $do, $formDesc = '', $id, $array, $markFields = false) {
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) && $do == 'insert') {
$out .= '<form action="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&table=' . $table . '" method="post" class="form-horizontal" ' . $formAppend . '>';
$out .= '<div class="form-desc">' . $formDesc . '</div>';
$out .= $markFields ? '<h3>Input Fields</h3>' : '';
foreach ($array as $type => $fieldname) {
if ($type == 'input') {
$out .= generateInputField($fieldname);
}
}
$out .= $markFields ? '<h3>Content Fields</h3>' : '';
foreach ($array as $type => $fieldname) {
if ($type == 'textarea') {
$out .= generateTextarea($fieldname, $cke);
}
}
$out .= $markFields ? '<h3>Images Fields</h3>' : '';
foreach ($array as $type => $fieldname) {
if ($type == 'image') {
$out .= generateImgField($fieldname);
}
}
$out .= form_hidden('user_data', '1');
$out .= form_hidden('id', self::generateID());
$out .= form_close();
return $out;
}
并致电:
$arr = array("textarea"=>"project_name", "input"=>"created", "input"=>"last_modified", "input"=>"published");
echo $automate->createArrayForm('projects', 'insert', 'Some form desc', '123', $arr, true);
但它只输出:
什么时候应该是这样的:
仅返回每个中的一个,例如输入。而不是它的所有实例。所以"input"=>"created", "input"=>"last_modified", "input"=>"published"
应该输入三个输入,但它只返回一个。
答案 0 :(得分:1)
您正在重复使用数组键。所以
$arr = array("textarea"=>"project_name", "input"=>"created", "input"=>"last_modified", "input"=>"published");
最终看起来像这样:
$arr = array("textarea"=>"project_name", "input"=>"published");
相反,请将代码修改为以下内容:
$arr = array("textarea"=>array("project_name"), "input"=>array("created", "last_modified", "published"));
然后取出那些单独的数组,并迭代它们。
foreach ($array['input'] as $fieldname) { // etc and so on
答案 1 :(得分:1)
在PHP中,您不能拥有共享密钥的数组。
您最好创建一个简单的数组,并创建子条目,以便维护顺序,但可以有多个输入/ textarea。
像这样:
$arr = array(
array('type' => 'textarea', 'name' => 'project_name'),
array('type' => 'input', 'name' => 'created'),
array('type' => 'input', 'name' => 'published'),
array('type' => 'input', 'name' => 'last_modified')
)
这也允许您添加比类型/名称更多的参数。
答案 2 :(得分:-1)
问题是你对数组中的所有内容使用相同的键。 可能性是交换价值和关键
$arr = array("textarea"=>"project_name", "input"=>"created", "input"=>"last_modified", "input"=>"published");
[...]
foreach ($array as $fieldname => $type) {
if ($type == 'textarea') {
$out .= generateTextarea($fieldname, $cke);
}
}
[...]