脱离 foreach 循环
我正在开发一个网站,其中根据用户数量生成行。在此示例中,我有三个用户。基本上,我使用下拉选择数据通过 $_POST 传递数据。这是我要传递给 PHP 的内容。它们被包裹在
...
<select name="taction[3]" >
<option value="accept">Accept</option>
<select name="taction[4]" >
<option value="accept">Accept</option>
<select name="taction[6]" >
<option value="accept">Accept</option>
...
我的 php 看起来像这样:
$total = 1;
foreach ($_POST['taction'] as $userid => $action)
{
if ($action == "accept")
{
if ($total<1)
{
break;
}
else
{
echo $userid."foo";
$total = ($total - 1);
}
}
}
出于某种原因,它仍然显示三个“foo”,而它应该在第一个“foo”之后停止。我做错了什么?
I'm working on a website wherein rows are generated depending on how many users there are. In this example, I have three users. Basically, I pass data through $_POST using drop down select data. Here's what I'm passing to PHP. These are wrapped in <form>
but I cleaned it to show just the important data.
...
<select name="taction[3]" >
<option value="accept">Accept</option>
<select name="taction[4]" >
<option value="accept">Accept</option>
<select name="taction[6]" >
<option value="accept">Accept</option>
...
My php looks like this:
$total = 1;
foreach ($_POST['taction'] as $userid => $action)
{
if ($action == "accept")
{
if ($total<1)
{
break;
}
else
{
echo $userid."foo";
$total = ($total - 1);
}
}
}
For some reason, it is still displaying three "foo's" when it should've stopped after the first "foo". What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其更改为
或者从 0 开始 $total 变量。
Change it to
Or start the $total variable at 0.
感谢您的建议。我的错误是依赖于 foreach 语句之外的变量。我必须再次复制循环内的操作才能注册添加的数据。
Thanks for your suggestions. I was at fault by relying on variables outside the foreach statement. I had to copy the operations inside the loop again for it to register the added data.