这周主要做了答题游戏的数据库和php的代码编写,实现了对选项的验证,下面是代码。
</pre><pre name="code" class="php"><?php session_start(); ?>
<?php include "mysql_connect.php"; ?>
<?php
$q=$_GET["q"];//我的选择
$result=explode('|', $q);
if($_SESSION['q_id']==1){
$b="select * from question where id=$_SESSION[q_id]";
$c=mysql_query($b);
$a=mysql_fetch_array($c);
$response="0".'|'."1".'|'.$a['q_content'].'|'.$a['q_answera'].'|'.$a['q_answerb'].'|'.$a['q_answerc'].'|'.$a['q_answerd'].'|'.$a['id'].'/25';
}
elseif($_SESSION['q_id']==26){
if($result[0]==1){
$q="A";
}
elseif($result[0]==2){
$q="B";
}
elseif($result[0]==3){
$q="C";
}
elseif($result[0]==4){
$q="D";
}
$b="select * from question where id=$_SESSION[q_id]-1";
$c=mysql_query($b);
$a=mysql_fetch_array($c);
if($q==$a['q_correctanswer']){
$_SESSION['correct_Answer']+=1;
$response=$q.'|'.$q.'|'."R".'|'."正确:".$_SESSION['correct_Answer']."错误:".$_SESSION['wrong_Answer'];
}
else{
$_SESSION['wrong_Answer']+=1;
$response=$q.'|'.$a['q_correctanswer'].'|'."R".'|'."正确:".$_SESSION['correct_Answer']."错误".$_SESSION['wrong_Answer'];
}
$user_questionCorrect="INSERT INTO user (correct_answer,default_answer) values ('$_SESSION[correct_Answer]','$_SESSION[wrong_Answer]')";//成绩
$result_sql2=mysql_query($user_questionCorrect);
}
elseif($_SESSION['q_id']>=1&&$_SESSION['q_id']<=25){
$b="select * from question where id=$_SESSION[q_id]-1";
$c=mysql_query($b);
$a=mysql_fetch_array($c);
//选的答案与数据库中取得答案进行对比,如果答案不正确,就在数据库中存入答错一题,反之。
//获取每题的答题时间
if($result[0]==1){
$q="A";
}
elseif($result[0]==2){
$q="B";
}
elseif($result[0]==3){
$q="C";
}
elseif($result[0]==4){
$q="D";
}
$result="select * from question where id=$_SESSION[q_id]";
$result_index=mysql_query($result);
$result_indexsql=mysql_fetch_array($result_index);
if($q==$a['q_correctanswer']){
$_SESSION['correct_Answer']+=1;
$response=$q.'|'.$q.'|'.$result_indexsql['q_content'].'|'.$result_indexsql['q_answera'].'|'.$result_indexsql['q_answerb'].'|'.$result_indexsql['q_answerc'].'|'.$result_indexsql['q_answerd'].'|'.$result_indexsql['id']."/25";
}
else{
$_SESSION['wrong_Answer']+=1;
$response=$q.'|'.$a['q_correctanswer'].'|'.$result_indexsql['q_content'].'|'.$result_indexsql['q_answera'].'|'.$result_indexsql['q_answerb'].'|'.$result_indexsql['q_answerc'].'|'.$result_indexsql['q_answerd'].'|'.$result_indexsql['id']."/25";
}
}
echo $response;
$_SESSION['q_id']+=1;
?>
var xmlHttp
function showHint(str,id)
{
xmlHttp=CreateXmlHttp()
//调用XMLHttpRequest对象
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return;
}
var url="gethint.php";
url=url+"?q="+str+"|"+id;
xmlHttp.onreadystatechange=StateChanged ;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function StateChanged(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
var response=xmlHttp.responseText;
var strs= new Array(); //定义一数组
strs=response.split("|");
var a=strs['0']; //所选答案
var b=strs['1']; //正确答案
var q_content=strs['2']; //题目
var q_answera=strs['3']; //选项A
var q_answerb=strs['4']; //选项B
var q_answerc=strs['5']; //选项C
var q_answerd=strs['6']; //选项D
var q_answerid=strs['7']; //问题id
var answer=document.getElementsByTagName("li");
if(a==0){
document.getElementById("question_num").innerHTML=q_answerid;
document.getElementsByClassName("q_question")[0].innerHTML=q_content;
document.getElementsByClassName("list_answer")[0].innerHTML=q_answera;
document.getElementsByClassName("list_answer")[1].innerHTML=q_answerb;
document.getElementsByClassName("list_answer")[2].innerHTML=q_answerc;
document.getElementsByClassName("list_answer")[3].innerHTML=q_answerd;
}
if(a==b){
if(a=="A"){
answer[0].style.backgroundColor="#0f6";
}
else if(a=="B"){
answer[1].style.backgroundColor="#0f6";
}
else if(a=="C"){
answer[2].style.backgroundColor="#0f6";
}
else if(a=="D"){
answer[3].style.backgroundColor="#0f6";
}
setTimeout(question,1000);
}
else if(a!=b&&a!=0){
if(a=="A"){
answer[0].style.backgroundColor="red";
}
else if(a=="B"){
answer[1].style.backgroundColor="red";
}
else if(a=="C"){
answer[2].style.backgroundColor="red";
}
else if(a=="D"){
answer[3].style.backgroundColor="red";
}
if(b=="A"){
answer[0].style.backgroundColor="#0f6";
}
else if(b=="B"){
answer[1].style.backgroundColor="#0f6";
}
else if(b=="C"){
answer[2].style.backgroundColor="#0f6";
}
else if(b=="D"){
answer[3].style.backgroundColor="#0f6";
}
setTimeout(question,1000);
}
checkTime();
function question(){
if(q_content=="R"){
document.getElementById("question_num").innerHTML="结果";
document.getElementsByClassName("q_question")[0].innerHTML=q_answera;
document.getElementsByClassName("list_answer")[0].innerHTML="1";
document.getElementsByClassName("list_answer")[1].innerHTML="2";
document.getElementsByClassName("list_answer")[2].innerHTML="3";
document.getElementsByClassName("list_answer")[3].innerHTML="4";
answer[0].style.background="none";
answer[1].style.background="none";
answer[2].style.background="none";
answer[3].style.background="none";
}
else{
document.getElementById("question_num").innerHTML=q_answerid;
document.getElementsByClassName("q_question")[0].innerHTML=q_content;
document.getElementsByClassName("list_answer")[0].innerHTML=q_answera;
document.getElementsByClassName("list_answer")[1].innerHTML=q_answerb;
document.getElementsByClassName("list_answer")[2].innerHTML=q_answerc;
document.getElementsByClassName("list_answer")[3].innerHTML=q_answerd;
answer[0].style.background="none";
answer[1].style.background="none";
answer[2].style.background="none";
answer[3].style.background="none";
var q_time=document.getElementById("q_time").firstChild.innerHTML="20秒";
}
}
//获取返回数据
}
}
function CreateXmlHttp(){
var xmlHttp=null;
try{
xmlHttp=new XMLHttpRequest();
}
catch(e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function checkTime(){
var time=20;
var record_time=setInterval(q_start,1000);
function q_start(){
var q_time=document.getElementById("q_time");
if(time<10)
{
q_time.firstChild.innerHTML="0"+time+"秒";
}
else
{
q_time.firstChild.innerHTML=time+"秒";
}
time--;
if(time<0){
//alert("时间到");
clearInterval(record_time);
}
}
function q_end(){
clearInterval(record_time);
}
document.getElementsByClassName("choose_index")[0].addEventListener('touchstart',q_end, false);
document.getElementsByClassName("choose_index")[1].addEventListener('touchstart',q_end, false);
document.getElementsByClassName("choose_index")[2].addEventListener('touchstart',q_end, false);
document.getElementsByClassName("choose_index")[3].addEventListener('touchstart',q_end, false);
//addEventListener('touchend',q_end, false);
//
}