I want to insert the answers of my quiz application in the database. I am using Codeigniter. The quiz questions are randomly generated. 10 questions are shown to the user. The following is my code:
The view
<div class="col-lg-8">
<table class="table" style="width: 100%;">
<?php
$i = 1;
echo form_open('Menu/submit_ans', array('name' => 'quiz'));
foreach ($quiz_array as $q){
?>
<td colspan="2"style="background-color: #337ab7;color:white;"> <h4>Question No. <?php echo $i?> </h4></td>
<tr>
<td colspan="2"><?php echo $q->ques;?></td>
<input hidden name="qid[]" type="text" value="<?php echo $q->qid;?>">
<input hidden name="uid[]" type="text" value="<?php echo $user['id'];?>">
</tr>
<tr>
<td><?php echo $q->ans_1;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="1"></td>
</tr>
<tr>
<td><?php echo $q->ans_2;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="2"></td>
</tr>
<tr>
<td><?php echo $q->ans_3;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="3"></td>
</tr>
<tr>
<td><?php echo $q->ans_4;?></td>
<td><input type="radio" name="ans[<?php print $i; ?>]" value="4"></td>
</tr>
<?php
$i++;
}
?>
</table>
<center><button class="btn btn-success" type="submit">Submit!</button></a></center>
</div>
<?php echo form_close();?>
The Controller:
function submit_ans(){
$data = array(
$q_array = $_POST['qid'],
$u_array = $_POST['uid'],
$ans_array = $_POST['ans']
);
$this->load->model('MyModel');
$this->MyModel->insert_ans($data);
}
The Model
function insert_ans($data){
foreach($data as $answer) {
var_dump($answer);
}
}
The vardump result
//for question id
array (size=12)
0 => string '4' (length=1)
1 => string '12' (length=2)
2 => string '3' (length=1)
3 => string '6' (length=1)
4 => string '11' (length=2)
5 => string '7' (length=1)
6 => string '10' (length=2)
7 => string '2' (length=1)
8 => string '8' (length=1)
9 => string '1' (length=1)
10 => string '5' (length=1)
11 => string '9' (length=1)
//for user id
array (size=12)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '1' (length=1)
3 => string '1' (length=1)
4 => string '1' (length=1)
5 => string '1' (length=1)
6 => string '1' (length=1)
7 => string '1' (length=1)
8 => string '1' (length=1)
9 => string '1' (length=1)
10 => string '1' (length=1)
11 => string '1' (length=1)
//for answers
array (size=12)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
4 => string '2' (length=1)
5 => string '2' (length=1)
6 => string '3' (length=1)
7 => string '3' (length=1)
8 => string '4' (length=1)
9 => string '3' (length=1)
10 => string '2' (length=1)
11 => string '3' (length=1)
I am getting all the results that I want from question ID to answers. How can I insert this table in the database now?
Following is my table made up of 4 columns:
ans_id - auto incremental user_id - user who answers the questions q_id - the question answered ans_att - the answer
Try setting the radio name as a array:
<input type="radio" name="asw[]" value="1"></td>
<input type="radio" name="asw[]" value="1"></td>
and, for include in your db, try this php:
$asw = $_POST['asw'];
foreach($asw as $answer){
mysql_query("insert into table_name (col_name) values ('". $aswer ."')");
}
Is this what you want?
You can't have a random name, because this last gonna be inserted to your data so you can try fix name ,random values and id. Try this one with java script function " type="radio" name="as" value=""> but could you please give us your table to be more clear