如何循环请求并使用动态变量命名? PHP! :)
我有一个表单提交了多人的详细信息,即在我得到的 $_REQUEST 中:
title1 = Mr,
first_name1 = 'Whatever',
surname1 = 'Whatever',
title2 = Mr,
first_name2 = 'Whatever',
surname2 = 'Whatever'
显然还有更多,但这解释了情况。可能有 10 个人被提交,因此它会上升到 title10、first_name10、surname10...
我一直在尝试使用:
for ($x = 1; $x < 4; $x++) {
$a = new applicant();
$a->title = $_REQUEST['title'+$x];
$a->first_name = $_REQUEST['first_name'+$x];
$a->surname = $_REQUEST['surname'+$x];
$a->Save();
}
但是看来您不能执行此 +$x 位。我知道有一种解决方法,因为我记得很久以前就做过这个,但我没有工作代码:/
有什么想法吗?
I have a form that submits details of multiple people i.e in the $_REQUEST I get :
title1 = Mr,
first_name1 = 'Whatever',
surname1 = 'Whatever',
title2 = Mr,
first_name2 = 'Whatever',
surname2 = 'Whatever'
There's obviously more but this explains the situation. There could be ten people being submitted and therefore it would go up to title10, first_name10, surname10...
I have been trying to use:
for ($x = 1; $x < 4; $x++) {
$a = new applicant();
$a->title = $_REQUEST['title'+$x];
$a->first_name = $_REQUEST['first_name'+$x];
$a->surname = $_REQUEST['surname'+$x];
$a->Save();
}
However it appears that you cannot do this +$x bit. I know there is a way around it since I remember doing this ages ago yet I don't have my code at work :/
Any ideas guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 使用
.
来连接字符串,使用+
来添加数字;这与其他一些使用+
的语言不同。可能令人困惑,但不太可能改变。'title' + $x
将尝试添加各个部分,就像它们是数字一样,并在必要时进行转换。<代码>'标题'。 $x 应该做你似乎正在寻找的事情。
另请阅读:精细手册
PHP uses
.
for concatenating strings, and+
for adding numbers; this is different from some other languages which use+
for both. Possibly confusing, but unlikely to change.'title' + $x
will try to add the parts as if they were numbers, casting if necessary.'title' . $x
should do what you seem to be looking for.Read also: The Fine Manual
PHP 中的字符串连接使用运算符
.
而不是+
。例如:$_REQUEST['title' 。 $x];
String concatenation in PHP uses the operator
.
not+
. For example:$_REQUEST['title' . $x];
您可以在 HTML 表单中对多个输入使用相同的属性名称,如下所示:
当提交到 PHP 脚本时,该属性名称将在 GET/POST 中作为数组提供。
You can use the same attribute name in your HTML form for mulitple inputs like so:
Which when submitted to the PHP script will be available in the GET/POST as an array already.
你可以尝试这个:
或者类似的东西
无论如何,你明白了。
you may try this:
or something like
Anyway, you got the idea.