在php中选择答案时,如何显示“对”或“错”?

时间:2019-07-11 13:27:19

标签: php html css logic

当某人选择一个单选按钮时,我想显示是对还是错。

不知道该怎么做。这是我到目前为止所做的。我已经建立了数据库来存储问题,选项和正确答案。我已经完成分页以在一个页面上显示一个问题,然后用户可以使用NEXT按钮转到下一个问题。

<?php
if (isset($_GET['pageno'])){
    $pageno = $_GET['pageno'];
} else {
    $pageno = 1;
}

$no_of_records_per_page = 1;
$offset = ($pageno - 1) * $no_of_records_per_page;
$conn = mysqli_connect("localhost", "root", "", "quizdb");

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    die();
}

$total_pages_sql = "SELECT COUNT(*) FROM entry1";
$result = mysqli_query($conn, $total_pages_sql);
$total_rows = mysqli_fetch_array($result)[0];
$total_pages = ceil($total_rows / $no_of_records_per_page);

$sql = "SELECT * FROM entry1 LIMIT $offset, $no_of_records_per_page";
$res_data = mysqli_query($conn, $sql);

while ($row = mysqli_fetch_array($res_data)) {
    include('server1.php');
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'quizdb';

    $con = mysqli_connect($host, $user, $pass, $db);
    $qno = $row["qno"];
    $question = $row["question"];
    $optiona = $row["optiona"];
    $optionb = $row["optionb"];
    $optionc = $row["optionc"];
    $optiond = $row["optiond"];
    $ans = $row["ans"];

    echo '<br>Q ' . $qno . '. ';
    echo '' . $question . '<br>';
    echo '<input type="radio" name="options" value="optiona"></button>' . $optiona . '<br>';
    echo '<input type="radio" name="options" value="optionb"></button>' . $optionb . '<br>';
    echo '<input type="radio" name="options" value="optionc"></button>' . $optionc . '<br>';
    echo '<input type="radio" name="options" value="optiond"></button>' . $optiond . '<br>';
    echo '<form action="practice.php" method="post">
    <input type="submit" name="submitans" value="Check Answer"></form>';
           if (isset($_POST['submitans'])) {
               echo '' . $ans . '<br>';
           }
}
mysqli_close($conn);
?>
<ul class="pagination">

    <li><a href="?pageno=1">First</a></li>

    <li class="<?php if ($pageno <= 1) {
        echo 'disabled';
    } ?>">

        <a href="<?php if ($pageno <= 1) {
            echo '#';
        } else {
            echo "?pageno=" . ($pageno - 1);
        } ?>">Prev</a></li>

    <li class="<?php if ($pageno >= $total_pages) {
        echo 'disabled';
    } ?>">

        <a href="<?php if ($pageno >= $total_pages) {
            echo '#';
        } 

    else {
        echo "?pageno=" . ($pageno + 1);
    } ?>">Next</a>
   </li>
   <li><a href="?pageno=<?php echo $total_pages; ?>">Last</a></li>
</ul>
</body>
</html>

期望:当某人选择一个选项时显示对或错答案。

0 个答案:

没有答案