Script not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

fastmike
Forum Commoner
Posts: 32
Joined: Wed Jan 24, 2007 12:52 am

Post by fastmike »

Hurray :) I got it lol.
but thats just for quiz1.php. Thankyou so much superdesign i read your post clearly and feys it worked. still having errors not giving me the rite output like who is the president of united states?
bush
when a user clicks show selected values it outputs you answer 0 out of 1 and gave me the rite answer even i clicked on bush. still thinking any advice would be great.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fastmike wrote:Hurray :) I got it lol.
but thats just for quiz1.php. Thankyou so much superdesign i read your post clearly and feys it worked.
Yay.
fastmike wrote:still having errors not giving me the rite output like who is the president of united states?
bush
when a user clicks show selected values it outputs you answer 0 out of 1 and gave me the rite answer even i clicked on bush. still thinking any advice would be great.
I'm missing something I guess; I have no idea what you're talking about.

$_SERVER['REQUEST_METHOD'] is in the $_SERVER superglobal. You can see what's in it via var_dump($_SERVER)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

No idea what you're saying, but I'll give it a shot.

Your quiz asks them a question, and they submit an answer... If you tell them the answer later on, initiate a session at the start of the quiz and use the session to record their answers, and a database to determine their correctness.

If it asks them, then tells them the correct answer immediately, you could just have it output the correct answer, then compare the two.

If this isn't what you're asking, maybe you should ask again, clearly.
fastmike
Forum Commoner
Posts: 32
Joined: Wed Jan 24, 2007 12:52 am

Post by fastmike »

sorry guys. i was so happy that the script was working i dont even know what i was talking about so here we go again. i dont have any notices or any kind of warnings in my quiz1.php script. like i said in my previous post that i have a button where it says (show selected values). after completing the quiz user clicks on the button(show selected values) to see how much he scored. i have one question in my database which is
Q)who is the president of USA?
clinton bush regan
i click on the radio button bush and then i click on the button(Show Selected values) and this is what i get my output
*******************.
You scored 0 out of 1
Here are the answers:
who is the president of USA?
»you didn't select an answer. The answer is bush
************************
Even though i selected the correct answer bush. i dont know whats going on the script looks fine to me here it is .

Code: Select all

<?php


include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
$submit = isset($_POST['submit']);
if (!$submit) {


	echo "<form method='post' action='$_SERVER[PHP_SELF]'>";
	echo "<table border=0>";

	while ($row = mysql_fetch_array($display)) {

	$id = $row["id"];
	$question = $row["question"];
	$opt1 = $row["opt1"];
	$opt2 = $row["opt2"];
	$opt3 = $row["opt3"];
	$answer = $row["answer"];

	echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
	echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

	}

	echo "</table>";
	echo "<input type='submit' value='See how you did' name='submit'>";
	echo "</form>";

}

elseif ($submit)

