如何按顺序显示结果?

时间:2018-01-06 10:53:58

标签: php mysql mysqli

我正在尝试为测验设计一个表单。我在数据库中有15个问题。我想通过改组向用户显示10个问题,但我无法顺序显示问题。

我的PHP代码是:

<form action="includes/backend_quiz.php" method="post">
    <?php
        $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT 10") or die("Could not retrieve data: " .mysqli_error($conn));

            if (mysqli_num_rows($query) > 0){           
                while($row = mysqli_fetch_assoc($query)){               
                    extract($row);  

                    ?>
                        <input type="hidden" name="id" value="<?php echo $id ?>" />
                        <h4><?php echo $id ?>.&nbsp  <?php echo $quation; ?></h4><br />

                        <input type="radio" name="response" value="a" /> <?php echo $opt1;?><br />
                        <input type="radio" name="response" value="b" /> <?php echo $opt2;?><br />
                        <input type="radio" name="response" value="c" /> <?php echo $opt3;?><br />
                        <input type="radio" name="response" value="d" /> <?php echo $opt4;?><br />
                        <br />
                    <?php

            }
        }       
    ?>
    <input type="submit" name="ans" value="Finish"/>
</form>

输出:

enter image description here

那么,如何按顺序显示问题,如Q1,Q2,Q3等?

1 个答案:

答案 0 :(得分:0)

试试这个

<form action="includes/backend_quiz.php" method="post">
<?php
    $query=mysqli_query($conn, "SELECT * FROM quiz order by rand() LIMIT 10") or die("Could not retrieve data: " .mysqli_error($conn));
        if (mysqli_num_rows($query) > 0){
            $number = 0;           
            while($row = mysqli_fetch_assoc($query)){               
                extract($row);  
                $number++;
                ?>
                    <input type="hidden" name="id" value="<?php echo $id ?>" />
                    <h4><?php echo $number?>.&nbsp  <?php echo $quation; ?></h4><br />

                    <input type="radio" name="response" value="a" /> <?php echo $opt1;?><br />
                    <input type="radio" name="response" value="b" /> <?php echo $opt2;?><br />
                    <input type="radio" name="response" value="c" /> <?php echo $opt3;?><br />
                    <input type="radio" name="response" value="d" /> <?php echo $opt4;?><br />
                    <br />
                <?php

        }
    }       
?>
<input type="submit" name="ans" value="Finish"/>