onbids
onbids

Reputation: 53

Count posts with iterator

can i count how many POST's are submitted as Field_Amount_1, Field_Amount_2, Field_Amount_3, etc... like this ?

$Counting = count($_POST['Field_Amount_']);

Thanks for any suggestion

Upvotes: 0

Views: 61

Answers (2)

prodigitalson
prodigitalson

Reputation: 60413

Well the easiest way would be to correct your form like:

<input name="Field_Amount[]" type="text" />
<input name="Field_Amount[]" type="text" />
<input name="Field_Amount[]" type="text" />

This makes it post an array so $_POST would contain:

Array (
   'Field_Amount' => Array (
      0 => 'amount'
      1 => 'amount'
      2 => 'amount'
    )
)

Then you can just do count($_POST['Field_Amount'])

The other way would be to manually count all the all the elements:

$keys = array_keys($_POST);
$counted = count(preg_grep('/^Field_Amount_\d+$/', $keys));

If you also need to make sure you only track fields that are not empty then you could do supply an empty string as the second param to array_keys:

$keys = array_keys($_POST, '');
$counted = count(preg_grep('/^Field_Amount_\d+$/', $keys));

If you need to do more validation than that then you will need to manually loop.

Upvotes: 2

Artychaud
Artychaud

Reputation: 1

if($_POST['Submit']) {

    $count=0;

    foreach( $_POST as $post ) {
        if( strstr($post, "field_amount_")) {
            $count++;
        }
    }
    echo $count;
}

Upvotes: 0

Related Questions