在PHP中处理动态数量的表单字段的最佳方法是什么? [英] Best way to handle dynamic amount of form fields in PHP?

查看:123
本文介绍了在PHP中处理动态数量的表单字段的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个系统,我需要在每周的每一天可以输入工作时间值的任意数量的员工列出文本字段。



<因此,我需要生成一个具有动态行数的表格,每行将包含7个文本字段。我只是想知道在将ID分配给这些字段时使用什么是最好的约定,以便在我的后端收到输入数据时轻松进行迭代?

每一行都会有一个与表示员工ID的行关联的ID号。



能够做到这样的事情真是太棒了:

  foreach($ rows as $ row)
{
$ id = $ row ['id'];

$ employee = Employee :: find($ id);

foreach($ row ['hoursWorked'] as $ dailyHours)
{
$ timecard = new Timecard();
$ timecard->小时= $ dailyHours;
$ employee-> timecards-> insert($ timecard);


$ / code $ / pre

结构我的表单和ID的最佳方式是什么在HTML方面的输入,使其尽可能无痛苦?



作为旁注,我在Laravel 3框架内工作,以便打开任何其他解决方案。 / code>将内部转换为 $ _ POST ['hoursWorked'] 下的数组。这意味着你可以做这样的事情:

 < input type =textname =hoursWorked [12345] [] /> <! - 星期日 - > 
< input type =textname =hoursWorked [12345] []/> <! - 周一 - >
< input type =textname =hoursWorked [12345] []/> <! - 星期二 - >
< input type =textname =hoursWorked [12345] []/> <! - 周三 - >
< input type =textname =hoursWorked [12345] []/> <! - 星期四 - >
< input type =textname =hoursWorked [12345] []/> <! - 星期五 - >
< input type =textname =hoursWorked [12345] []/> <! - 星期六 - >

然后,在PHP中:

<$ p $ ($ _POST ['hoursWorked'] as $ employeeId => $ dayArray){
foreach($ dayArray as $ dayOfWeek => $ hoursWorked){
// $ employeeId将是12345
// $ dayOfWeek将是0,1,2,3,4,5,6
// $ hoursWorked将是文本字段
}
}


I have a system where I need to list an arbitrary amount of employees with a textfield for each day of the week where an "hours worked" value can be entered.

So I need to generate a table with a dynamic number of rows, and each row is going to contain 7 text fields. I'm just wondering what is the best convention to use when assigning ID's to these fields to make it easy to iterate over once I receive the input data on my back end?

Each row will have an ID number associated with the row that represents the employee's ID.

It would be awesome to be able to do something like:

foreach($rows as $row)
{
     $id = $row['id'];

     $employee = Employee::find($id);

     foreach($row['hoursWorked'] as $dailyHours)
     {
           $timecard = new Timecard();
           $timecard->hours = $dailyHours;
           $employee->timecards->insert($timecard);
     }
}

What's the best way to structure my form and ID my inputs on the HTML side to make this as painless as possible?

As a sidenote, I'm working within the Laravel 3 framework in case that opens up any other solutions.

解决方案

<input type="text" name="hoursWorked[]" /> will internally convert to an array under $_POST['hoursWorked']. That means you can do something like this:

<input type="text" name="hoursWorked[12345][]" /> <!-- Sunday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Monday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Tuesday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Wednesday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Thursday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Friday -->
<input type="text" name="hoursWorked[12345][]" /> <!-- Saturday -->

Then, in PHP:

<?php
foreach ($_POST['hoursWorked'] as $employeeId=>$dayArray) {
    foreach ($dayArray as $dayOfWeek=>$hoursWorked) {
        // $employeeId will be 12345
        // $dayOfWeek will be 0, 1, 2, 3, 4, 5 ,6
        // $hoursWorked will be the value of the text field
    }
}

这篇关于在PHP中处理动态数量的表单字段的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