我正在使用带有一些输入字段的小表在页面中发布。 我想检索用户为特定仪器编号填写的数据。
我的代码
<form name="frmDeposit" action="paymentdeposited.php" method="post">
<table cellpadding="0" cellspacing="0" border="0" id="table" class="tinytable" style="width:700px;">
<thead>
<tr>
<th><h3>Email</h3></th>
<th><h3>Amount Paid</h3></th>
<th><h3>Instrument Type</h3></th>
<th><h3>Instrument No.</h3></th>
<th><h3>Date Paid</h3></th>
<th class="nosort"><h3>Date Deposited</h3></th>
<th class="nosort"><h3>Bank Name</h3></th>
<th class="nosort"><h3>Slip No.</h3></th>
<th class="nosort"><h3>Submit</h3></th>
</tr>
</thead>
<tbody>
<?php
foreach($paymentsdeposited as $paymentdeposited)
{
?>
<tr>
<td><?php echo $paymentdeposited[email];?></td>
<td><?php echo $paymentdeposited[amount];?></td>
<td><?php echo $paymentdeposited[instrument];?></td>
<td><?php echo $paymentdeposited[instrumentnumber];?></td>
<td><?php echo $paymentdeposited[dated];?></td>
<td><input type="text" name="txtDateDeposited_<?php echo $paymentdeposited[pk_paymentinstrumentid];?>" class="field date-pick"/></td>
<td><input type="text" name="txtBankName_<?php echo $paymentdeposited[pk_paymentinstrumentid];?>" class="field"/></td>
<td><input type="text" name="txtSlipNo_<?php echo $paymentdeposited[pk_paymentinstrumentid];?>" class="field"/><input type="hidden" name="txtPaymentInstrumentNo_<?php echo $paymentdeposited[pk_paymentinstrumentid];?>" value="<?php echo $paymentdeposited[pk_paymentinstrumentid];?>" class="field"/></td>
<td><input type="submit" name="btnSubmit1" value="Submit"/></td>
</tr>
<?php
}
?>
</tbody>
</table>
print_r命令输出
Array
(
[txtDateDeposited_57] => 2010-05-07
[txtBankName_57] => pnb
[txtSlipNo_57] => 121
[txtPaymentInstrumentNo_57] => 57
[btnSubmit1] => Submit
[txtDateDeposited_51] =>
[txtBankName_51] =>
[txtSlipNo_51] =>
[txtPaymentInstrumentNo_51] => 51
[txtDateDeposited_52] =>
[txtBankName_52] =>
[txtSlipNo_52] =>
[txtPaymentInstrumentNo_52] => 52
[txtDateDeposited_45] =>
[txtBankName_45] =>
[txtSlipNo_45] =>
[txtPaymentInstrumentNo_45] => 45
[txtDateDeposited_47] =>
[txtBankName_47] =>
[txtSlipNo_47] =>
[txtPaymentInstrumentNo_47] => 47
)
我想检索已输入值的id 57的值。但是我无法构造用于检索此值的逻辑。我想让它充满活力。
答案 0 :(得分:2)
使用explode。 E.g:
foreach ($POST AS $key => $value) {
if (strpos ($key, '_') !== false) {
list($field, $id) = explode ('_', $key, 2);
if ($value) {
var_dump ($field, $id, $value);
}
}
}
或者如果您知道Id:
var_dump ($_POST['txtPaymentInstrumentNo_'.$Id]);
修改强>
更简单的代码。 thx to notJim。
答案 1 :(得分:1)
我会使用数组表示法格式化您的输入名称:
<td><input type="text" name="txtDateDeposited[<?php echo $paymentdeposited[pk_paymentinstrumentid];?>]" class="field date-pick"/></td>
这样您的结果数据就可以作为
进行访问$_REQUEST['txtDateDeposited']['57']
更新:
foreach( $_POST as $key => $value) {
$keyPieces = explode("_", $key);
$field = implode("_", array_slice( $keyPieces, 0, count($keyPieces)-1 ));
$id = $keyPieces[count($keyPieces)-1];
// txtDateDeposited_57 becomes
// $id -> 57
// $field -> txtDateDeposited
}
如果你确定你只使用一个下划线,那么:
foreach( $_POST as $key => $value) {
$keyPieces = explode("_", $key);
$field = $keyPieces[0];
$id = $keyPieces[1];
// txtDateDeposited_57 becomes
// $id -> 57
// $field -> txtDateDeposited
}
也可以。
注意,对于使用上述方法的任何内容,我发现首先放置数字/标识符更好,这样你就可以做$ pieces [0]而不是计算数组。另外,你的array_slice($ pieces,1)将为你取出它,而无需再次计算。