试题表question:qid(编号)/chapter(章节)/question(题目)/option1(选项A)/option2(选项B)/option3(选项C)/key(答案)
成绩表score:mid(成绩编号)/sid(学生编号)/(成绩)/time(考试时间)<?php
mysql_connect("localhost","root","123456");
mysql_select_db("exam");define("NUM",2);//定义随机取题的数量,为方便测试暂时取2
$id=array();//定义一个数组$id,用它接收符合指定条件的试题id
$query=mysql_query("select qid from question where chapter='$_GET[chapter]'");//接受HTTP传递过来的参数
while($row=mysql_fetch_row($query)){
$id[]=$row[0];//用数组$id接收符合条件的所有试题id
}
$rand_id=array_rand($id,NUM);//利用函数array_rand()在数组$id中随机获取NUM道试题的id号,并赋给数组
for($i=0;$i<count($rand_id);$i++){
$exam_id=$input[$rand_id[$i]];//用$exam_id存放要输出到页面的试题id
}
?>
/----------------------------print.php---------------------------------/
/-----------把随机获取的NUM道试题在页面上输出---------------------------/<?php
mysql_connect("localhost","root","123456");
mysql_select_db("exam");
for($i=0;$i<NUM;$i++){
$j=$i+1;
$query=mysql_query("select question,option1,option2,option3 from question where qid='$exam_id[$i;
$rs=mysql_fetch_array($query,MYSQL_ASSOC);//得到一个关联索引的数组
?>
<div align="center">
  <table>
    <tr>
      <td>题目<?php echo $j".".$rs[question]?>( )</td>
    </tr>
    <tr>
      <td>A. <?php echo $rs[option1]?></td>
    </tr>
    <tr>
      <td>B. <?php echo $rs[option2]?></td>
    </tr>
        <tr>
      <td>C. <?php echo $rs[option3]?></td>
    </tr>
  </table>
</div>
<?php
}//结束循环
?>可能错误比较多,希望大侠HELP喔!这个没法显示题目和选项呀,新手看晕了。
另外要怎么去做评分,并把分数写入成绩表呢?怎么判断能让数据库的压力最小化?....请大家批评指正,给“小菜”多些指导啊!!!!!叩谢~~~~~急救120!SOS啦~~~~~~

解决方案 »

  1.   

    不是同一页面,我想把获取ID的封装成一函数,以后要获取ID时直接调用函数,函数的参数就是$chapter(第几章) 
      

  2.   

    1.这个没法显示题目和选项呀,新手看晕了
    ——————————————————————————]估计你的获取题目ID面页和显示题目面页不是同一个文件。
    rand.phpsession_start();
    mysql_connect("localhost","root","123456");
    mysql_select_db("exam");define("NUM",2);//定义随机取题的数量,为方便测试暂时取2
    $id=array();//定义一个数组$id,用它接收符合指定条件的试题id
    $query=mysql_query("select qid from question where chapter='$_GET[chapter]'");//接受HTTP传递过来的参数
    while($row=mysql_fetch_row($query)){
        $id[]=$row[0];//用数组$id接收符合条件的所有试题id
    }
    $rand_id=array_rand($id,NUM);//利用函数array_rand()在数组$id中随机获取NUM道试题的id号,并赋给数组
    for($i=0;$i<count($rand_id);$i++){
        $exam_id=$input[$rand_id[$i]];//用$exam_id存放要输出到页面的试题id
    }
    $_SESSION['exam_id']=$exam_idprint.phpsession_start();
    mysql_connect("localhost","root","123456");
    mysql_select_db("exam");
    $exam_id=$_SESSION['exam_id'];
    for($i=0;$i<NUM;$i++){
    $j=$i+1;
    $query=mysql_query("select question,option1,option2,option3 from question where qid='$exam_id[$i;
    $rs=mysql_fetch_array($query,MYSQL_ASSOC);//得到一个关联索引的数组
    ?>
    <div align="center">
      <table>
        <tr>
          <td>题目<?php echo $j".".$rs[question]?>( )</td>
        </tr>
        <tr>
          <td>A. <?php echo $rs[option1]?></td>
        </tr>
        <tr>
          <td>B. <?php echo $rs[option2]?></td>
        </tr>
            <tr>
          <td>C. <?php echo $rs[option3]?></td>
        </tr>
      </table>
    </div>
    <?php
    }//结束循环2.另外要怎么去做评分,并把分数写入成绩表呢?怎么判断能让数据库的压力最小化?
    --------------------------------------------------------------------------
    加个表单提交数据给一个面页做相关处理!
      

  3.   

    下班了,回家研究下,session.....
      

  4.   

    为什么print_r($id);的时候只有一个值,而不是三个?<?php
    //srand((float) microtime() * 10000000);
    $input = array("1", "2", "3", "4", "5");
    $rand_keys = array_rand($input, 3);
    print $input[$rand_keys[0]] . "\n";
    print $input[$rand_keys[1]] . "\n";
    print $input[$rand_keys[2]] . "\n";
    for($i=0;$i<2;$i++){
    $id=$input[$rand_keys[$i]];
    }
    print_r($id);?>
      

  5.   

    $id[] = $input[$rand_keys[$i]];
      

  6.   

    一直不明白为什么会出现值为0??????<?php
    $input = array("1", "2", "3", "4", "5");
    $rand_keys = array_rand($input, 3);
    print_r($rand_keys);
    ?>
      

  7.   

    array_rand返回的是数组键名数组,不是数组值,仔细看手册.