php测验中的随机问题,无重复问题
这是php代码php测验中的随机问题,无重复问题,php,Php,这是php代码 if(isset($_SESSION['stdname'])) { $result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";"); $r1=mysql_fetch_array($res
if(isset($_SESSION['stdname']))
{
$result=executeQuery("select stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid']." and qnid=".$_SESSION['qn'].";");
$r1=mysql_fetch_array($result);
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." Order by rand() ");
$r=mysql_fetch_array($result);
1. >
2. >
3. >
4. >
有了这段代码,问题是以随机的方式出现的,但实际的问题是问题不断重复,如何从上面的代码中解决这个重复的问题
--
--表问题的表结构
如果不存在,则创建表格问题
(
testid
bigint(20)非空默认值“0”,
qnid
int(11)非空默认值“0”,
question
varchar(500)默认为空,
optiona
varchar(100)默认为空,
optionb
varchar(100)默认为空,
optionc
varchar(100)默认为空,
optiond
varchar(100)默认为空,
correctanswer
enum('optiona'、'optionb'、'optionc'、'optiond')默认为空,
标记
int(11)默认为空,
主键(testid
,qnid
)
)ENGINE=InnoDB默认字符集=1
--
--正在转储表问题的数据
如果存在,则删除表格studentquestion
;
/*!40101 SET@saved_cs_client=@@character_SET_client/;
/!40101集合字符\集合\客户端=utf8/;
创建表格studentquestion
(
stdid
bigint(20)非空默认值“0”,
testid
bigint(20)非空默认值“0”,
qnid
int(11)非空默认值“0”,
answered
enum('answered'、'unanswered'、'review')默认为空,
stdanswer
enum('optiona','optionb','optionc','optiond')默认为空,
主键(stdid
,testid
,qnid
),
键testid
(testid
,qnid
),
约束studentquestion\u ibfk\u 1
外键(stdid
)引用student
(stdid
),
约束studentquestion\u ibfk\u 2
外键(testid
,qnid
)参考question
(testid
,qnid
)
)ENGINE=InnoDB默认字符集=1;
/!40101 SET character_SET_client=@saved_cs_client*/
--
--正在转储表的数据studentquestion
锁定表格studentquestion
WRITE;
/*!40000 ALTER TABLEstudentquestion
DISABLE key/;
/!40000 ALTER TABLEstudentquestion
ENABLE key*/;
解锁表格 使用上述代码,问题也会重复出现对不起。编辑了第一个查询。请根据以上编辑的第一个查询再次检查。我做了更改,但现在当测试开始时,问题及其选项不会显示。我进一步添加了代码。请再次查看我编辑的问题。是。你能不能也提供表格结构?
// select the questions id's already answered by the current student
$result=executeQuery("select qnid, stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid'].";");
$already_answered_qns = array();
while($r1=mysql_fetch_array($result))
{
array_push($already_answered_qns, $r1['qnid']);
}
// select a question excluding the already answered questions
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid not in (".implode(', ', $already_answered_qns).") Order by rand() ");
$r=mysql_fetch_array($result);