php 選擇題問題

不好意思,我請問一下,選項不一定是四個選項,有兩到三選項,而且是亂數的,如果是四個選項會顯示全部選項,其他兩到三選項,會出現空白選項,要該怎麼解決?
<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\">
<title>Answer Game</title>
<script type=\"text/javascript\">
function chkdata(){
if(document.form1.id.value.null)
alert(\"選項不得空白!!\");
}
</script>

</head>
<body style=\"background-image:url(images.jpg);background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
background-size: cover;\">
<div style=\"
width:35%;
height:530px;
position: absolute;
top: 50%;
left: 50%;
margin: -220px 0 0 -17.5%;\">
<body>
<?php
$opt=array(\"A\",\"B\",\"C\",\"D\");
$dbhost = \"localhost\";
$dbuser = \"wk\";
$dbpass = \"jod93\";
$dbname = \"wk\";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_query(\"SET NAMES 'utf8'\");
mysql_select_db($dbname);
$sql = \"select * from test ORDER BY RAND() \";//資料庫
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)){
$qid = $row[\"qid\"]; //題號
$res = $row[\"res\"].\"<br>\";//答案
$ques = $row[\"question\"].\"<br>\";//題目
$ans = array($row[\"ans1\"], $row[\"ans2\"], $row[\"ans3\"], $row[\"ans4\"]);

$ans1 = $row[\"ans1\"].\"<br>\"; //選項1
$ans2 = $row[\"ans2\"].\"<br>\"; //選項2
$ans3 = $row[\"ans3\"].\"<br>\"; //選項3
$ans4 = $row[\"ans4\"].\"<br>\"; //選項4
//echo gettype($ans) . \"<br>\";
}

srand((double)microtime()*1000000);
$a=mt_rand(0,3);
echo $ques;
echo \"<form method='post' action='show.php'>\";
for ($i=0; $i<4; $i++){
$idx=$a+$i;
if($idx>=4){
$idx=$idx-4;
}

if(isset($ans[$i])&& ($ans[$i])){
echo '<input type=\"Radio\" id=\"form1\" name=\"ans\" value=\"$opt[$i]\">';
echo $ans[$idx];
}else{
continue;
}


echo \"<br>\";
echo '<input type=\"submit\" name=\"submit\" value=\"submit\">';
echo '&nbsp';
echo '<input type=\"reset\" name=\"reset\" value=\"reset\">';

echo '</form>';
echo \"$res\";
//setcookie(\"res\",$res);

?>
</body>
</html>


Sign In or Register to comment.