0

Columns in my database: stat_id, stat1, stat2, stat3.

I am using a form that allows me to loop through a list of members and input 3 different stats each. ($member_stat is an array already that includes 'stat_id' as one of its keys)

foreach($member_stats as $member_stat) {
    echo '<label for="stat1"> Stat 1</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat1" id="stat1" />';
    echo '<label for="stat2"> Stat 2</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat2" id="stat2" />';
    echo '<label for="stat3"> Stat 3</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'stat3" id="stat3" /><br />';
}

I hit submit and my $_POST array/data looks like this:

Array (

    [157stat1] = 1
    [157stat2] = 4
    [157stat3] = 7
    [158stat1] = 2
    [158stat2] = 2
    [158stat3] = 6
    [159stat1] = 8
    [159stat2] = 6
    etc...
)

My questions is: How do I take this $_POST array and insert it into my database as above? ie.

157 stat1 stat2 stat3
158 stat1 stat2 stat3
159 stat1 stat2 etc...

I have tried various things but am having a tough time wrapping my head around separating the stat_id from the different stats in the $_POST keys (ie: 157 from stat1, stat2, stat3). I am wondering if maybe I set it up wrong in the form and should be naming my inputs in some other way.

3 Answers 3

1
$out = array();

$post= array(
 '157stat1' => 1,
 '157stat2' => 2,
 '157stat3' => 3,
 '158stat1' => 1
);

foreach($post as $key => $value)
{
    if (preg_match('/^(\d+)(\w+)$/', $key, $match) !== 0)
       $out[$match[1]][] = $match[2];
}

var_dump($out);

After that loop through $out and prepare SQL statements.

foreach($out as $key => $values)
{
  // escape array $values (array('stat1', 'stat2', 'stat3, ...) for each $key)
  // and string $key (it will be 157, 158 and so on)
  // prepare and execute SQL statement
  // function join will help to deal with array $values
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your time. I am getting an error: Undefined offset on the line with: $out[$match[1]][] = $match[2]; Would you mind dumbing down this line?
@user2949794 Are you sure that all keys of your array look like 157stat1 ? I do not think so.. Ok, I'll modify the answer to take this into account.
yes all keys are 3 digits(stat_id) followed by either stat1, stat2 or stat3.
@user2949794 I do not think so, otherwise there will be no such error. Try the updated code.
I put your code in new php file to test and didn't get an error. Must be something else I'm doing wrong. I'll play around with it. Thanks for your help!
|
0

Imagine that your $_POST comes like that:

array(
    'stats' => array(
        157 => array(
            stat1 => 1,
            stat2 => 3,
            stat3 => 4
        ),
        158 => array(
            stat1 => 2,
            stat2 => 3,
            stat3 => 1
        )
        .
        .
    )
)

Then you could just:

foreach ($_POST['stats'] as $whateverId => $stats) {
    // now you have the desired $whateverId and
    // its $stats
    // you can access each stats indexing $stats like: $stats['stat1']
}

And finally you can accomplish the desired $_POST format using the following html form naming (using arrays notation):

foreach($member_stats as $member_stat) {
    $id = $member_stat['stat_id'];

    echo '<label for="stat1"> Stat 1</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat1]\" id=\"stat1\" />";

    echo '<label for="stat2"> Stat 2</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat2]\" id=\"stat2\" />";

    echo '<label for="stat3"> Stat 3</label>';
    echo "<input type=\"text\" name=\"stats[$id][stat3]\" id=\"stat3\" /><br />";
}

Don't forget to validate your $_POST['stats'] input.

1 Comment

This loop will create multiple stat1 ids -- this would create an invalid HTML document. The behavior of the for attributes may be unexpected as well.
0

Use HTML arrays.

foreach($member_stats as $member_stat) {
    echo '<label for="stat1"> Stat 1</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat1]" id="stat1" />';
    echo '<label for="stat2"> Stat 2</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat2]" id="stat2" />';
    echo '<label for="stat3"> Stat 3</label>';
    echo '<input type="text" name="'.$member_stat['stat_id'].'[stat3]" id="stat3" /><br />';

}

This will return something like:

Array (

    [157stat][1] = 1
    [157stat][2] = 4
    [157stat][3] = 7
    [158stat][1] = 2
    [158stat][2] = 2
    [158stat][3] = 6
    [159stat][1] = 8
    [159stat][2] = 6
)

You could see an example at: http://www.thefutureoftheweb.com/blog/use-arrays-with-html-form-inputs and another great stack overflow answer here: HTML input arrays

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.