Jump to content

Quiz Help


billynastie

Recommended Posts

Hi hope somebody can help me firstly I am writing a piece of code to run an online quiz simpy and easy firstly the grid looks all images 10 in total with a submit box underneath for answers then when an answer is inserted it puts it into the answers table on my database which is easy enough where I am having problems is back on the main screen when a timer runs out and if an answer has been placed it read the database table and picks one at random the what it needs to do is show the winners details instead of a image I have managed to get this working on one table cell but when a second correct answer is found it will not display both winners.

Mysql Database tables


SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `quiz`
--

-- --------------------------------------------------------

--
-- Table structure for table `answers`
--

CREATE TABLE IF NOT EXISTS `answers` (
  `ID` int(10) NOT NULL AUTO_INCREMENT,
  `table` int(10) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `userID` int(11) NOT NULL,
  `won` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `answers`
--

INSERT INTO `answers` (`ID`, `table`, `answer`, `userID`, `won`) VALUES
(1, 1, 'SUPERDRUG', 1, 1),
(2, 1, 'SUPERDRUG', 2, 1),
(3, 1, 'SUPERDRUG', 3, 1),
(4, 1, 'DINNER', 1, 1);

-- --------------------------------------------------------

--
-- Table structure for table `questions`
--

CREATE TABLE IF NOT EXISTS `questions` (
  `ID` int(10) NOT NULL AUTO_INCREMENT,
  `table` int(10) NOT NULL,
  `answer` varchar(255) NOT NULL,
  `image` varchar(255) NOT NULL,
  `userID` int(10) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `questions`
--

INSERT INTO `questions` (`ID`, `table`, `answer`, `image`, `userID`) VALUES
(1, 1, 'SUPERDRUG', 'images/100.jpg', 3),
(2, 1, 'DINNER', 'images/200.jpg', 1);





Index.php page structure.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript" src="includes/timerbar.js">
</script> 
</head>

<body>
<?php include("includes/db.php"); ?>
<?php
$array[0] = "SUPERDRUG";
$array[1] = "BADASS";
$array[2] = "TIME";
$array[3] = "DINNER";

?>
<table width="50%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><?php 
$image = "SELECT * FROM questions WHERE answer = '$array[0]' OR answer = '$array[1]' OR answer = '$array[2]' OR answer = '$array[3]'"; 
$query = "SELECT questions.answer, answers.userID, answers.won ".
"FROM questions, answers ".
"WHERE questions.answer = answers.answer ORDER BY RAND() LIMIT 1";
$imageresult = mysql_query($image) or die(mysql_error());
$imagerow = mysql_fetch_array($imageresult) or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
$id = $row['userID'];

if ( $row['answer'] == $array[0] && $row['won'] == '1' ){
$updatequery = mysql_query("SELECT * FROM questions WHERE answer='$array[0]'") 
or die(mysql_error());  
$winner = mysql_fetch_array( $updatequery );
echo "Well Done ".$winner['userID']." Your a winner";
}	elseif ( $row['answer'] == $array[0] && $row['won'] =='0') {
mysql_query("UPDATE questions SET userID=$id WHERE answer='$array[0]'") 
or die(mysql_error());
mysql_query("UPDATE answers SET won=1 WHERE answer='$array[0]'") 
or die(mysql_error());
}  else {
echo "<img src=\"".$imagerow['image']."\"width=\"150\" height=\"50\" />";
}
}
?></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td><?php 
$image3 = "SELECT * FROM questions WHERE answer = '$array[3]'"; 
$imageresult3 = mysql_query($image3) or die(mysql_error());
$imagerow3 = mysql_fetch_array($imageresult3) or die(mysql_error());

$query3 = "SELECT questions.answer, answers.userID, answers.won ".
"FROM questions, answers ".
"WHERE questions.answer = answers.answer ORDER BY RAND() LIMIT 1";

$result3 = mysql_query($query3) or die(mysql_error());
while($row3 = mysql_fetch_array($result3)){
$id = $row3['userID'];
if ( $row3['answer'] == $array[3] && $row3['won'] == '1' ){
$updatequery3 = mysql_query("SELECT * FROM questions WHERE answer='$array[3]'") 
or die(mysql_error());  
$winner3 = mysql_fetch_array( $updatequery3 );
echo "Well Done ".$winner3['userID']." Your a winner";
}	elseif ( $row3['answer'] == $array[3] && $row3['won'] =='0') {
mysql_query("UPDATE questions SET userID=$id WHERE answer='$array[3]'") 
or die(mysql_error());
mysql_query("UPDATE answers SET won=1 WHERE answer='$array[3]'") 
or die(mysql_error());
}  else {
echo "<img src=\"".$imagerow3['image']."\"width=\"150\" height=\"50\" />";
}
}
?></td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td> </td>
    <td> </td>
  </tr>
</table>
<h4>Place your answer:</h4>
<form action="submit.php" method="post"> 

Answer: 
  <input name="answer" type="text" /> 
  <input name="quizid" type="hidden" value="1" />
<input type="submit" />
</form>
</body>
</html>

If anybody can help me sort out how to get it to show two static winners I would be most appreciative and then I will be able to work out the rest for myself.

Link to comment
https://forums.phpfreaks.com/topic/159497-quiz-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.