smarty练习: 设置试题及打印试卷

数据库表格:shiti, shititimu, timu, kemu, xuanxiang

根据科目设置一个可以添加试题的页面:(如下图)

具体的题目从数据库中查出并形成一张试卷的形式

考试试题设置:

考试试题设置后台:ksset.php

1 <?php
2 include("../init.inc.php");
3 include("../../DBDA.class.php");
4 $db=new DBDA();
5 $sql="select * from kemu";
6 $attr=$db->Query($sql);
7 $smarty->assign("kemu",$attr);
8 $smarty->display("ksset.html");
View Code

考试试题设置前端:ksset.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>考试设置</title>
 6 <script src="../../jquery-1.11.2.min.js"></script>
 7 </head>
 8 
 9 <body>
10 <h1>考试试题设置</h1>
11 <form action="kschuli.php" method="post">
12 <div>请输入题目编号:<input type="text" name="code" /></div>
13 <div>请输入题目名称:<input type="text" name="name" /></div>
14 <div>请选择考试科目:
15 <select name="kemu" id="km">
16 <{foreach $kemu as $v}>
17 <option value="<{$v[0]}>"><{$v[1]}></option>
18 <{/foreach}>
19 </select>
20 </div>
21 <div>请输入考试时间:<input type="text" name="shijian" />分钟</div>
22 <div>请输入题目数量:<input type="text" name="shuliang" id="sl" /></div>
23 <div>请输入题目分值:<input type="text" name="fenzhi" /></div>
24 <input type="submit" value="提交" id="tj"/>
25 </form>
26 </body>
27 <script type="text/javascript">
28 //当发现考试科目中的试题数量小于输入的数量值时怎么办?
29 //点击提交时进行提醒
30 $(document).ready(function(e) {
31     $("#tj").click(function(){
32         //取科目的数量和输入的数量进行比较
33         var km=$("#km").val();
34         var sl=$("#sl").val();
35         var bs="";
36         
37         $.ajax({
38             async:false,
39             url:"bijiaosl.php",
40             data:{km:km,sl:sl},
41             type:"POST",
42             dataType:"TEXT",
43             success: function(data){                
44                 bs=data;
45                 }
46             });
47             
48         if(bs.trim()=="OK")
49         {
50             return true;
51             }
52         else
53         {
54             alert("该科目下题目数量不足!")
55             return false;
56             }
57         
58         })
59 });
60 </script>
61 </html>
View Code

提交处理页面:kschuli.php

 1 <?php
 2 include("../../DBDA.class.php");
 3 $db=new DBDA();
 4 //往试题表添加考试题目,科目等信息
 5 $code=$_POST["code"];
 6 $name=$_POST["name"];
 7 $kemu=$_POST["kemu"];
 8 $shijian=$_POST["shijian"];
 9 $shuliang=$_POST["shuliang"];
10 $fenzhi=$_POST["fenzhi"];
11 
12 $sql="insert into shiti values('{$code}','{$shijian}','{$kemu}','{$name}','{$shuliang}','{$fenzhi}')";
13 $db->Query($sql,1);
14 
15 //往试题题目表添加具体的题目内容
16 //根据科目和数量随机生成
17     //查科目
18 $all="select code from timu where kemu='{$kemu}'";
19 $aall=$db->Query($all);//里面包含本科目所有题目代号
20 
21 $attr=array();//存放生成的试题代号
22 //根据数量来取
23 for($i=0;$i<$shuliang;$i++)
24 {
25     //随机取代号
26     $n=rand(0,count($aall)-1);
27     $daihao=$aall[$n][0];
28     //判断取出的代号是否重复
29     if(in_array($daihao,$attr))
30     {
31         $i--;//若什么也不添加,循环出的数量会减少,所以若代号重复是,$i减少一次,再加一个
32     }
33     else
34     {
35     //$aall[$n][0];
36         array_push($attr,$daihao);
37     }
38 }
39 
40 
41   //往shititimu(试题题目)表添加数据
42   //code(自增长), shiti(试题代号)=$code, timu=$v, shunxu=$k
43 
44 foreach($attr as $k=>$v)
45 {
46     $shunxu=$k+1;
47     $stm="insert into shititimu values('','{$code}','{$v}','{$shunxu}')";
48     $db->Query($stm,1);
49     }
50 header("location:ksset.php");
View Code

注意:若该科目的试题数量少于输入的试题数量时,要提醒试题数量不足(如图)

 

使用ajax来进行处理,处理页面:bijiaosl.php

 1 <?php
 2 
 3 include("../../DBDA.class.php");
 4 $db=new DBDA();
 5 
 6 $km=$_POST["km"];
 7 $sl=$_POST["sl"];
 8 
 9 $sql="select count(*) from timu where kemu='{$km}'";
10 $zs = $db->StrQuery($sql);
11 
12 if($zs>=$sl)
13 {
14     echo "OK";
15     }
16 else
17 {
18     echo "NO";
19     }
20     
View Code

 

形成试卷页面:

形成试卷页面后台:shijuan.php

 1 <?php
 2 include("../init.inc.php");
 3 
 4 include("../../DBDA.class.php");
 5 $db=new DBDA();
 6 
 7 $sql="select * from shiti";
 8 $attr=$db->Query($sql);
 9 
10 $shuzu=array();//存储题目信息
11 //判断考试名称的值是否为空
12 if(empty($_POST["ksmc"]))
13 {
14     
15 }
16 else
17 {
18     //取出试题的代号
19         $code=$_POST["ksmc"];
20         $stm="select * from shititimu where shiti='{$code}'";
21         $atm=$db->Query($stm);
22         
23         //根据代号去查找相应的题目及选项
24         foreach($atm as $v)
25         {
26             //题目代号
27             $tmdh=$v[2];
28             //题目顺序
29             $tmsx=$v[3];
30             
31             //从题目表中取出试题的题目
32             $sname="select name from timu where code='{$tmdh}'";            
33             $aname=$db->StrQuery($sname);
34             
35             //从选项表中取出相应的选项
36             $sxx="select name,xuanxiang from xuanxiang where timu='{$tmdh}'";
37             $axx=$db->Query($sxx);
38             
39             $timuall=array("tmsx"=>$tmsx,"tmmc"=>$aname,"tmxx"=>$axx);
40             $shuzu[]=$timuall;
41             
42             }
43         
44 }
45 
46 $smarty->assign("shiti",$attr);
47 $smarty->assign("timu",$shuzu);
48 $smarty->display("shijuan.html");
View Code

形成试卷页面前端:shijuan.html

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <title>试卷页面</title>
 6 </head>
 7 
 8 <body>
 9 <h1>试题详情</h1>
10 <form action="shijuan.php" method="post">
11 <div>请选择考试名称:
12 <select name="ksmc">
13 <{foreach $shiti as $v}>
14 <option value="<{$v[0]}>"><{$v[3]}></option>
15 <{/foreach}>
16 </select>
17 <input type="submit" value="查询" />
18 </div>
19 </form><br />
20 
21 <div>
22 <{foreach $timu as $v}>
23 
24 <div><{$v.tmsx}>、<{$v.tmmc}>(&nbsp;&nbsp;&nbsp;)</div>
25     <{foreach $v.tmxx as $v1}>
26     <div><{$v1[1]}>:<{$v1[0]}></div>
27     <{/foreach}><br />
28 <{/foreach}>
29 
30 </div>
31 
32 </body>
33 </html>
View Code

 

posted @ 2016-06-06 17:01  陌上初薰  阅读(139)  评论(0编辑  收藏