douju6542 2014-08-03 17:43
浏览 25

数组shuffle与测验php的关键

<?php
$qq=mysql_query("select * from quiz");
$row=mysql_fetch_array($qq);
//var_dump($row);
    $question = $row['question'];
    $_SESSION['quiz_id']=$row['id'];
    $ans_array = array($row['answer1'],$row['answer2'],$row['answer3'],$row['answer4']);
    shuffle($ans_array);
?>
<form name="qform" id="qform">
 <h2> <?php echo $question; ?></h2>   
 <label><input type="radio" value="<?php echo $ans_array[0]; ?>" name="answer"> <?php echo $ans_array[0]; ?></label>
 <label><input type="radio" value="<?php echo $ans_array[1]; ?>" name="answer"> <?php echo $ans_array[1]; ?></label>
 <label><input type="radio" value="<?php echo $ans_array[2]; ?>" name="answer"> <?php echo $ans_array[2]; ?></label>
 <label><input type="radio" value="<?php echo $ans_array[3]; ?>" name="answer"> <?php echo $ans_array[3]; ?></label>
    <p id="submit-row-btn"><input type="submit" id="quiz_submit" class="q_submit" value="Submit">
    <input type="submit" id="quiz_ans_submit" value="Check Answer" class="q_submit" style="display:none">
    <span id="ans_err"></span></p>
 </form>

In my MySQL table, I have stored;

id   question   answer1     answer2     answer3     answer4     ans_key     date
1     1+1=        2           3           1           6           1     2014-08-03 

here is ans_key;

1 =answer1
2 =answer2
3 =answer3
4 =answer4

I am using Ajax to send user's answer.

I want to check the answer key with the relevant value, after array shuffle, how to get the key value to check if the answer is right or not?

  • 写回答

1条回答 默认 最新

  • douzhuangna6906 2014-08-03 18:03
    关注

    i found the answer

    <?php
    $ans_array = array(1=>$row['answer1'],$row['answer2'],$row['answer3'],$row['answer4']);
    $shuffleKeys = array_keys($ans_array);
    shuffle($shuffleKeys);
    $newArray = array();
        foreach($shuffleKeys as $key) 
        {
            $newArray[$key] = $ans_array[$key];
        ?>
            <label><input type="radio" value="<?php echo $newArray[$key] ?>" data-key="<?php echo $key;?>" name="answer"> <?php echo $newArray[$key]; ?></label>
        <?php
        }
        ?>
    

    here data-key holds the key value of array

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线