如何使用 php 处理可变数量的复选框
我的目标是处理未知数量的名称(2 到 8 之间)并以良好的方式对其进行格式化。 每个用户的名称数量各不相同。因此,一个用户将看到 4 个名称,另一个用户将仅看到 2 个名称。 我将展示一个包含 3 个名称的示例。
<input type="checkbox" name="inuser[]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[]" id="u3" value="Lotte" /><label for="u3">Lotte</label>
假设罗莎的复选框被选中。我想将“Rosa”存储在表中。 假设罗莎和弗洛里斯被检查了。现在我想存储“Floris and Rosa” 假设所有三个都已检查。比起我想存储“Floris、Rosa 和 Lotte”,
我对数组没有那么丰富的经验。所以这对于我来说是一个无法解决的问题。 我尝试通过阅读教程来解决这个问题,但我需要有人帮助我。
让我尝试解释为什么名称的数量是可变的......在该网站上,学生将能够在那里记录学校项目活动。他们成群结队地工作,但人数不同。因此,每个“项目”都会有不同数量的学生参与其中。
当他们记录一项活动时,可以检查执行该活动的学生,服务器应将其转换为字符串(例如:“Floris、Rosa 和 Lotte”)并将其存储在日志表中。这样稍后在服务器上就能检索到日志信息(What、When、Who 等)。
请随意纠正我糟糕的英语。我来自荷兰。
My goal is to process an unknown amount of names (between 2 and 8) and format it in a nice way.
The amount of names varies per user. So one user will see 4 names, another will see only 2 names.
I'll show an example with 3 names.
<input type="checkbox" name="inuser[]" id="u1" value="Floris" /><label for="u1">Floris</label>
<input type="checkbox" name="inuser[]" id="u2" value="Rosa" /><label for="u2">Rosa</label>
<input type="checkbox" name="inuser[]" id="u3" value="Lotte" /><label for="u3">Lotte</label>
Say Rosa's checkbox is checked. Than I want to store 'Rosa' in the table.
Say Rosa and Floris are checked. Now I want to store 'Floris and Rosa'
Say all three are checked. Than I want to store 'Floris, Rosa and Lotte'
I'm not that experienced with arrays. So this is an unsolvable problem for me.
I've tried to solve it by reading tutorials, but I need someone to help me a bit.
Let me try to explain why the amount of names are variable... On the site students will be able to log there school project activities. They work in groups, but the amount of people differ. So every 'project' will have a different amount of students working on it.
When they log an activity the students who carried out the activity can be checked and the server should turn this into a string (for example: 'Floris, Rosa and Lotte') and store it in the log table. So later on the server will be able to retrieve the log information (What, When, Who, etc.)
Feel free to correct my terrible English. I'm from Holland.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是另一个可能有帮助的代码示例:
Here's another code example that might help:
目前尚不完全清楚您到底在哪里遇到问题,但您面临的问题之一(我认为...)是,当您发布表单时,不会发布未选中的复选框,因此您的数组
$_POST['inuser']
将具有可变长度,并且您不能依赖索引来访问您的值:Lotte
,则$_POST['inuser' ][0]
将是'Lotte'
Floris
和Lotte
,则$_POST['inuser'][0]
将'Floris'
和$_POST['inuser'][1]
将是'Lotte'
我将向表单添加索引- 字段,以便您可以轻松检查某个用户是否是检查过。那么这个值就不再重要了,处理你的数组会更容易:
现在你可以检查:
你可以在它周围包裹一个循环来循环所有用户。
It´s not completely clear where you are having a problem exactly, but one of the problems you are facing (I think...), is that un-checked checkboxes are not posted when you post your form, so your array
$_POST['inuser']
will have variable lengths and you cannot rely on the indices to access your values:Lotte
is checked,$_POST['inuser'][0]
will be'Lotte'
Floris
andLotte
are checked,$_POST['inuser'][0]
will be'Floris'
and$_POST['inuser'][1]
will be'Lotte'
I would add an index to the form-fields so that you can easily check if a certain user is checked. Then the value does not really matter anymore and processing your array will be easier:
Now you can check:
You can wrap a loop around it to loop through all your users.
好吧,如果您要循环遍历可能的复选框,那么您可以循环遍历可能的发布数据。
例如:
将为您提供复选框(参见此处)。
然后对于您的帖子数据:
希望这有用。
Well, if you're looping through the possible checkboxs, then you can loop through the possible post data.
For instance:
Will give you your check boxes (see here).
Then for your post data:
Hope this is of use.