gpt4 book ai didi

php - 在每个 div 选项卡中显示一个随机测试问题

转载 作者:行者123 更新时间:2023-11-29 10:29:50 25 4
gpt4 key购买 nike

我是 php 新手,正在从事技能测试项目。我的目标是在每个 div 选项卡中显示 mysql 数据库中的一个随机问题,而不重复问题。我当前的问题是每个选项卡中都显示相同的问题。我希望有人能帮助我解决这个问题。非常感谢!

这是我的代码:

         <?php   
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "palo";
$conn= mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection failed: ". mysqli_connect_error());
}
$sql = "SELECT test_id, question, optiona, optionb, optionc, optiond FROM tquestions ORDER BY RAND() LIMIT 1";
$result = mysqli_query ($conn, $sql); // Run the query
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
?>

<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
document.getElementById("defaultOpen").click();
</script>

<div class="tab">
<button class="tablinks" onclick="openTab(event, 'q1')" id="defaultOpen">1</button>
<button class="tablinks" onclick="openTab(event, 'q2')">2</button>
<button class="tablinks" onclick="openTab(event, 'q3')">3</button>
</div>

<?php if (!empty($_SESSION['msg'])) { ?>
<div class="alert alert-info"><?php echo $_SESSION['msg']; ?></div>
<?php } ?>

<div id="q1" class="tabcontent">
<table class="table table-hover">
<tbody>
<?php if (mysqli_num_rows($result) > 0): ?>
<?php foreach ($result as $results):?>
<tr class="form-group">
<h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $results['question']; ?> </h3>
</tr>
<tr class="form-group">
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiona'];?>"><?php echo $results['optiona'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionb'];?>"><?php echo $results['optionb'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionc'];?>"><?php echo $results['optiond'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" ame="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiond'];?>"><?php echo $results['optiond'];?>
</label>
<br>
</tr>
<?php endforeach ?>
<?php endif ?>
</tbody>
</table>
</div>

<div id="q2" class="tabcontent">
<table class="table table-hover">
<tbody>
<?php if (mysqli_num_rows($result) > 0): ?>
<?php foreach ($result as $results):?>
<tr class="form-group">
<h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $results['question']; ?> </h3>
</tr>
<tr class="form-group">
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiona'];?>"><?php echo $results['optiona'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionb'];?>"><?php echo $results['optionb'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionc'];?>"><?php echo $results['optiond'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" ame="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiond'];?>"><?php echo $results['optiond'];?>
</label>
<br>
</tr>
<?php endforeach ?>
<?php endif ?>
</tbody>
</table>
</div>

最佳答案

当您使用时,您对查询的排序是正确的

ORDER BY RAND()

但是你需要根据你想要达到的总数来限制结果,例如

limit 0, 2

即使您使用的是 foreach,您也使用相同的记录。您需要迭代记录,如下所示:

     <?php   
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "palo";
$conn= mysqli_connect($servername, $username, $password, $dbname);
if(!$conn){
die("Connection failed: ". mysqli_connect_error());
}
$sql = "SELECT test_id, question, optiona, optionb, optionc, optiond FROM tquestions ORDER BY RAND() LIMIT 1";
$result = mysqli_query ($conn, $sql); // Run the query
if (!$result) {
printf("Error: %s\n", mysqli_error($conn));
exit();
}
?>

<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
document.getElementById("defaultOpen").click();
</script>

<div class="tab">
<button class="tablinks" onclick="openTab(event, 'q1')" id="defaultOpen">1</button>
<button class="tablinks" onclick="openTab(event, 'q2')">2</button>
<button class="tablinks" onclick="openTab(event, 'q3')">3</button>
</div>

<?php if (!empty($_SESSION['msg'])) { ?>
<div class="alert alert-info"><?php echo $_SESSION['msg']; ?></div>
<?php } ?>

<?php if (mysqli_num_rows($result) > 0): ?>
<?php $index = 1; ?>
<?php foreach ($result as $results):?>
<div id="q<?php echo ($index++); ?>" class="tabcontent">
<table class="table table-hover">
<tbody>
<tr class="form-group">
<h3 name="ques[<?php echo $test_id;?>]" style="text-indent: 40px;"> <?php echo $results['question']; ?> </h3>
</tr>
<tr class="form-group">
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiona'];?>"><?php echo $results['optiona'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionb'];?>"><?php echo $results['optionb'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" name="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optionc'];?>"><?php echo $results['optiond'];?>
</label>
<br>
<label class="radio-inline" style="text-indent: 70px; font-size: 18px;">
&nbsp;&nbsp;<input style="font-size: 18px;" type="radio" ame="ans[<?php echo $results['test_id'];?>" value="<?php echo $results['optiond'];?>"><?php echo $results['optiond'];?>
</label>
<br>
</tr>
</tbody>
</table>
</div>
<?php endforeach ?>
<?php endif ?>

关于php - 在每个 div 选项卡中显示一个随机测试问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47608450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com