php测验中的随机问题,不重复问题

时间:2018-03-20 10:48:26

标签: 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($result);
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." Order by rand() ");
$r=mysql_fetch_array($result);

                  1.>                   2.>                   3.>                   4.>                    使用此代码,问题以随机的方式出现,但实际问题是如何从上面的代码中重复解决这个重复问题的问题

-

- 表question

的表结构

CREATE TABLE IF NOT NOT EXISTS question(   testid bigint(20)NOT NULL DEFAULT'0',   qnid int(11)NOT NULL DEFAULT'0',   question varchar(500)DEFAULT NULL,   optiona varchar(100)DEFAULT NULL,   optionb varchar(100)DEFAULT NULL,   optionc varchar(100)DEFAULT NULL,   optiond varchar(100)DEFAULT NULL,   correctanswer枚举('optiona','optionb','optionc','optiond')DEFAULT NULL,   marks int(11)DEFAULT NULL,   PRIMARY KEY(testidqnid) )ENGINE = InnoDB DEFAULT CHARSET = latin1;

-

- 转储表question

的数据

DROP TABLE IF EXISTS studentquestion; / *!40101 SET @saved_cs_client = @@ character_set_client /; / !40101 SET character_set_client = utf8 /; CREATE TABLE studentquestion(   stdid bigint(20)NOT NULL DEFAULT'0',   testid bigint(20)NOT NULL DEFAULT'0',   qnid int(11)NOT NULL DEFAULT'0',   answered枚举('已回答','未接听','审核')DEFAULT NULL,   stdanswer枚举('optiona','optionb','optionc','optiond')DEFAULT NULL,   PRIMARY KEY(stdidtestidqnid),   KEY testidtestidqnid),   约束studentquestion_ibfk_1外键(stdid)参考studentstdid),   约束studentquestion_ibfk_2外键(testidqnid)参考questiontestidqnid) )ENGINE = InnoDB DEFAULT CHARSET = latin1; / !40101 SET character_set_client = @saved_cs_client * /;

-

- 转储表studentquestion

的数据

LOCK TABLES studentquestion WRITE; / *!40000 ALTER TABLE studentquestion DISABLE KEYS /; / !40000 ALTER TABLE studentquestion ENABLE KEYS * /; 解锁表格;

1 个答案:

答案 0 :(得分:-1)

// 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);