2017-06-07 53 views
1

我已經將數據靜態存儲在數據庫中。它有5個問題和4個問題。尋求答案。基本上它是一個測驗遊戲格式。現在我想以4列的單選按鈕的形式從數據庫中獲取數據。如何實現該方法。目前我正在以正常文本模式提取。在PHP中以問答遊戲的單選按鈕格式提取數據

下面是代碼

<?php 

// Create connection 
$conn = new mysqli("localhost","root","","QuizQuestions"); 

// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 
echo "Connected successfully<br><br>"; 

$sql = "SELECT Question, Answer1, Answer2, Answer3, Answer4 FROM Questions"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 
    echo "<br>Question: " . $row["Question"]. "<br>"; 
    echo " A) " . $row["Answer1"]. "<br>"; 
    echo " B) " . $row["Answer2"]. "<br>"; 
    echo " C) " . $row["Answer3"]. "<br>"; 
    echo " D) " . $row["Answer4"]. "<br>"; 
} 
} else { 
echo "0 results"; 
} 
$conn->close(); 

?> 

回答

0

如果我正確理解你的問題,你只需要打的答案爲單選按鈕。你可以做到這一點:

while($row = $result->fetch_assoc()) { 
    echo "<br>Question: " . $row["Question"]. "<br>"; 
    echo ' A) <input type="radio" value="'.$row["Answer1"].'">'.$row["Answer1"].'<br>'; 
    echo ' B) <input type="radio" value="'.$row["Answer2"].'">'.$row["Answer2"].'<br>'; 
    echo ' C) <input type="radio" value="'.$row["Answer3"].'">'.$row["Answer3"].'<br>'; 
    echo ' D) <input type="radio" value="'.$row["Answer4"].'">'.$row["Answer4"].'<br>'; 
} 
+0

它顯示在下拉列表的形式不是單選按鈕 – LazyLearner

+0

噢,對不起! mybad 2秒我會更新它 –

+0

thanku它的工作,但有一個問題..問題是我提取超過1個問題(一次5問題),所以它只從5個問題中選擇一個 – LazyLearner

0

如果我正確地理解你的問題。
如果你的數據庫字段包含HTML代碼的單選按鈕,然後ü需要:

使用htmlspecialchars_decode

stripslashes

替換以下行..

echo " A) " . $row["Answer1"]."<br>"; 

隨着

echo htmlspecialchars_decode(stripslashes($row["Answer1"])); 

否則使用簡單的PHP代碼來顯示前端

<input type='radio' name="ans" value="1" > <?php echo $row["Answer1"]; ?> 
<input type='radio' name="ans" value="2" > <?php echo $row["Answer2"]; ?> 

<input type='radio' name="ans" value="3" > <?php echo $row["Answer3"]; ?> 
<input type='radio' name="ans" value="4" > <?php echo $row["Answer4"]; ?> 
該校驗值後

和後

0

驗證ANS爲什麼你不嘗試比無線電選擇?但在這裏就是你要找什麼,如果我沒有記錯

<input type="radio" name="'.$row["Answer1"].'" value="'.$row["Answer1"].'" /> 
then just validate in your javascript if the radio button has a value make it checked.