Home  >  Article  >  Backend Development  >  Online question answering for php+mysql

Online question answering for php+mysql

WBOY
WBOYOriginal
2016-08-08 09:29:551251browse

寒假在公司实习做了一个小东西,用php实现一个在线答题的项目,用户的答题数用switch进行关联,一开始思考用ajax实现页面的自刷新,之后觉得太大材小用了,可以在当前页面做一个自循环,用js来进行计时,当时写完后发现整个逻辑还是存在一定的问题,js中的跳转和表单的操作是分离的导致判断正确题目时会少进行一次,最后只能采取用两个界面分别显示结果。希望在将来能够对此进行改进,下面贴上代码

connetvar.php

<?php
  // Define database connection constants
  define(&#39;DB_HOST&#39;, &#39;localhost&#39;);
  define(&#39;DB_USER&#39;, &#39;root&#39;);
  define(&#39;DB_PASSWORD&#39;, &#39;&#39;);
  define(&#39;DB_NAME&#39;, &#39;test&#39;);
?>

index.php

<?php 
session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>问答</title>
</head>
<body>
<script type="text/javascript">
	window.onload=function(){
	setInterval(function(){
		var text=document.getElementById("text");
		var value=text.value;//innerHTML
		if(value>0){
			value-=1;
			text.value=value;
		}else{
			location.href="show_js.php";
			return false;
		}
	},1000);
	
}
</script>
<?php
require_once(&#39;connectvars.php&#39;);
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc,&#39;utf8&#39;);
	
if(isset($_POST[&#39;index&#39;])){
	$index= $_POST[&#39;index&#39;];
	$choice=$_POST[&#39;choice&#39;];
	//check
	$arr = $_SESSION[&#39;arr&#39;];
	//$arrconut = $_SESSION[&#39;conut($arr)&#39;];
	$query = "SELECT * FROM question where id=$arr[$index]";
	$results = mysqli_query($dbc, $query);
	$row = mysqli_fetch_row($results);
	$answer=$row[&#39;5&#39;];
	$_SESSION[&#39;index&#39;] = $index;
    if ($choice == $answer)
	{
		echo"<script type=&#39;text/javascript&#39;>alert('答对了,下一题');</script>";//location='index1.php';

	}
	else
	{
		echo"<script type=&#39;text/javascript&#39;>alert('你答错了,游戏结束');location='show.php';</script>";
	}

	if($index == count($arr)-1) {

		$index=$index+1;
		$_SESSION['index'] = $index;
		echo"<script type=&#39;text/javascript&#39;>alert('题都给你答完了');location='show.php';</script>";exit;
	}
	else {
		$index++;
		//var_dump($index);
		//var_dump($_SESSION['index']);
		//var_dump(count($arr));

	}

}
else {
	$arr = range(1, 5);
	shuffle($arr);
	$_SESSION['arr'] = $arr;
	$index = 0;
}
?>
<?php
$query = "SELECT * FROM question where id=$arr[$index]";
$results = mysqli_query($dbc, $query);
$row = mysqli_fetch_row($results);
?>
<form method="post" action="index.php">
<?php echo $index+1 ?> <?php echo $row[&#39;0&#39;]?><br />
<input type="radio" name="choice" value="A" />A.<?php echo $row[&#39;1&#39;]?><br />
<input type="radio" name="choice" value="B" />B.<?php echo $row[&#39;2&#39;]?><br />
<input type="radio" name="choice" value="C" />C.<?php echo $row[&#39;3&#39;]?><br />
<input type="radio" name="choice" value="D" />D.<?php echo $row[&#39;4&#39;]?><br />
<input type="submit" value="决定是你了" /> <input type="reset" value="让我再想想" />
<input type="hidden" name="index" value="<?php echo $index ?>" /><br />
您还有<input type="text" name="clock" size="2" value="10" id="text" />秒的答题时间
</form>
</body>
</html>

show.php

<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Guitar Wars - High Scores</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php 
$id = $_SESSION[&#39;index&#39;] ;
echo &#39;你一共答对了&#39;.$id.&#39;题&#39;;
//var_dump($id);
switch ($id)
{
	case 0 :              
		echo &#39;你的折扣为9.5折&#39;;//用户自行修改case和相应的折扣参数
		break;
    case 1 :
		echo &#39;你的折扣为9折&#39;;
		break;
	case 2 :
		echo &#39;你的折扣为8.5折&#39;;
		break;
	case 3 :
		echo &#39;你的折扣为8折&#39;;
		break;
	case 4 :
		echo &#39;你的折扣为7.5折&#39;;
		break;
	case 5 :
		echo &#39;你的折扣为7折&#39;;
		break;
}
?>
 
  <hr />
  </body> 
</html>
<?php 

unset($_SESSION[&#39;index&#39;]);

?>

show_js.php

<?php session_start();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Guitar Wars - High Scores</title>
  <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php 
$id = $_SESSION[&#39;index&#39;]+1 ;
echo &#39;你一共答对了&#39;.$id.&#39;题&#39;;
//var_dump($id);
switch ($id)
{
	case 0 :              
		echo &#39;你的折扣为9.5折&#39;;//用户自行修改case和相应的参数
		break;
    case 1 :
		echo &#39;你的折扣为9折&#39;;
		break;
	case 2 :
		echo &#39;你的折扣为8.5折&#39;;
		break;
	case 3 :
		echo &#39;你的折扣为8折&#39;;
		break;
	case 4 :
		echo &#39;你的折扣为7.5折&#39;;
		break;
	case 5 :
		echo &#39;你的折扣为7折&#39;;
		break;
}
?>
 
  <hr />
  </body> 
</html>
<?php 

unset($_SESSION[&#39;index&#39;]);

?>

以上就介绍了php+mysql的在线答题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn