编辑: 我解决了我的问题: 感谢大家的评论,它帮助我以不同的方式思考: 我不知道返回...; (我相信它只是发送价值没有结束功能),所以就像我说的,我刚刚开始,我是一个新手... 我解决了我的问题与多维数组,我认为它应该像我现在要。我在这篇文章的最后发布了代码。PHP:函数()检查表索引数组
--------------------------------------------- ---------------
我想做一个基本的检查窗体函数,将用于检查我未来的网站上的所有窗体。它花了我几几小时(几天...)来做这个短代码,因为我是PHP的新手,我刚刚开始几天前。 这里是想法(请注意,表单更大,我只是为了调试而创建了一个更小的版本)。 PHP库包含有需要()的HTML页面:
function check_form() {
$n = 0;
$indexed = array_values($_POST);
// How should I do to make this loop,
// to edit the HTML of each input one by one,
// without modifying other input that the one corresponding
// to the $n position in the table ?
if (isset($indexed[$n])) {
if (!empty($indexed[$n])) {
$value = $indexed[$n];
$icon = define_article_style()[0];
$color = define_article_style()[1];
} else {
$value = define_article_style()[4];
$icon = define_article_style()[2];
$color = define_article_style()[3];
}
$form_data_input = array($value, $icon, $color);
return $form_data_input;
}
}
HTML表单:
<form role="form" method="post" action="#checked">
<div class="form-group <?php echo check_form()[1]; ?>">
<label class="sr-only" for="title">Title</label>
<input type="text" name="title" class="form-control input-lg" id="title" placeholder="Enter title * (min: 5 characters, max: 30 characters)" value="<?php echo check_form()[0]; ?>">
<?php echo check_form()[2]; ?>
</div>
<div class="form-group <?php echo check_form()[1]; ?>">
<label class="sr-only" for="another">Title</label>
<input type="text" name="another" class="form-control input-lg" id="another" placeholder="Enter title * (min: 5 characters, max: 30 characters)" value="<?php echo check_form()[0]; ?>">
<?php echo check_form()[2]; ?>
</div>
<button type="submit" class="btn btn-default" name="check_article" value="#checked">Check</button>
</form>
我的问题是,返回的值到输入端始终的内容“title”,例如如果
$array[0] = $indexed[$index];
$array[1] = define_article_style()[0];
$array[2] = define_article_style()[1];
为“标题”是等于
$array[0] = "blabla";
$array[1] = "myclass1";
$array[2] = "myclass2";
每隔一个输入(在这种情况下,它只是“另一个”)的形式的具有相同的值。 我有点困惑,我不明白一切是如何运作的,但我认为这与输入分享$array[n]
以获得它们的价值/风格的事实有关,但我并不真正了解如何保留这个概念,并使其工作。 所以,如果你明白什么不在这里工作,我会对解释感兴趣(请记住,我是代码中的新手,还有PHP。) 谢谢。 问候。
-------------------------------------------- ----------------
这是我的工作代码(我必须验证,但我认为它工作正常)。
function check_form() {
$n = 0;
$index_post = array_values($_POST);
while ($n < count($index_post)) {
if (isset($index_post[$n])) {
if (!empty($index_post[$n])) {
$value[$n] = $index_post[$n];
$color[$n] = define_article_style()[0];
$icon[$n] = define_article_style()[1];
} else {
$value[$n] = define_article_style()[4];
$color[$n] = define_article_style()[2];
$icon[$n] = define_article_style()[3];
}
}
$array_all = [$value, $color, $icon];
$n = $n + 1;
}
return $array_all;
}
再次,感谢您的回答,很好看,即使是新手不明白的一半时,他们使用此功能在这个其他功能在这里得到答案,他们做了什么。 重拳出击。 问候。
有太多的困惑在这里,我不知道从哪里开始array_values回报0基于数字索引的数组,所以你的if语句是没有意义的。接下来,return语句将退出函数,所以你永远不会循环多次 – Steve 2014-09-13 14:36:28