简体   繁体   English

在PHP中以单选按钮格式获取数据以进行问答游戏

[英]Fetching data in form of radio button format for quiz game in PHP

I have stored data in database statically. 我已将数据静态存储在数据库中。 Its having 5 col One for question and 4 col. 它有5列问题之一和4列问题。 for answers. 寻找答案。 Basically its a quiz game format. 基本上是问答游戏的格式。 Now I want to fetch data from database in the form of radio button for that 4 col. 现在,我想以那四个列的单选按钮的形式从数据库中获取数据。 How to implement that method. 如何实现该方法。 Currently i am fetching in normal text mode. 目前,我正在以普通文本模式获取。

Here is the code 这是代码

 <?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();

?>

If I understand your question correctly, you simply want to make the answers into radio buttons. 如果我正确理解了您的问题,则只想将答案放入单选按钮即可。 You can achieve this by doing this: 您可以通过执行以下操作来实现:

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>';
}

if i understood your question correctly. 如果我正确理解您的问题。
and if your database field contain html code for radio button then u need to: 如果您的数据库字段包含单选按钮的html代码,则您需要:

Use htmlspecialchars_decode 使用htmlspecialchars_decode

and stripslashes 反斜杠

Replace the following line.. 替换以下行。

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

With

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

else use simple php code to show front end 否则使用简单的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"]; ?>

after that check value and validate ans on post 在该检查值之后并在发布后验证ans

why you dont try select than radio? 为什么你不尝试选择比收音机? but here's what you're looking for if i'm not mistaken 但是如果我没记错的话,这就是你要寻找的东西

<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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM