Location: PHPKode > projects > PHP Trivia > php-trivia/question.php
<?php
// ***************************************************************************
// *
// * PHP Dynamic Trivia -- Add your own trivia file.
// * Copyright (C) 2005  Robin van de Vusse (hide@address.com)
// * 
// * This program is free software; you can redistribute it and/or
// * modify it under the terms of the GNU General Public License
// * as published by the Free Software Foundation; either version 2
// * of the License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU General Public License for more details.
// * 
// * You should have received a copy of the GNU General Public License
// * along with this program; if not, write to the Free Software
// * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// *
// ***************************************************************************
require_once "config.php";

if (strlen(trim($strSessionPath)) > 0)
	session_save_path($strSessionPath);

session_start();
?>
<html>
<head>
<title>Question</title>
</head>
<body>

<table width='800'>
	<tr>
		<td width='160'><table></table></td>
		<td width='640' align='center'>
			<table>Play</table>
		</td>		
	</tr>
</table>

<hr>

<table width='800'>
	<tr>
		<td width='160' valign='top' align='center' STYLE='background-color: #9999cc;'>
		</td>
		<td width='640' align='center'>
			<table>
<?php
$strMessage = "";
$boolError = false;
$strName = "";
$intUserNumber=0;
$strTempDBInfo = "";
$min = 1;
$max = 0;
$RandomRecordID = 0;

if (empty($_SESSION))
{
	$boolError = true;
	$strName = "";
}
else
{
	if (strlen(trim($_SESSION['name'])) > 0)
	{
		$boolError = false;
		$strName = $_SESSION['name'];
		$intUserNumber = $_SESSION['usernumber'];
	}
	else
	{
		$boolError = true;
		$strName = "";
		$intUserNumber = 0;
		$strMessage = "You must log in to proceed.";
	}
}

if (!$boolError)
{
	if (strlen(trim($_GET['strTriviaTable'])) > 0)
	{
		$boolError = false;
		$_SESSION['triviatable'] = $_GET['strTriviaTable'];
		$strTriviaTable = $_GET['strTriviaTable'];

	}
	else
	{
		$boolError = true;
		$_SESSION['triviatable'] = "";
		$strMessage = "No trivia game has been selected.";
	}
}

if (!$boolError)
{
	$strTempDBInfo =  DBTYPE."_pconnect";
	$conn = $strTempDBInfo($DB_HOST,$DB_USER,$DB_PASS);
	
	if (!$conn) 
	{
		$strTempDBInfo =  DBTYPE."_error";
		$strMessage = "Unable to connect to DB server: " . $strTempDBInfo($conn);
		$boolError = true;
	}
	
	$strTempDBInfo = DBTYPE."_select_db";
}

if (!$boolError && (!$strTempDBInfo($DB_NAME)))
{
	$strTempDBInfo = DBTYPE."_error";
	$strMessage = "Unable to select DB name: " . $strTempDBInfo($conn);
	$strTempDBInfo = DBTYPE."_close";
	$strTempDBInfo($conn);
	$boolError = true;   		
}

// This is where the question is marked as answered wrong.  The question is ALWAYS marked as
// aswered wrong just before the question is asked.  After the question is answered correctly
// the table is updated properly.  This is to prevent people from hitting F5 (refresh) or
// by selecting Back inorder to get out of answering a question without being marked for not
// answering.
if (!$boolError)
{
	$sql =	"SELECT TableName, UserNumber, QuestionsAnswered, QuestionsCorrect, CurrentStreak, BestStreak FROM highscores " .
		"WHERE TableName = '$strTriviaTable' AND UserNumber = $intUserNumber";
	
	$strTempDBInfo = DBTYPE."_query";
	$result = $strTempDBInfo($sql);
	
	if (!$result) 
	{
		$_SESSION['usernumber'] = 0;
		$_SESSION['name'] = "";
		$strTempDBInfo = DBTYPE."_error";
		$strMessage = "Could not successfully run query ($sql) from DB: " . $strTempDBInfo($conn);
		$boolError = true;   		
	}
}

if (!$boolError)
{	
	$strTempDBInfo = DBTYPE."_num_rows";
	
	if ($strTempDBInfo($result) == 0)
	{
		$sql =	"INSERT INTO highscores (TableName, UserNumber, QuestionsAnswered, QuestionsCorrect, CurrentStreak, BestStreak) " .
			"VALUES ('$strTriviaTable', $intUserNumber, 1, 0, 0, 0)";
		$strTempDBInfo = DBTYPE."_query";
		$result = $strTempDBInfo($sql);
		
		if (!$result) 
		{
			$_SESSION['usernumber'] = 0;
			$_SESSION['name'] = "";
			$strTempDBInfo = DBTYPE."_error";
			$strMessage = "Could not successfully run query ($sql) from DB: " . $strTempDBInfo($conn);
			$boolError = true;   		
		}
	}
	else
	{
		$strTempDBInfo = DBTYPE."_fetch_assoc";
		$row = $strTempDBInfo($result);			
		
		// Make sure in questionproc.php that questions answered does NOT
		// get incremented there anymore.  However, replace CurrentStreak
		// with the original number if the question was answered correct.
		$intQuestionsAnswered = $row['QuestionsAnswered'];
		$intQuestionsCorrect = $row['QuestionsCorrect'];
		$intCurrentStreak = $row['CurrentStreak'];
		$intBestStreak = $row['BestStreak'];

		$intQuestionsAnswered = $intQuestionsAnswered + 1;
		$_SESSION['prevCurrentStreak'] = $intCurrentStreak;

		$sql =	"UPDATE highscores SET " .
				"QuestionsAnswered = $intQuestionsAnswered, " . 
				"QuestionsCorrect = $intQuestionsCorrect, " .
				"CurrentStreak = 0, " .
				"BestStreak = $intBestStreak " .
			"WHERE UserNumber = $intUserNumber AND TableName = '$strTriviaTable'";

		$strTempDBInfo = DBTYPE."_query";
		$result = $strTempDBInfo($sql);
		
		if (!$result) 
		{
			$_SESSION['usernumber'] = 0;
			$_SESSION['name'] = "";
			$strTempDBInfo = DBTYPE."_error";
			$strMessage = "Could not successfully run query ($sql) from DB: " . $strTempDBInfo($conn);
			$boolError = true;   		
		}
	}
}

