zglevk

这个实例主要给大家介绍如何使用jQuery+PHP+MySQL来实现在线测试题,包括动态读取题目,答题完毕后台评分,并返回答题结果。

读取答题列表:

 1 $sql = "select * from quiz order by id asc"; 
 2 $query = mysql_query($sql); 
 3 while ($row = mysql_fetch_array($query)) { 
 4     $answers = explode(\'###\', $row[\'answer\']); 
 5     $arr[] = array( 
 6         \'question\' => $row[\'id\'] . \'、\' . $row[\'question\'], 
 7         \'answers\' => $answers 
 8     ); 
 9 } 
10 $json = json_encode($arr);


生成答题列表:

<div id=\'quiz-container\'></div>


通过遍历出来的列表,生成答题功能:

1 $(function(){  
2     $(\'#quiz-container\').jquizzy({  
3         questions: <?php echo $json;?>, //试题信息  
4         sendResultsURL: \'data.php\' //结果处理地址  
5     });  
6 });


当用户打完题,点击“完成”按钮时,会向ajax.php发送一个Ajax交互请求,ajax.php会根据用户的答题情况,比对正确答案,然后给出用户所得分。

 1 $data = $_REQUEST[\'an\']; 
 2  
 3 $answers = explode(\'|\',$data); 
 4 $an_len = count($answers)-1; //题目数 
 5  
 6 $sql = "select correct from quiz order by id asc"; 
 7  
 8 $query = mysql_query($sql); 
 9 $i = 0; 
10 $score = 0; //初始得分 
11 $q_right = 0; //答对的题数 
12 while($row=mysql_fetch_array($query)){ 
13     if($answers[$i]==$row[\'correct\']){ 
14         $arr[\'res\'][] = 1; 
15         $q_right += 1; 
16     }else{ 
17         $arr[\'res\'][] = 0; 
18     } 
19     $i++; 
20 } 
21 $arr[\'score\'] = round(($q_right/$an_len)*100); //总得分 
22 echo json_encode($arr);


quiz表结构:

 1 CREATE TABLE IF NOT EXISTS `quiz` ( 
 2   `id` int(11) NOT NULL AUTO_INCREMENT, 
 3   `question` varchar(100) NOT NULL, 
 4   `answer` varchar(500) NOT NULL, 
 5   `correct` tinyint(2) NOT NULL, 
 6   PRIMARY KEY (`id`) 
 7 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ; 
 8  
 9 -- 
10 -- 转存表中的数据 `quiz` 
11 -- 
12  
13 INSERT INTO `quiz` (`id`, `question`, `answer`, `correct`) VALUES 
14 (1, \'罗马帝国曾一度辉煌,令人神往,故有“条条大陆通罗马”一说。那么,今天你是怎样理解这一谚语的准确含义的?\', \'A.入乡随俗 ###B.四通八达 ###C.殊途同归 ###D.流连忘返\', 3), 
15 (2, \'找出不同类的一项:\', \'A.斑马 ###B.军马 ###C.赛马 ###D.骏马 ###E.驸马\', 5), 
16 (3, \' 蜡烛在空气中燃烧,蜡烛质量逐渐变小。这说明\', \'A.物质可以自生自灭###B.发生的不是化学变化###C.不遵守质量守恒定律###D.生成物为气体,散发到空气中了\', 4), 
17 (4, \'以下哪位歌手没有获得过《我是歌手》总冠军?\', \'A.羽泉###B.韩磊###C.邓紫棋###D.韩红\', 3), 
18 (5, \'下列哪个标签不是HTML5中的新标签?\', \'A.<article>###B.<canvas>###C.<section>###D.<sub>\', 4);


本文转自:https://www.sucaihuo.com/php/113.html 转载请注明出处!

分类:

技术点:

相关文章:

  • 2021-10-02
  • 2021-10-02
  • 2021-12-18
  • 2021-10-08
  • 2021-11-20
  • 2021-11-28
  • 2022-01-11
  • 2021-12-23
猜你喜欢
  • 2021-05-27
  • 2021-11-27
  • 2021-06-25
  • 2021-07-08
  • 2021-08-13
  • 2022-01-13
  • 2021-10-16
相关资源
相似解决方案