tags:

views:

54

answers:

2

Hi

I am using tiny table with some input fields for posting in a page. I want to retrieve the data which the user fills up for a particular instrument number.

My code

<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>

The print_r command outputs

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
)

I want to retrieve the values for id 57 for which he has entered values. But i am unable to construct logic for retrieving this value.I want to make it dynamic.

Please help me on this.

Thanks

+1  A: 

I would format your input names using array notation:

 <td><input type="text"  name="txtDateDeposited[<?php echo $paymentdeposited[pk_paymentinstrumentid];?>]"  class="field date-pick"/></td> 

so that your resulting data could get accessed as

$_REQUEST['txtDateDeposited']['57']

update:

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
}

if you are sure you're only using ONE underscore, then:

 foreach( $_POST as $key => $value) {
        $keyPieces = explode("_", $key);
        $field = $keyPieces[0];
        $id = $keyPieces[1];
        // txtDateDeposited_57 becomes
        //  $id -> 57
        //  $field -> txtDateDeposited
    }

would work too.

Note, for anything using the above method i find putting the number/identifier first is better so that you can do $pieces[0] instead of counting the array. Plus, you array_slice($pieces, 1) will take it out for you without the additional count again.

Dan Heberden
Hi dan thanks but this $_REQUEST['txtDateDeposited']['57'] is static i want to make it dynamic
Pankaj Khurana
Updated answer to show parsing array keys
Dan Heberden
I want to update a table based on the entries made by the user for a particular instrument. The code will give be all ids but i want the id for which he has made an entry.
Pankaj Khurana
+1  A: 

Use 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);
        }
    }
}

Or if you know the Id:

var_dump ($_POST['txtPaymentInstrumentNo_'.$Id]);

Edit

Simpler code. thx to notJim.

Piotr Pankowski
I think you want `explode('_', $key, 2);` as the final argument specifies the number of elements in the returned array. You can use `list($field, $id) = explode ('_', $key, 1);` to avoid `$tmp`, fyi. Better make sure that underscore exists first!
notJim
Yes, you're right. thx for pointing this.
Piotr Pankowski
I want to update a table based on the entries made by the user for a particular instrument. The code will give me all ids but i want the id for which he has made an entry.
Pankaj Khurana
Take a look at example above. I check for $value enough?
Piotr Pankowski
@piotr is the following code correct.foreach ($_POST AS $key => $value) { if (strpos ($key, '_') !== false) { list($field, $id) = explode ('_', $key, 2); if ($value!='') { if($field=='txtDateDeposited') { $date=$value; $instrumentid=$id; } if($field=='txtBankName') $bank=$value; if($field=='txtSlipNo') $slip=$value; } } }echo $date.'<br>'.$bank.'<br>'.$slip.'<br>'.$instrumentid;
Pankaj Khurana
Looks good. You should know if you see proper values in the echo.
Piotr Pankowski