View difference between Paste ID: E4wVcFQk and 1J3BwVSY
SHOW: | | - or go back to the newest paste.
1
<?php 
2
$numberOfQuestions = 10;
3
$questionNo = rand(1, $numberOfQuestions);
4
$text = "Enter your answer";
5
$button = "Submit";
6
$action = "proceed()";
7
$debug = True;
8
$verify = False;
9
10
// Start the output buffer
11
ob_start();
12
?>
13
<html>
14
<head>
15
	<title>GAME!</title>
16
	<script type="text/javascript">
17
	function proceed() {
18
		var form = document.createElement('form');
19
		form.setAttribute('method', 'post');
20
		form.setAttribute('action', 'game.php');
21
22
		var hiddenField = document.createElement("input");
23
		hiddenField.setAttribute("type", "hidden");
24
		hiddenField.setAttribute("name", 'q');
25
		hiddenField.setAttribute("value", <?php echo "'" . $questionNo . "'";?>);
26
		form.appendChild(hiddenField);
27
28
		hiddenField = document.createElement("input");
29
		hiddenField.setAttribute("type", "hidden");
30
		hiddenField.setAttribute("name", 'ans');
31
		hiddenField.setAttribute("value", "'" + document.getElementById("answer").value + "'");
32
		form.appendChild(hiddenField);
33
		
34
		form.style.display = 'hidden';
35
		document.body.appendChild(form)
36
		form.submit();
37
	}
38
	</script>
39
	<style type="text/css">
40
		body {
41
			background: #EEE;
42
		}
43
		.content {
44
			vertical-align: middle;
45
			text-align: justify;
46
		    position:absolute;
47
		    width: 50%;
48
		    max-width: 600px;
49
		    min-width: 300px;
50
		    min-height: 100px;
51
		    z-index:15;
52
		    top:25%;
53
		    left:25%;
54
		    /*margin:-150px 0 0 -150px;*/
55
		    background: #FFF;
56
		    padding: 10px;
57
		    -moz-border-radius: 20px;
58
		    -webkit-border-radius: 20px;
59
		    -khtml-border-radius: 20px;
60
		    border-radius: 20px;
61
		}
62
		.answer {
63
			text-align: center;
64
		}
65
		#answer {
66
			max-width:600px;
67
			min-width:300px;
68
		}
69
	</style>
70
</head>
71
<body>
72
<?php
73
if (!isset($_COOKIE['answered'])) {
74
	setcookie('answered', urlencode(serialize(array(0 => '0'))));
75
	setcookie('score', 0);
76
}
77
if (isset($_POST['q'])) {
78
	$questionNo = $_POST['q'];
79
	$text = str_replace("\\", "", $_POST['ans']);
80
	$text = ltrim(rtrim($text, "'"), "'");
81
	$button = "Next one";
82
	$action = "window.location='game.php';";
83
	$verify = True;
84
} else {
85
	$verify = False;
86
	$answered = unserialize(urldecode($_COOKIE['answered']));
87
	if (gettype($answered) == 'boolean') {
88
		$answered[0] = "";
89
	}
90
91
	while (True) {
92
		if (array_search($questionNo, $answered) > 0 && count($answered) < ($numberOfQuestions + 1)) {
93
			$questionNo = $questionNo + 1;
94
			$questionNo = ($questionNo > $numberOfQuestions ? $questionNo - $numberOfQuestions : $questionNo);
95
		} else {
96
			$answered[count($answered)] = $questionNo;
97
			setcookie('answered', urlencode(serialize($answered)));
98
			break;
99
		}
100
	}
101
}
102
103
if (count($answered) > ($numberOfQuestions + 1)) {
104
	echo '<div class="content">';
105
	echo "<h2>CONGRATULATIONS!</h2>";
106
	echo "You've answered all the questions! Your score is <b>" . $_COOKIE['score'] . "</b> out of " . $numberOfQuestions . ".<br><br>";
107
	echo '<input type="button" value="Go again?" onclick="window.location=\'game.php\';">';
108
	echo "<br>";
109
	setcookie ("score", "", time() - 3600);
110
	setcookie ("answered", "", time() - 3600);
111
} else {
112
	showContent($questionNo, $button, $action, $text, $verify, $debug);
113
	// Just send everything to the browser
114
}
115
ob_end_flush();
116
117
function showContent($questionNo, $button, $action, $text, $verify, $debug=False){
118
	echo '<div class="content">';
119-
	// $conn = mysql_connect("fdb4.freehostingeu.com","1259147_9263","Renna1991") or die(mysql_error());
119+
120-
	// mysql_select_db('1259147_9263', $conn) or die(mysql_error());
120+
121
122
	$result = mysql_query("SELECT * FROM teasers WHERE TeaserID LIKE " . $questionNo) or die(mysql_error());
123
	while ($row = mysql_fetch_array($result)){
124
		echo '<p>' . $row['Question'] . '</p>';
125
		$dbentry = $row;
126
	}
127
	?>
128
	<p class="answer">
129
	<textarea id="answer"><?php echo $text;?></textarea>
130
	<br>
131
	<input type="button" value=<?php echo '"' . $button . '"';?> onclick="<?php echo $action?>">
132
	</p>
133
134
	<?php
135
	if ($verify) {
136
	// This should be called only once an answer has been entered
137
		$answer = $text;
138
		echo '<br><br>Our answer: ' . $dbentry['LongAnswer'] . "<br>";
139
		echo ' You were <b>';
140
		$pattern = "/(" . str_replace(" ", "", str_replace(",", "|", $dbentry['Answers'])) . ")/sim";
141
		preg_match_all($pattern, $answer, $matches);
142
		if (count($matches[0]) >= $dbentry['AmountReq']) {
143
			$score = $_COOKIE['score'];
144
			setcookie('score', $score + 1);
145
			echo "correct!";
146
		} else {
147
			echo "WRONG!";
148
		}
149
		echo "</b>";
150
151
		if ($debug){
152
			echo "</div>";
153
			echo "Amount of keywords required: " . $dbentry['AmountReq'] . " (" . $dbentry['Answers'] . ")<br>";
154
			echo "Amount detected: " . count($matches[0]) . " VS " . count($matches);
155
			echo "<br>Pattern: " . $pattern . "<br>Result: ";
156
			echo print_r($matches);
157
			echo "<br>";
158
			echo "Question no. " . $dbentry['TeaserID'] . " vs ";
159
		}
160
	}
161
}
162
163
echo "</div>";
164
if ($debug){
165
	echo $questionNo . "<br>";
166
	echo "I IS " . $i . "<br>COOKIE: ";
167
	echo print_r(unserialize(urldecode($_COOKIE['answered'])));
168
	echo "<br>SCORE: " . $_COOKIE['score'];
169
	echo "<br>AMOUNT ANSWERED: " . count($answered) . "; <br>PER COOKIE: " . count(unserialize(urldecode($_COOKIE['answered'])));
170
}
171
?>