I’m facing a big problem here with PHP and I need your help. I’m creating questions in PHP with alternatives. Each question will have 4 alternatives. Questions will be inserted in a table called questions and all alternatives will be inserted in another table which is called alternatives. Every questions has its own id and even every alternative will have own id too. But every group of alternatives will have question id’s too , to know which alternative belongs to which question. And then I will select them as radio buttons.
For example if we have two questions like
-
What’s the name of Australian capital city? (Let’s say this question have id nr 1)
A) Sidney
B) New Delhi
C) Canberra
D) Maimi
And All those alternatives above will have their own id’s and questions id with is 1 in this case. -
What is 89 + (3*9) (Let’s say this question have id nr 3)
A) 116
B) 117
C) 115
D) 112
And All those alternatives above will have their own id’s and questions id with is 3 in this case.
3. An object is dropped from the top of a 100-m-high tower. Its height aboveground after t seconds is 100-4.9t2 m. how fast is it failing 2 sec after is it dropped?
A) 98m
B) 78m
C) 56m
D) None of them
And so on …..
Here is my code:
if (isset($_REQUEST['submit']) && $_REQUEST['submit'] != "") {
$questions = $_POST['questions'];
$alternatives = $_POST['alternatives'];
if(isset($_POST['questions']) && $_POST['questions'] != '')
{
foreach($questions as $q) {
if($q !='') {
$sql1 = " INSERT INTO questions (question) ".
" VALUES ('".$q."'); ";
if (!db_query($sql1))
$errors['err']='Unable to create question';
$question_id[] = mysql_insert_id();
}
}
foreach($question_id AS $q_id) { // I think here is the problem but I don't know how to solv'
foreach($alternatives as $alt){
if($alt !=''){
$sql = " INSERT INTO alternatives (alternative, question_id) ".
" VALUES ('".$alt."', ".$q_id."); ";
if (!db_query($sql))
$errors['err']='Unable to create alternatives';
}
}
}
}
}
<form action="test.php" method="POST" enctype="multipart/form-data"><br /><br /><br />
for($i=1; $i<=10; ++$i){
$i.") "?><input type="text" name="questions[]" value="" size="100"/><br />
<br /><p>Alternatives:</p><br />
for($j=1; $j<=4; ++$j){
<input type="text" name="alternatives[]" value="" /><br /> } }
<input type="submit" name="submit" value="Send"></input>
<input class="button" type="reset" value="Reset" />
</form>
Sorry for my code.. it was very difficult to write on here. But what is happening is :
All the 12 alternatives are inserted three times and each time they get all the same question_id. I mean the first 12 alternatives get question_id 1 , then those 12 alternatives are inserted again and they get question_id = 2 , and so on…. What I want is the first four alternatives get question_id 1, the alternatives of the second question would have question_id nr 2 and so on. I don’t know how to solve it.
Why don’t you add the question number to the name of the input as a key:
that way you’ll end with two very easy to loop arrays such as