我的 php 代码如下: 这是测验的一部分,我通过 ajax jQuery 在 html 页面中显示一个问题和 4 个多项选择。我知道如何运行 while 循环并一个接一个地显示所有数据,但我如何一次只显示一个问题?
因此,在回答了一个问题后,我想查看下一个问题。是否可以运行一个计数器并一次提取一个结果和下一个结果等等..?
<?php
header("Access-Control-Allow-Origin: *");
require 'db.php';
// making empty variable
$createTable = "";
$test_id=$_POST["test_id"];
$sql=mysqli_query($con,"select * from mst_question where test_id='$test_id' ");
$counter = 0;
while($row=mysqli_fetch_array($sql))
{
$counter++;
$createTable .= '<div class="text-subhead-2 text-center" style="background-color:#42A5F5">Question ';
$createTable .= $counter;
$createTable .= ' of 25</div>';
$createTable .= '<div class="panel panel-default paper-shadow" data-z="0.5">';
$createTable .= '<div class="panel-body">';
$createTable .= '<p class="text-body-2">';
$createTable .= $row['que_desc'];
$createTable .= '</p>';
$createTable .= '</div>';
$createTable .= '</div>';
$createTable .= '<div class="text-subhead-2 text-light">Your Answer</div>';
$createTable .= '<div class="panel panel-default paper-shadow" data-z="0.5">';
$createTable .= '<div class="panel-body">';
$createTable .= '<div class="radio radio-success">';
$createTable .= '<input type="radio" name="radio';
$createTable .= $counter;
$createTable .= '" id="radio1';
$createTable .= $counter;
$createTable .= '" value="';
$createTable .= $row['ans1'];
$createTable .= '" >';
$createTable .= '<label for="radio1';
$createTable .= $counter;
$createTable .= '">';
$createTable .= $row['ans1'];
$createTable .= '</label>';
$createTable .= '</div>';
$createTable .= '<div class="radio radio-success">';
$createTable .= '<input type="radio" name="radio';
$createTable .= $counter;
$createTable .= '" id="radio2';
$createTable .= $counter;
$createTable .= '" value="';
$createTable .= $row['ans2'];
$createTable .= '" >';
$createTable .= '<label for="radio2';
$createTable .= $counter;
$createTable .= '">';
$createTable .= $row['ans2'];
$createTable .= '</label>';
$createTable .= '</div>';
$createTable .= '<div class="radio radio-success">';
$createTable .= '<input type="radio" name="radio';
$createTable .= $counter;
$createTable .= '" id="radio3';
$createTable .= $counter;
$createTable .= '" value="';
$createTable .= $row['ans3'];
$createTable .= '" >';
$createTable .= '<label for="radio3';
$createTable .= $counter;
$createTable .= '">';
$createTable .= $row['ans3'];
$createTable .= '</label>';
$createTable .= '</div>';
$createTable .= '<div class="radio radio-success">';
$createTable .= '<input type="radio" name="radio';
$createTable .= $counter;
$createTable .= '" id="radio4';
$createTable .= $counter;
$createTable .= '" value="';
$createTable .= $row['ans4'];
$createTable .= '" >';
$createTable .= '<label for="radio4';
$createTable .= $counter;
$createTable .= '">';
$createTable .= $row['ans4'];
$createTable .= '</label>';
$createTable .= '</div>';
$createTable .= '</div>';
$createTable .= '</div>';
}
echo $createTable;
mysqli_close($con);
?>