我是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;">
<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;">
<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;">
<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;">
<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;">
<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;">
<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;">
<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;">
<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>
答案 0 :(得分:1)
使用
时,您正确地订购了查询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;">
<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;">
<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;">
<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;">
<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 ?>