how to get php counter contents associated with variables - php

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);

Related

while-loop not working properly

I want to create a page, where I can create Keys, and save them in a databank, but I got some problems.
There is a textfield, where you can write in, how many keys you want to create. For example I write 5 in, but it does only 3 entrys in the databank, but down in the code is an echo command, which says that he did it 5 times, here is a screenshot:
Also, what I noticed is, that the website is doing max 3 entrys in the databank, for example I write 10, and he still only does 3 entrys.
If I write only 1-3 keys, the while-loop is working.
echo "<p><a href='key.php'>Betakey administration</a></p>";
echo "<form action='#' method='POST'>";
echo "<input type='number' name='numberkeys' placeholder='Number of Keys'>";
echo "<br>";
echo "<input type='submit' name='createkey' value='Create Key'>";
echo "<input type='submit' name='keyanzeigen' value='Show all Keys'>";
echo "<input type='submit' name='unusedkeys' value='Show all unused Keys'>";
echo "<input type='submit' name='usedkeys' value='Show all used Keys'>";
echo "</form>";
$createdkeys=0;
if(isset($_POST['createkey'])){
$minus= array(5,10,15);
while($createdkeys<$_POST['numberkeys']){
//creation of the keys
for($i=1;$i<20 ;$i++){
if(in_array($i,$minus)){
$key=$key."-";
}
else{
$key=$key.mt_rand(0,9);
}
//the key looks like this: 1234-5678-1234-5678
}
//here it checks, if the key already exists
$check = mysqli_query($db,"SELECT keyy FROM keyys WHERE keyy='$key'");
$check = mysqli_fetch_array($check);
//if the code not exists, the website will insert the key in the databank
//the website will echo the variable $createdkeys and increase the variable $createdkeys by one
if(!isset($check)){
$createdkeys= $createdkeys+1;
echo "<p>".$createdkeys."</p>";
mysqli_query($db,"INSERT INTO keyys VALUES('$key','NO')");
}
}
}

PHP & SQL - Update query fails to update value

I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.

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>";
}

checkbox appears dynamic in 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 />";
}

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