2010-05-24 46 views
0

我正在使用带有一些输入字段的小表来发布页面。 我想检索用户填写特定仪器编号的数据。检索动态发布值

我的代码

<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。但我无法构建检索此值的逻辑。我想让它变得动态。

回答

2

使用explode。例如: -

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不要吉姆。

+2

我想你想'explode('_',$ key,2);'作为最后一个参数指定返回数组中元素的数量。你可以使用'list($ field,$ id)= explode('_',$ key,1);'以避免'$ tmp',fyi。最好确保下划线先存在! – notJim 2010-05-24 08:20:07

+0

是的,你是对的。 thx指出这一点。 – 2010-05-24 08:22:55

+0

我想根据用户为特定仪器所做的条目更新表格。该代码会给我所有的ID,但我想要他已经进入的ID。 – 2010-05-24 08:48:33

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)会在没有额外计数的情况下为你取出。

+0

嗨丹谢谢,但这$ _REQUEST ['txtDateDeposited'] ['57']是静态我想使它动态 – 2010-05-24 08:11:07

+0

更新的答案显示解析数组键 – 2010-05-24 08:23:29

+0

我想根据条目进行更新表用户为特定的仪器。代码将会是所有ID,但是我想要他已经输入的ID。 – 2010-05-24 08:24:41