{

	$score = 0;
	$total = mysql_num_rows($display);
		while ($result = mysql_fetch_array($display))


		{

			$answer = $result["answer"];
			$q = $result["q"];

		if ($q == $answer)
		{
		$score++;
		}

	}

	echo "<p align=center><b>You scored $score out of $total</b></p>";
	echo "<p>";

	if   ($score == $total) {
	echo "Congratulations! You got every question right!";
	}
	elseif ($score/$total < 0.34) {
	echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
	}
	elseif ($score/$total > 0.67) {
	echo "Well done! You certainly know your stuff.";
	}
	else {
	echo "Not bad - but there were a few that caught you out!";
	}

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<tr><td><br>$question</td></tr>";

if ($q == $answer)
		{
		echo "<tr><td>&raquo;you answered ${$q}, which is correct</td></tr>";
		}
elseif ($q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>&raquo;you answered ${$q}. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}

?>
fastmike
Forum Commoner
Posts: 32
Joined: Wed Jan 24, 2007 12:52 am

Post by fastmike »

I got my quiz1.php runiung.

Code: Select all

<?php


include("contentdb.php");

$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
$submit = isset($_POST['submit']);
if (!$_POST['submit'])  {


	echo "<form method=post action=$_SERVER[PHP_SELF]>";
	echo "<table border=0>";

	while ($row = mysql_fetch_array($display)) {

	$id = $row["id"];
	$question = $row["question"];
	$opt1 = $row["opt1"];
	$opt2 = $row["opt2"];
	$opt3 = $row["opt3"];
	$answer = $row["answer"];

	echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
	echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td></tr>";

	}

	echo "</table>";
	echo "<input type='submit' value='See how you did' name='submit'>";
	echo "</form>";

}

elseif ($_POST['submit'])

{

	$score = 0;
	$total = mysql_num_rows($display);
		while ($result = mysql_fetch_array($display))


		{

			$answer = $result["answer"];
			$iq = $result["id"];
			$q = $_POST["q$iq"];

		if ($q == $answer)
		{
		$score++;
		}

	}

	echo "<p align=center><b>You scored $score out of $total</b></p>";
	echo "<p>";

	if   ($score == $total) {
	echo "Congratulations! You got every question right!";
	}
	elseif ($score/$total < 0.34) {
	echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
	}
	elseif ($score/$total > 0.67) {
	echo "Well done! You certainly know your stuff.";
	}
	else {
	echo "Not bad - but there were a few that caught you out!";
	}

echo "</p>";

echo "<p>Here are the answers:";

echo "<table border=0>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$b = $row['id'];
$answer = $row["answer"];
$q = $_POST["q$b"];

echo "<tr><td><br>$question</td></tr>";

if ($q == $answer)
		{
		echo "<tr><td>&raquo;you answered $q, which is correct</td></tr>";
		}
elseif ($q == "") {
echo "<tr><td>&raquo;you didn't select an answer. The answer is $answer</td></tr>";
}
else {
echo "<tr><td>&raquo;you answered $q. The answer is $answer</td></tr>";
}

}
echo "</table></p>";



}

?>
can i ask just one last question ? i dont want to post another topic.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fastmike wrote:can i ask just one last question ? i dont want to post another topic.
with baited breath I await.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

feyd wrote:
fastmike wrote:can i ask just one last question ? i dont want to post another topic.
with baited breath I await.
ewe... bait breath!
fastmike
Forum Commoner
Posts: 32
Joined: Wed Jan 24, 2007 12:52 am

Post by fastmike »

lol spider man(feyd) get some mouth freshner J/k. anyways everything is working just fine now after i updated my quiz1.php and admin.php file. i can add questions in the database, i can take the quiz. i am having problem with my editquizlist.php file. i have a form which shows all the questions in the database and, in each question i have an option of edit(Editing the question) and delete(delete the question) from the database. my edit works but when i try to click on delete it does not delete the question from the database. here is my editquizlist.php file

Code: Select all

<HTML>
<link rel="stylesheet" href="quiz.css" type="text/css">
<body><center>
<P>&nbsp;</P>

<B>Admin area - edit the quiz</B>
<br><br>
  <table width="300" border="0" cellspacing="0" cellpadding="0">

        <?php

include("contentdb.php");

$result = mysql_query("SELECT id, question FROM $table ORDER BY id",$db);

echo "<table>";

while ($row = mysql_fetch_array($result))
{
$alternate = "";

	$id = $row["id"];
	$question = $row["question"];
	if ($alternate == "1") {
	$color = "#ffffff";
	$alternate = "2";
	}
	else {
	$color = "#efefef";
	$alternate = "1";
	}
	echo "<tr bgcolor=$color><td>$id:</td><td>$question</td><td>[ <a href='editquiz.php?id=$id'>edit</a> ]</td><td>[ <a href='deletequiz.php?id=$id' onClick=\"return confirm('Are you sure?')\">delete</a> ]</td></tr>";
}
echo "</table>";
?>
        <br>
        <br>
        <a href="editquiz.php">Add a new question to the quiz</a></td>
    </tr>
  </table>
  <br>
  <br>
<a href="quizinfo.php">See the full quiz table</a>
</center>
and here is my deletequiz.php file

Code: Select all

<HTML>
<link rel="stylesheet" href="quiz.css" type="text/css">
<center>
<?php
include("contentdb.php");

mysql_query("DELETE FROM $table WHERE id=$id",$db);
echo "<P>&nbsp;</P>";

echo "<B>Admin area - delete a quiz question<br><br></B>";

echo "Question deleted<br><br>";
echo "<a href='editquizlist.php'>Back to list of quiz questions</a>";
?>
</center>
</HTML>
Thankyou
Post Reply