我正在处理包含许多插入字段的页面。 如何缩短以下代码?
$title_1 = $_POST['title_1'];
$content_1 = $_POST['content_1'];
$link_1 = $_POST['link_1'];
$img_link_1 = $_POST['img_link_1'];
$title_2 = $_POST['title_2'];
$content_2 = $_POST['content_2'];
$link_2 = $_POST['link_2'];
$img_link_2 = $_POST['img_link_2'];
$title_3 = $_POST['title_3'];
$content_3 = $_POST['content_3'];
$link_3 = $_POST['link_3'];
$img_link_3 = $_POST['img_link_3'];
答案 0 :(得分:1)
我愿意:
$howmany = 3; // How many sets of fields are submitted.
for($i=0;$i<$howmany;$i++){
$field[$i]['title'] = $_POST['title_'.$i];
$field[$i]['content'] = $_POST['content_'.$i];
$field[$i]['link'] = $_POST['link_'.$i];
$field[$i]['img_link'] = $_POST['img_link_'.$i];
}
然后您可以$field[1]['title']
形式访问数据。
答案 1 :(得分:1)
你可以像这样遍历$ _POST数组:
foreach ($_POST as $key => $value) {
${$key} = $value;
}
这会使您的帖子变量如$_POST['title_1']
变为$title_1
请记住,您的帖子名称必须是您希望引用变量的确切名称。
答案 2 :(得分:0)
我在您编辑帖子后重新编写了这个答案。使用变量。
foreach ($_POST as $key => $val)
{
if (preg_match('/^(([a-z]+_)+\d)$/', $key, $match)
{
$$match[0] = $val;
}
}
使用[a-z0-9]
或[a-zA-Z0-9]
作为替代方案。
答案 3 :(得分:0)
您可以使用摘录(http://php.net/manual/en/function.extract.php):extract($_POST)
但是你应该小心 - 如果客户端POST user_id,或者什么?至少,您应该指定$ _POST值不会覆盖已定义的变量:extract($_POST, EXTR_SKIP)
答案 4 :(得分:0)
<?php
$key_prefixes = array (
'title',
'content',
'link',
'img_link'
);
$i = 1;
while (true) {
$post_values_missing = 0;
foreach ($key_prefixes as $key_prefix) {
$key = $key_prefix . '_' . $i;
if (!isset($_POST[$key])) {
$post_values_missing += 1;
continue;
};
$val = $_POST[$key];
// do something with $val
}
// did you get any values through this iteration?
$post_values_exist_bool = (count($key_prefixes) !== $post_values_missing);
// if not, you must've gotten them all
if (false === $post_values_exist_bool) {
break;
}
$i += 1;
}
答案 5 :(得分:0)
最简单的方法是使用PHP的POST数据处理功能为您完成工作。
考虑使用HTML表单名称,如下所示:
<form action="{url}" method="post">
<input type="text" name="data[0][title]" />
<input type="text" name="data[0][content]" />
<input type="text" name="data[0][link]" />
<input type="text" name="data[0][image_link]" />
<input type="text" name="data[1][title]" />
<input type="text" name="data[1][content]" />
<input type="text" name="data[1][link]" />
<input type="text" name="data[1][image_link]" />
...
</form>
在PHP中提取数据如下:
$data = $_POST['data'];
这会将您的PHP代码缩短为一行。该语句将直接为您提供PHP中data
表单输入的数组。 var_dump
看起来如下:
array (
0 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'),
1 => array('title'=>'...','content'=>'...','link'=>'...','image_link'=>'...'),
...
)
答案 6 :(得分:0)
您不必更改您的名字,只需将其设为数组;
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
<input type="text" name="title[]" />
<input type="text" name="content[]" />
<input type="text" name="link[]" />
<input type="text" name="image_link[]" />
PHP:
extract($_POST);
$count=count($title);
for($i=0;$i<$count;$i++) {
//You can perform your any function on this loop, to get title use $title[$i]
}