Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $numberOfQuestions = 10;
- $questionNo = rand(1, $numberOfQuestions);
- $text = "Enter your answer";
- $button = "Submit";
- $action = "proceed()";
- $debug = True;
- $verify = False;
- // Start the output buffer
- ob_start();
- ?>
- <html>
- <head>
- <title>GAME!</title>
- <script type="text/javascript">
- function proceed() {
- var form = document.createElement('form');
- form.setAttribute('method', 'post');
- form.setAttribute('action', 'game.php');
- var hiddenField = document.createElement("input");
- hiddenField.setAttribute("type", "hidden");
- hiddenField.setAttribute("name", 'q');
- hiddenField.setAttribute("value", <?php echo "'" . $questionNo . "'";?>);
- form.appendChild(hiddenField);
- hiddenField = document.createElement("input");
- hiddenField.setAttribute("type", "hidden");
- hiddenField.setAttribute("name", 'ans');
- hiddenField.setAttribute("value", "'" + document.getElementById("answer").value + "'");
- form.appendChild(hiddenField);
- form.style.display = 'hidden';
- document.body.appendChild(form)
- form.submit();
- }
- </script>
- <style type="text/css">
- body {
- background: #EEE;
- }
- .content {
- vertical-align: middle;
- text-align: justify;
- position:absolute;
- width: 50%;
- max-width: 600px;
- min-width: 300px;
- min-height: 100px;
- z-index:15;
- top:25%;
- left:25%;
- /*margin:-150px 0 0 -150px;*/
- background: #FFF;
- padding: 10px;
- -moz-border-radius: 20px;
- -webkit-border-radius: 20px;
- -khtml-border-radius: 20px;
- border-radius: 20px;
- }
- .answer {
- text-align: center;
- }
- #answer {
- max-width:600px;
- min-width:300px;
- }
- </style>
- </head>
- <body>
- <?php
- if (!isset($_COOKIE['answered'])) {
- setcookie('answered', urlencode(serialize(array(0 => '0'))));
- setcookie('score', 0);
- }
- if (isset($_POST['q'])) {
- $questionNo = $_POST['q'];
- $text = str_replace("\\", "", $_POST['ans']);
- $text = ltrim(rtrim($text, "'"), "'");
- $button = "Next one";
- $action = "window.location='game.php';";
- $verify = True;
- } else {
- $verify = False;
- $answered = unserialize(urldecode($_COOKIE['answered']));
- if (gettype($answered) == 'boolean') {
- $answered[0] = "";
- }
- while (True) {
- if (array_search($questionNo, $answered) > 0 && count($answered) < ($numberOfQuestions + 1)) {
- $questionNo = $questionNo + 1;
- $questionNo = ($questionNo > $numberOfQuestions ? $questionNo - $numberOfQuestions : $questionNo);
- } else {
- $answered[count($answered)] = $questionNo;
- setcookie('answered', urlencode(serialize($answered)));
- break;
- }
- }
- }
- if (count($answered) > ($numberOfQuestions + 1)) {
- echo '<div class="content">';
- echo "<h2>CONGRATULATIONS!</h2>";
- echo "You've answered all the questions! Your score is <b>" . $_COOKIE['score'] . "</b> out of " . $numberOfQuestions . ".<br><br>";
- echo '<input type="button" value="Go again?" onclick="window.location=\'game.php\';">';
- echo "<br>";
- setcookie ("score", "", time() - 3600);
- setcookie ("answered", "", time() - 3600);
- } else {
- showContent($questionNo, $button, $action, $text, $verify, $debug);
- // Just send everything to the browser
- }
- ob_end_flush();
- function showContent($questionNo, $button, $action, $text, $verify, $debug=False){
- echo '<div class="content">';
- $conn = mysql_connect("localhost","root","") or die(mysql_error());
- mysql_select_db('server2go', $conn) or die(mysql_error());
- $result = mysql_query("SELECT * FROM teasers WHERE TeaserID LIKE " . $questionNo) or die(mysql_error());
- while ($row = mysql_fetch_array($result)){
- echo '<p>' . $row['Question'] . '</p>';
- $dbentry = $row;
- }
- ?>
- <p class="answer">
- <textarea id="answer"><?php echo $text;?></textarea>
- <br>
- <input type="button" value=<?php echo '"' . $button . '"';?> onclick="<?php echo $action?>">
- </p>
- <?php
- if ($verify) {
- // This should be called only once an answer has been entered
- $answer = $text;
- echo '<br><br>Our answer: ' . $dbentry['LongAnswer'] . "<br>";
- echo ' You were <b>';
- $pattern = "/(" . str_replace(" ", "", str_replace(",", "|", $dbentry['Answers'])) . ")/sim";
- preg_match_all($pattern, $answer, $matches);
- if (count($matches[0]) >= $dbentry['AmountReq']) {
- $score = $_COOKIE['score'];
- setcookie('score', $score + 1);
- echo "correct!";
- } else {
- echo "WRONG!";
- }
- echo "</b>";
- if ($debug){
- echo "</div>";
- echo "Amount of keywords required: " . $dbentry['AmountReq'] . " (" . $dbentry['Answers'] . ")<br>";
- echo "Amount detected: " . count($matches[0]) . " VS " . count($matches);
- echo "<br>Pattern: " . $pattern . "<br>Result: ";
- echo print_r($matches);
- echo "<br>";
- echo "Question no. " . $dbentry['TeaserID'] . " vs ";
- }
- }
- }
- echo "</div>";
- if ($debug){
- echo $questionNo . "<br>";
- echo "I IS " . $i . "<br>COOKIE: ";
- echo print_r(unserialize(urldecode($_COOKIE['answered'])));
- echo "<br>SCORE: " . $_COOKIE['score'];
- echo "<br>AMOUNT ANSWERED: " . count($answered) . "; <br>PER COOKIE: " . count(unserialize(urldecode($_COOKIE['answered'])));
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement