Peter
Peter

Reputation: 1290

extract a numeric indexed array in php

I have a few dynamicaly build input fields in a form that looks like this:

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

Because i write my input name like this: person[] & function[] i get an numeric indexed array in my $_POST array. Because the input fields are made dynamicaly it is never known for sure how many person[] & function[]index numbers there will be in the $_POST.

How do i handle this? In other words what if i would like to INSERT person[0] + function[0] & person[1] + function[1] + ... (variable amount of person's & their functions) into my database? how could i extract this so it is possible to do this?

NOTE: Every person has his own function so it is important that it stays together.

I did not got very far so i cannot provide more code i have tried:

foreach ($_POST['person'] as $value){
...
} 

This gives me a list of that specific array person[] inside of the $_POST but this isn't usefull to me..

Upvotes: 1

Views: 169

Answers (3)

i alarmed alien
i alarmed alien

Reputation: 9530

This form design will lead to problems if you don't have a way to ensure that the person and the function inputs are tied together in some way. Imagine someone is filling in the form, and they leave the function blank for one person:

Person    Function

Anna      array_map
Betty
Claire    count
          date_format

Rather than knowing that Betty's function is blank, the form logic will pair up Betty with count and Claire with date_format. It is therefore better to group each person/function combination with an ID so you can distinguish them. It is easy enough to do with an iterator in either JS or PHP.

New form:

<input type="text" class="form-control" name="persfunc[1][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[1][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[2][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[2][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[3][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[3][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[4][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[4][f]" placeholder="Function" />

You can then use a function like this to process the data:

    # gather up all the 'persfunc' data from $_POST
    if (! empty($_POST['persfunc'])) {
        # initialise an array for the results
        $pers_func_arr = array();

        foreach ($_POST['persfunc'] as $pf) {
            # make sure that both $persfunc[x]['p'] and $persfunc[x]['f'] have values
            if (
                isset($pf['p'])
                && isset($pf['f'])
                && strlen($pf['p']) > 0
                && strlen($pf['f']) > 0
            ) {
                # you've got a pair! Do whatever you want with them.

                # e.g. print out the name/function pair:
                echo "<p>Person " . $pf["p"]
                    . " has function " . $pf["f"]."</p>";

                # e.g. add them to a data structure for later use
                $pers_func_arr[] = array( $pf["p"], $pf["f"] );
            }
        }
    }

Give this form the dodgy input data from before, and check the output:

Person Anna has function array_map

Missing full data for person_2

Person Claire has function count

Missing full data for person_person_4

This is a much more robust way to handle the form data than just trusting your inputs will match up, and it doesn't require a lot more coding.

Upvotes: 1

Dave
Dave

Reputation: 3288

Something like this would probably work

$sql = "INSERT INTO tbl (Person,Function) VALUES ";
$components = array();

$totalitemcount = count($_POST['person']);

for ($i=0;$i<=$totalitemcount;$i++) {
    $components[] = "('".$_POST['person'][$i]."','".$_POST['function'][$i]."')";
}

$insertstring = implode(",",$components);

$finalsql = $sql.$insertstring;

//do insert here

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

It sounds like there are the same number of 'people' as there are 'functions'. If this is the case, then you can do something like this:

foreach($_POST['person'] as $key => $value) {
    echo 'Person ' . $value . ' has function ' . $_POST['function'][$key] . '<br>';
}

Upvotes: 2

Related Questions