James Stafford
James Stafford

Reputation: 1054

Foreach pulling multiple inputs

I have a set of inputs - 4 questions which are each arrays (could range from 1-100 keys) - they may or may not start at 1 (may be like 95,96,97,98...) I need to simultaneously pull the input from all 4 questions for each key 1 at a time, unfortunately I can not quite figure out how... I am familiar with the foreach statement and I think it will probably be my best bet:

here is what I have

<textarea name="question[98]" rows="3" cols="60"></textarea>
<select name="anstype[98]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[98]" type="text" size="10">
<input name="a_d_on[98]" type="text" size="10">

the next set of inputs could be

<textarea name="question[99]" rows="3" cols="60"></textarea>
<select name="anstype[99]">
<option value="break">Section Title</option>
...more options
</select>
<input name="d_on[99]" type="text" size="10">
<input name="a_d_on[99]" type="text" size="10">

ideally I need to get these into a mysql insert statements

$insquery = "INSERT INTO questions (question, anstype, d_on, a_d_on) VALUES('$_POST['question[98]']', '$_POST['anstype[98]']', '$_POST['d_on[98]']', '$_POST['a_d_on[98]']') ";

again I cant know what the key is going to start at, any help is appreciated

Upvotes: 0

Views: 1662

Answers (2)

Hanky Panky
Hanky Panky

Reputation: 46900

foreach($_POST["question"] as $key=>$value)
{
$question=$value;
$anstype=$_POST["anstype"][$key];
$d_on=$_POST["d_on"][$key];
$a_d_on=$_POST["a_d_on"][$key];

// Run your query here for one complete entry and it will repeat with loop

}

Upvotes: 1

user1646111
user1646111

Reputation:

You need to use the key like this:

$_POST['question'][]

In your case, for example: 98: $_POST['question'][98], but its better to iterate over it.

Upvotes: 0

Related Questions