从用户检索条目时有两(2)种情况。
情景1:
用户为每个人输入一(1)个条目。
var_dump()返回(正确)
array(
5
)
{
[0] => string(8) "John Doe"1 => string(6) "Apples"2 => string(2) "50"[3] => string(3) "200"[4] => string(10) "2015-01-16"
}
array(
5
)
{
[0] => string(8) "Jane Doe"1 => string(7) "Bananas"2 => string(2) "20"[3] => string(3) "100"[4] => string(10) "2015-01-16"
}
情景2:
用户为其中一个或两个人输入多个(1)条目。
var_dump()返回(不正确)
array(
5
)
{
[0] => string(8) "John Doe"1 => string(6) "Apples"2 => string(2) "50"[3] => string(3) "200"[4] => string(10) "2015-01-16"
}
array(
5
)
{
[0] => string(8) "Jane Doe"1 => string(7) "Oranges"2 => string(2) "20"[3] => string(3) "100"[4] => string(10) "2015-01-16"
}
array(
5
)
{
[0] => NULL1 => string(7) "Bananas"2 => string(2) "20"[3] => string(3) "200"[4] => string(10) "2015-01-16"
}
以上转储显示第二个条目 Jane Doe 而不是 John Doe , Jane Doe 的条目带有 NULL 名称输入。
HTML表单
<div class="toclone">
<p class="name">
<input type="text" name="name[]" id="sname" placeholder="Name"/>
</p>
<div class="inner-wrap">
<!-- <strong>Procurement Information</strong> -->
<div class="clone-inner">
<p>
<input type="text" name="item[]" id="sitem" placeholder="Item"/>
<input type="text" name="quantity[]" id="sqty" placeholder="Quantity"/>
<input type="text" name="amount[]" id="samt" placeholder="Amount"/>
<input type="text" name="date[]" id="sdate" placeholder="Date"/>
<a href="#" class="phclone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i></a>
<a href="#" class="phdelete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i></a>
</p>
</div>
</div>
<a href="#" class="clone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i> Add Name</a>
<a href="#" class="delete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i> Delete Name</a>
</div>
<p class="submit">
<input type="submit" name="saveProcurement" value="Save" class="btn btn-sm btn-primary"/>
</p>
控制器
$name = $this->input->post('name');
$item = $this->input->post('item');
$quantity = $this->input->post('quantity');
$amount = $this->input->post('amount');
$date = $this->input->post('date');
$limit = array();
if (count($name) >= count($item)) {
$limit = $name;
} else {
$limit = $item;
}
for ($i = 0; $i < count($limit); $i++) {
var_dump(array($name[$i], $item[$i], $quantity[$i], $amount[$i], $date[$i]));
}
如何修改循环以使两种方案都能正常工作?
感谢任何帮助。
答案 0 :(得分:1)
你需要一个隐藏的输入:
<input type="hidden" name="rows[]" id="srows" value="1" />
每次添加或删除名称时,此输入都应反映行数。
您的更新模板:
<div class="toclone">
<p class="name">
<input type="text" name="name[]" id="sname" placeholder="Name"/>
<input type="hidden" name="rows[]" id="srows" value="1" />
</p>
<div class="inner-wrap">
<!-- <strong>Procurement Information</strong> -->
<div class="clone-inner">
<p>
<input type="text" name="item[]" id="sitem" placeholder="Item"/>
<input type="text" name="quantity[]" id="sqty" placeholder="Quantity"/>
<input type="text" name="amount[]" id="samt" placeholder="Amount"/>
<input type="text" name="date[]" id="sdate" placeholder="Date"/>
<a href="#" class="phclone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i></a>
<a href="#" class="phdelete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i></a>
</p>
</div>
</div>
<a href="#" class="clone btn btn-xs btn-success"><i class="fa fa-plus fa-fw"></i> Add Name</a>
<a href="#" class="delete btn btn-xs btn-danger"><i class="fa fa-times fa-fw"></i> Delete Name</a>
</div>
<p class="submit">
<input type="submit" name="saveProcurement" value="Save" class="btn btn-sm btn-primary"/>
</p>
更新后的controller.php:
$name = $_POST['name'];
$item = $_POST['item'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
$date = $_POST['date'];
$limit = array();
if (count($name) >= count($item)) {
$limit = $name;
} else {
$limit = $item;
}
$offset = 0;
for ($i = 0; $i < count($_POST['name']); $i++) {
for ($j = 0; $j < intval($_POST['rows'][$i]); $j++)
{
$dataArray = array($name[$i]);
array_push($dataArray, $item[$offset + $j], $quantity[$offset + $j], $amount[$offset + $j], $date[$offset + $j]);
var_dump($dataArray);
}
$offset = intval($_POST['rows'][$i]);
}
使用您的输入在我的机器上测试了这个:
array(5) { [0]=> string(6) "Mouser" [1]=> string(4) "test" [2]=> string(2) "20" [3]=> string(2) "30" [4]=> string(4) "3000" }
array(5) { [0]=> string(6) "Mouser" [1]=> string(5) "test2" [2]=> string(2) "20" [3]=> string(2) "40" [4]=> string(4) "4000" }
array(5) { [0]=> string(7) "Mouser2" [1]=> string(5) "test3" [2]=> string(2) "39" [3]=> string(3) "489" [4]=> string(5) "34499" }
第一个循环遍历所有名称。内循环遍历所有数据。隐藏输入提供的行索引设置数据的偏移量。因此John Doe
的偏移量为0 + n
,其中n
是行数(2)。对于Jane
2 + n
,其中n
是一行。
我没有使用codeIngniter所以我已经将POST变量的检索更改为php的原生$_POST
。