if (!$boolError)
{
	$sql = "SELECT count( question ) AS QuestionCount FROM " . $strTriviaTable ;
	$strTempDBInfo = DBTYPE."_query";
	$result = $strTempDBInfo($sql);

	if (!$result)
	{
		$strTempDBInfo = DBTYPE."_error";
		$strMessage = "Could not successfully run query ($sql) from DB: " . $strTempDBInfo($conn);
		$boolError = true;   		
	}
}

if (!$boolError)
{
	$strTempDBInfo = DBTYPE."_num_rows";
	
	if ($strTempDBInfo($result) == 1)
	{
		$strTempDBInfo = DBTYPE."_fetch_assoc";
		$row = $strTempDBInfo($result);			
		
		$max = $row['QuestionCount'];
		$strMessage = "";
		$boolError = false;	
	}
	else
	{
		$strMessage = "Query was not executed properly.";
		$boolError = true;
	}
}

if (!$boolError)
{
	if ($min < $max)
	{
		$RandomRecordID = mt_rand($min, $max);
		$boolError = false;
	}
	else
	{
		$strMessage = "Random record number could not be generated.";
		$boolError = true;
	}
}

if (!$boolError)
{
	$sql = "SELECT question, answer FROM " . $strTriviaTable . " WHERE id = " . $RandomRecordID;
	$strTempDBInfo = DBTYPE."_query";
	$result = $strTempDBInfo($sql);

	if (!$result)
	{
		$strTempDBInfo = DBTYPE."_error";
		$strMessage = "Could not successfully run query ($sql) from DB: " . $strTempDBInfo($conn);
		$boolError = true;   		
	}
}

if (!$boolError)
{
	$strTempDBInfo = DBTYPE."_num_rows";
	
	if ($strTempDBInfo($result) == 1)
	{
		$strTempDBInfo = DBTYPE."_fetch_assoc";
		$row = $strTempDBInfo($result);					
		$strMessage = "";
		$boolError = false;	
	}
	else
	{
		$strMessage = "Query was not executed properly.";
		$boolError = true;
	}
}

if ($boolError)
{
	$_SESSION['name'] = "";
	$_SESSION['message'] = $strMessage;	
	
	header("Location: index.php");
	exit;
}
else
{
	$strRow = str_replace($strCrLf, "<br>\n", $row['question']);

	echo "<form name='questionproc' action='questionproc.php' method='POST'>\n";
	echo "<table width='212' height='234' border='0' cellpadding='0' cellspacing='0' class='noborder'>\n";
	echo "	<tr>\n";
	echo "		<td valign='bottom' align='center' class='noborder'>\n";
	echo "			<font face=\"Courier New\">\n" . $strRow . "<br>\n";
	echo "			</font>\n";
	echo "		</td>\n";
	echo "	</tr>\n";

	$strRow = $row['answer'];
	$strAnswer = explode($strFieldSeperator, $strRow);
	$max = count($strAnswer);
	$RandomAnswer = mt_rand($min, $max);
	$strAnswer[$RandomAnswer - 1] = trim($strAnswer[$RandomAnswer - 1]);
	$strMask = strtolower($strAnswer[$RandomAnswer - 1]);	
	
	for ($i = 97; $i <= 122; $i++)
	{
		$strMask = str_replace(chr($i), ".", $strMask);
	}
	
	$i = strlen($strMask);
	
	echo "	<tr>\n";
	echo "		<td valign='bottom' align='center' class='noborder'>\n";
	echo "			<INPUT NAME='youranswer' STYLE='font-family: Courier New' MAXLENGTH=$i SIZE=$i><br>\n";
	echo "			<INPUT disabled VALUE=\"$strMask\" STYLE=\"font-family: Courier New\" MAXLENGTH=$i SIZE=$i><br>\n";
//	echo "			<INPUT readonly NAME='realanswer' STYLE='font-family: Courier New' VALUE=\"" . $strAnswer[$RandomAnswer - 1] . "\" MAXLENGTH=$i SIZE=$i><br>\n";
	echo "			<INPUT TYPE='hidden' NAME='realanswer' STYLE='font-family: Courier New' VALUE=\"" . $strAnswer[$RandomAnswer - 1] . "\" MAXLENGTH=$i SIZE=$i><br>\n";
	echo "		</td>\n";
	echo "	</tr>\n";
	echo "	<tr>\n";
	echo "		<td valign='bottom' align='center' class='noborder'>\n";
	echo "			<input type='submit' value='OK' class='button'>\n";
	echo "		</td>\n";
	echo "	</tr>\n";
	echo "</table>\n";
	echo "</form>\n";	
}
?>
			</table>
		</td>
	</tr>
</table>

<?php require_once "footer.php"; ?>
</body>
</html>

<script type="text/javascript">
<!--
document.questionproc.youranswer.focus();
//-->
</script>
Return current item: PHP Trivia