首页 文章

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

提问于
浏览
-3

这是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.>使用此代码,问题以随机方式出现,但实际问题是如何从上面的代码中重复解决这个重复问题的问题

- 表格问题的表结构

CREATE TABLE IF NOT NOT EXISTS questiontestid 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 enum('optiona','optionb','optionc','optiond')DEFAULT NULL, marks int(11)DEFAULT NULL,PRIMARY KEY( testidqnid ))ENGINE = InnoDB DEFAULT CHARSET = latin1;

- 转储表格问题的数据

DROP TABLE IF EXISTS studentquestion ; / *!40101 SET @saved_cs_client = @@ character_set_client /; /!40101 SET character_set_client = utf8 /; CREATE TABLE studentquestionstdid bigint(20)NOT NULL DEFAULT '0', testid bigint(20)NOT NULL DEFAULT '0', qnid int(11)NOT NULL DEFAULT '0', answered enum('answered','unanswered','review')DEFAULT NULL, stdanswer enum('optiona' ,'optionb','optionc','optiond')DEFAULT NULL,PRIMARY KEY( stdidtestidqnid ),KEY testidtestidqnid ),CONSTRAINT studentquestion_ibfk_1 FOREIGN KEY( stdid )REFERENCES studentstdid ),CONSTRAINT studentquestion_ibfk_2 FOREIGN KEY( 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 回答

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

相关问题