php - 如果选中行,则从表列中获取值
问题描述
我正在使用 PHP 从表中获取值并需要进一步处理它们。如果选中行,我需要从 Quantity 和 FMK Code 列中获取值。
桌子:
<table class="table table-bordered">
<thead>
<tr class="success">
<th>#<br/></th>
<th>Article </th>
<th>Name </th>
<th>Quantity</th>
<th>FMK CODE<br/></th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($r=$q->fetch()){ ?>
<tr>
<td><input type="checkbox" class="from-control" name="id[]" value="<?php echo $r['id']?>"></td>
<td><?=$r['Article']?></td>
<td><?=$r['Name']?></td>
<td><input type="text" class="form-control" value="<?=$r['quantity'];?>" name="quantity"></td>
<td>
<select class="form-control col-lg-2" name="childCode"><?php getChildCodes($r["code"]) ?></select>
</td>
</tr>
<?php } ?>
</tbody>
</table>
在提交时,我需要从“数量和子代码”中获取所选每一行的值。
<button type="submit" name="getValues"> Submit </button>
PhP处理:
<?php
if (isset($_POST['getValues'])) {
$id = $_POST['id'];
$quantity = $_POST['quantity'];
$childCode = $_POST['childCode'];
$values = array();
foreach($id as $id) {
foreach($quantity as $quant){
foreach($childCode as $code){
array_push($values, $id,$quant,$code);
}
}
echo "<pre>";
var_dump($values);
echo "</pre>";
}
?>
值输出。在输出中,我从表中获取所有值,无论它们是否被检查是错误的。数组打印也不好,来自一个名称的数组的值。我需要将数组形成 id、数量、子代码的第一个元素到 b 一个数组。[0]=>"177239","10.000",113
array(3) {
[0]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(6) "177239"
[1]=>
string(6) "177240"
[2]=>
string(6) "177241"
}
}
[1]=>
array(3) {
[0]=>
string(6) "10.000"
[1]=>
string(7) "100.000"
[2]=>
string(6) "10.000"
}
[2]=>
array(3) {
[0]=>
string(11) "113"
[1]=>
string(10) "87"
[2]=>
string(10) "91"
}
}
解决方案
misorude 指出您需要以构建 PHP 数组的样式命名表单元素。这是用[]
提到的字符完成的。所以第一行会有像<input name="id[0]" ...
,这样的元素名称<input name="quantity[0]" ...
。第二行将具有元素名称,例如<input name="id[1]"
和<input name="quantity[1]"
还要确保foreach
块不会覆盖$id
变量:)
foreach($id as $id) // Oops!
推荐阅读
- amazon-web-services - 解析 EC2 用户数据 cloudformation 模板中的动态引用
- splat - 当被 culam 解析时,如何在 NodeConstant.cpp 中找到未通过断言错误的代码?
- docker - 如果我启动 Node2,Cassndra Docker Image 会停止
- python - PySpark:导出到 SQL Server 时更改时间戳
- apache-camel - 在 Jboss Fuse 中运行 Spring Boot Camel
- list - 从 Haskell 列表中删除项目
- python - 数据库中的熊猫年数据转换为列中的年数
- html - 如何加粗标题html标签
- c# - .NET Core 程序集中的嵌入式资源命名
- python - 子图频率图