如何在页面本身上显示条目的正确答案?

时间:2019-07-11 10:55:59

标签: php pagination

我正在测验。我显示了来自db的Q / A和正确的答案。单击“检查答案”按钮时如何查看特定问题的答案。

<?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 个答案:

没有答案