checkbox appears dynamic in php - php

Here is my piece of code:
$a=mysql_query("SELECT denumire_intrebare,denumire_varianta,tip_intrebare
FROM intrebari,variante
WHERE intrebari.cod_chestionar='".$_SESSION['cod']."'
AND intrebari.cod_intrebare=variante.cod_intrebare");
while($b=mysql_fetch_array($a))
if($b['tip_intrebare']==3)
{
echo $b['denumire_intrebare'];
echo "<br>";
echo "<input type='checkbox' name='option1' value='Milk'>";
echo $b['denumire_varianta'];
echo "<br>";
}
So let me explain. I query the database and it brings me up a question and the answers that are related to it. I want the answers to put them as checkbox answers. The problem is that my question is repeating for all the answers. So if i have 5 answers then the question appears 5 times like this: question answer1, question answer2, ... . I want my question to appear only once with the answers under the question. I'm missing something but I don't know what. Any help?

Make an array with all $b['denumire_varianta'] and use a foreach() after the while has finished, also make your checkboxes array so you can get them afterwards:
$variante = array();
while($b=mysql_fetch_array($a)) {
if($b['tip_intrebare']==3){
$intrebare = $b['denumire_intrebare'];
$variante[] = $b['denumire_varianta'];
}
}
echo $intrebare."<br />";
foreach($variante as $varianta){
echo "<input type='checkbox' name='option[]' value='".$varianta."'>";
echo $varianta."<br />";
}

Related

How to check if radio button contains correct answers?

I have 2 tables in my database, 1 for questionnaires and 1 for correct answers. both have id linked to each other. code for displaying questions
while($row = $result->fetch_assoc()) {
echo "<br><label>". $row["Question"] ."</label><br><br>";
echo "<input type='radio' name='ans' value='ans1'>". $row["AnswerA"] ."</label><br>";
echo "<input type='radio' name='ans' value='ans2'>". $row["AnswerB"] ."</label><br>";
echo "<input type='radio' name='ans' value='ans3'>". $row["AnswerC"] ."</label><br>";
}
and then when user hits submit, the it should check if the radio button which contains the correct answer. code for checking answers
while($row = $result->fetch_assoc()) {
if(isset($_POST['submit'])){
if($_POST['ans']== $row["CorrectAns"]) {echo "Correct";}
else {echo "Incorrect";}
}
}
I think this will work for only one question. However I have about 20 questions and 3 radio buttons each. What addition should i add while checking the answer? I am trying to avoid javascript in this one but if there is a simple js solution I am open for it
First, reduce the database round trips and reduce rows fetched from database.
If you have multiple questions, get all the correct answers from database for these questions. Then create one array with correct answers $answers[question_id] = $answer;.
Then check for all the answers submitted by the user with actual answers.
<?php
if(isset($_POST['submit'])){
// $_POST['ans'] is array for all questions
$submittedAnswers = [];
$correctAnswers = [];
foreach ($_POST['ans'] as $questionID => $answer) {
$submittedAnswers[$questionID] = $answer;
}
// use mysql binding for actual query in production
//$sql = "select * from answers where Question_ID in {" . implode(',', array_keys($submittedAnswers)) . "}";
while($row = $result->fetch_assoc()) {
$correctAnswers[$row["Question_ID"]] = $row["CorrectAns"];
}
$failedQuestions = [];
foreach ($submittedAnswers as $questionID => $answer) {
if ($answer != $correctAnswers[$questionID]) {
$failedQuestions[] = $questionID;
}
}
}

php mysql remove row from button [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Why does this not work? I want to be redirected to id?1 and remove.php removes it from table. How is this done?
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM pages")
or die(mysql_error());
echo "<table border='1'>";
while ($row = mysql_fetch_array($result)) {
echo "<li class='list-group-item'>";
echo $row['header'];
echo "<br/>";
echo $row['description'];
echo "<br/>";
echo "<button type='button' class='btn btn-default btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ändra innehåll</button>";
echo " ";
echo "<form method='POST'><a href='delete.php?". $row['page_id']."' name='action' class='btn btn-danger btn-xs' style='margin-top:8px;margin-bottom:0px;'>Ta bort sida</a></form>";
echo "<td><a href='obrisi.php?id=$id'>Delete</a></td>";
echo "</li>";
}
echo "</table>";
?>
delete.php
<?php
if (isset($_POST['1']))
{
foreach ($_POST['id'] as $page_id)
{
if (!mysql_query("DELETE FROM pages WHERE id = '$page_id'"))
{
echo mysql_error();
}
}
}
?>
Please help me. Thanks in advance.
You can not get the id in POST becuase you are using it as a Query String. You can get it as:
$page_id = intval( $_GET['id'] );
Now you can check it as:
if( $page_id > 0 ){
// your stuff.. use this id in your query
}
What I have changed:
Use $_GET (SUPER GLOBAL) instead of $_POST becuase of query string.
Side Note:
There is no need to use foreach loop becuase it's not an array.
I suggest you to use mysqli_* or PDO instead of mysql_* becuase its deprecated and not available in PHP 7.
Form - PHP Manual

how to get php counter contents associated with variables

I have written a quiz script and it all works fine. What I want to do now is some fine tuning.
There are 6 questions. The questions and answers are defined in variables like this:
$q7="(7) __________ you like the red one or the blue one? (Positive, simple present)";
$a7="(7) <strong>Do</strong> you like the red one or the blue one? (Positive, simple present)";
As the person answers each question there is a counter that increments each correct answer.
I can view the counter and see that it contains the correct number of responses, but it is only a number.
If the person gets 3 out of 6 correct I show them the score like this -
You got 3/6 correct.
What I want to do is also show them the 3 correct answers they chose, and not the incorrect answers.
Is this possible? If the counter knows there are 3 correct answers, how do I get that info? Those particular variables?
How do I get this -
if $counter contains 4 or less correct answers then display those answers.
// *** question 12 *** //
print "<p> </p><table id='english_nb'><tr>";
print "<th>$q12</th></tr></table>";
print "<table id='english_nb'><tr>";
if ($_POST['answer12']==$do)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$do' />$do</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$do' />$do</td>";
if ($_POST['answer12']==$didnt){
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$didnt' />$didnt</td>";
$correct++;
}
else
print "<td width='25%'><input type='radio' name='answer12' value='$didnt' />$didnt</td>";
if ($_POST['answer12']==$doesnt)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$doesnt' />$doesnt</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$doesnt' />$doesnt</td>";
if ($_POST['answer12']==$did)
print "<td width='25%'><input type='radio' checked='checked' name='answer12' value='$did' />$did</td>";
else
print "<td width='25%'><input type='radio' name='answer12' value='$did' />$did</td>";
print "</tr></table>";
// *** check the answers *** //
foreach ($_POST as $value){
if (isset ($value)){
$done++;
}
}
if ($done !=7) //set this to 1 higher than the number of questions and answers
print "<input type='submit' name='submit' value='Check answers' />";
if (($correct<5)&&($done==7)) //set this to the number of minimum correct answers
print "<p>You should review the information and try the quiz again.</p>";
print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers
if (($done > 0)&&($done < 7)) //set this to 1 higher than the number of questions and answers
print "<p>You haven't answered all the questions. Please finish the quiz and re-submit your answers.</p>";
if(($done==7)&&($correct>4))
{ //set this to 1 higher than the number of questions and answers
print "<p>Your score is $correct/6 correct.</p>"; //set this to the same number of questions and answers
if ($correct==0)
$correct='0';
else
{
print "<p>The correct answers:</p>";
print "<p>$a7</p>";
print "<p>$a8</p>";
print "<p>$a9</p>";
print "<p>$a10</p>";
print "<p>$a11</p>";
print "<p>$a12</p>";
You could improve your code a lot, arrays would be a great start as others have said.
A simple way to do what you want could be adding a $correctAnswers = ""; variable, and everytime you use $correct++; add $correctAnswers .= "<p>$a1</p>"; and so on.
Then you could show them with print $correctAnswers;
$q=array($q1,$q2,....); //questions array
$a=array($a1,$a2,....); //answers array
correctAnswer=array(); //array of correct answers. empty at start
if (question=$q[0] && answer=$a[0]){ //if question and answer match
$correctAnswer[]=$a[0]; //adds the correct answer to the array
}
else{
// do something if the q &a doesnt match
}
$correct_answer_count = count($correctAnswer);

Why are my radio buttons overlapping?

I'm trying to make a quiz form which displays questions and 3 answers which are taken from my database. When I run my loop, it displays all the questions and answer choices correctly with the radio button but when I click on question 1 answer "b" then click question 2 answer "c" the radio button un-clicks question 1's answer "b" and highlight question 2 answer "c";
Answering question 1 Trying to answer question 2 after answering question 1
1. What is your name? 1. What is you name?
()John ()John
(*)Jake ()Jake <--un-clicked
()Joe ()Joe
2. Where are you from? 2. Where are you from?
()San Antonio ()San Antonio
()Austin ()Austin
()New York (*)New York
If you notice question 1 answer is removed when question 2 is answered.
Here is my code that retrieves the data from the database and displays it.
$mysql = "SELECT * FROM $table WHERE $table.quiz_name = '$name'";
$mydata = mysql_query($mysql,$con);
//post quiz name (here)
echo $name."</br>";
while($records = mysql_fetch_array($mydata)){
echo "<div>";
echo $records['question_description']."<br>";
//image displayed here
echo "<label><input type='radio' name='option' value=".$records['option_a'].">".$records['option_a']."</label><br>";
echo "<label><input type='radio' name='option' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
echo "<label><input type='radio' name='option' value=".$records['option_c'].">".$records['option_c']."</label><br>";
echo "</ br> <hr>";
echo "</div>";
}
I hope someone can help me solve this issue.
you are grouping all radio buttons with 'option'... do this;
$counter = 0 //declare a counter outside the loop
and add this with the name
name='option".$counter."'
and increment counter inside the loop
$counter++
This will give you a group for each set of radio buttons
Your radio buttons need different names. Having them all with the same name will mean you can only select one out of every radio you have.
Your radio buttons should have a different name for each question. Consider using something like your database ID like so:
while($records = mysql_fetch_array($mydata)) {
echo $records['question_description']."<br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_a'].">".$records['option_a']."</label><br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_b'].">".$records['option_b']."</lable><br>";
echo "<label><input type='radio' name='option" . $record['id'] . "' value=".$records['option_c'].">".$records['option_c']."</label><br>";
}

Pulling a survey with multiple answers from a database

I have a working survey that pulls each question and any related answers from multiple tables. Each question is stored in the following table:
tblQuestions, with the fields:
qID
qText.
The related answers are stored in the table:
tblPossAnswers, with the fields:
aID
qID
answerText.
So, I would have 3 possible answers for each question. My sql to pull everything is:
select * from tblQuestions, tblPossAnswers where
tblPossAnswers.qID = tblQuestions.qID
order by tblQuestions.qID ASC
And my PHP to display it:
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['qText'] . "</p><br />";
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
echo $row['answerText'];
}
The problem is this is displaying the qText every time it displays a possible answer. So it looks like:
Question 1
Possible answer 1
Question 1
Possible answer 2
Question 1
Possible answer 3
Question 2
Possible answer 1
Question 2
Possible answer 2
Question 2
Possible answer 3
What I would like to do is have the qText only display when the first possible answer is pulled. I'm still somewhat of a newb to MySQL, so the solution might be something very simple that I'm just not seeing.
You can either test for whether the question has changed within your PHP loop:
while ($row = mysql_fetch_array($result)) {
if ($row['qID'] != $lastQuestionID) {
echo "<p>" . $row['qText'] . "</p><br />";
$lastQuestionID = $row['qID'];
}
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
echo $row['answerText'];
}
Or else you can group the MySQL results by question using GROUP_CONCAT, specifying a separator on which you then split the answers in PHP:
select *, group_concat(answerText separator char(30)) as answers
from tblQuestions join tblPossAnswers using (qID)
group by tblQuestions.qID
order by tblQuestions.qID ASC
Then:
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['qText'] . "</p><br />";
foreach (explode(chr(30), $row['answers']) as $answer) {
echo "<input type='radio' name='".$row['qID']."' value='".$answer."' />";
echo $answer;
}
}
No need to change anything in sql just change php slightly as following
while ($row = mysql_fetch_array($result)) {
if ($row['qText'] != $lastQuestion) {
echo "<p>" . $row['qText'] . "</p><br />";
$lastQuestion = $row['qText'];
}
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />".$row['answerText'];
}

Categories