寒假在公司实习做了一个小东西,用php实现一个在线答题的项目,用户的答题数用switch进行关联,一开始思考用ajax实现页面的自刷新,之后觉得太大材小用了,可以在当前页面做一个自循环,用js来进行计时,当时写完后发现整个逻辑还是存在一定的问题,js中的跳转和表单的操作是分离的导致判断正确题目时会少进行一次,最后只能采取用两个界面分别显示结果。希望在将来能够对此进行改进,下面贴上代码
connetvar.php
<?php
// Define database connection constants
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'test');
?>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('connectvars.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc,'utf8');
if(isset($_POST['index'])){
$index= $_POST['index'];
$choice=$_POST['choice'];
//check
$arr = $_SESSION['arr'];
//$arrconut = $_SESSION['conut($arr)'];
$query = "SELECT * FROM question where id=$arr[$index]";
$results = mysqli_query($dbc, $query);
$row = mysqli_fetch_row($results);
$answer=$row['5'];
$_SESSION['index'] = $index;
if ($choice == $answer)
{
echo"<script type='text/javascript'>alert('答对了,下一题');</script>";//location='index1.php';
}
else
{
echo"<script type='text/javascript'>alert('你答错了,游戏结束');location='show.php';</script>";
}
if($index == count($arr)-1) {
$index=$index+1;
$_SESSION['index'] = $index;
echo"<script type='text/javascript'>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['0']?><br />
<input type="radio" name="choice" value="A" />A.<?php echo $row['1']?><br />
<input type="radio" name="choice" value="B" />B.<?php echo $row['2']?><br />
<input type="radio" name="choice" value="C" />C.<?php echo $row['3']?><br />
<input type="radio" name="choice" value="D" />D.<?php echo $row['4']?><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['index'] ;
echo '你一共答对了'.$id.'题';
//var_dump($id);
switch ($id)
{
case 0 :
echo '你的折扣为9.5折';//用户自行修改case和相应的折扣参数
break;
case 1 :
echo '你的折扣为9折';
break;
case 2 :
echo '你的折扣为8.5折';
break;
case 3 :
echo '你的折扣为8折';
break;
case 4 :
echo '你的折扣为7.5折';
break;
case 5 :
echo '你的折扣为7折';
break;
}
?>
<hr />
</body>
</html>
<?php
unset($_SESSION['index']);
?>show_js.php
立即学习“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['index']+1 ;
echo '你一共答对了'.$id.'题';
//var_dump($id);
switch ($id)
{
case 0 :
echo '你的折扣为9.5折';//用户自行修改case和相应的参数
break;
case 1 :
echo '你的折扣为9折';
break;
case 2 :
echo '你的折扣为8.5折';
break;
case 3 :
echo '你的折扣为8折';
break;
case 4 :
echo '你的折扣为7.5折';
break;
case 5 :
echo '你的折扣为7折';
break;
}
?>
<hr />
</body>
</html>
<?php
unset($_SESSION['index']);
?>以上就介绍了php+mysql的在线答题,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号