<?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>
在我的MySQL表中,我已存储;
id question answer1 answer2 answer3 answer4 ans_key date
1 1+1= 2 3 1 6 1 2014-08-03
这里是 ans_key ;
1 =answer1
2 =answer2
3 =answer3
4 =answer4
我正在使用Ajax发送用户的答案。
我想用相关值检查答案键, 在数组shuffle之后,如何获取键值以检查答案是否正确?
答案 0 :(得分:1)
我找到了答案
<?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
}
?>
这里data-key保存数组的键值