【发布时间】:2020-10-15 09:48:37
【问题描述】:
我有下面的代码。从本质上讲,它从数据库中提取数学问题并将它们布置给用户回答。有两个部分:mathcalc 和 mathnocalc。现在,程序随机输出它们。
有时可能有 15 个 mathcalc,而只有 5 个 mathnocalc。这对我来说是个问题。我需要能够指定程序只输出 10 个 mathcalc 和 10 个 mathnocalc。
此外,我希望能够拆分输出,以便第一页仅 mathcalc(它可以超过一页。我只需要打印出所有 mathnocalc 首先,然后打印出所有的数学计算。基本上是两组)。
我想知道如何将它与我现有的代码结合起来。这绝对是一个挑战,但我想知道是否有人能帮助我。
更新完整代码:
<?php
session_start();
$email = $_SESSION['email'];
if (empty($email)) {
echo "Not logged in";
}
else {
require '../functions/convert_functions.php';
$servername = "localhost";
$username = "root";
$password = "";
$database = "questionbank";
$answerkey = array();
$typelist = array();
$topiclist = array();
$skilllist = array();
$counter = 0;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
$queryNoCalc = "SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10";
$queryCalc = "SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathcalc' ORDER BY RAND() LIMIT 0,10";
$noCalcResult = $conn->query($queryNoCalc);
$CalcResult = $conn->query($queryCalc);
if (($noCalcResult->num_rows > 0) && ($noCalcResult->num_rows > 0)){
// output data of each row
echo '<form method="post">';
echo ' <div class="flex-container>';
echo '<h3>NON CALULATOR SECTION</h3>';
while ($row = mysqli_fetch_array($noCalcResult)){
echo "<div class='flex-child'>";
echo "<p style='font-size:0.50em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
$correctanswer = $row['correctanswer'];
$temptype = $row['type'];
$temptopic = $row['Topic'];
$tempskill = $row['Skill'];
array_push($answerkey, "$correctanswer");
array_push($typelist, "$temptype");
array_push($topiclist, "$temptopic");
array_push($skilllist, "$tempskill");
if (empty($row['imagename'])) {
}
else {
echo '
<tr>
<td>
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
</td>
</tr>
';
}
if ($row['type'] == 'mathnocalc') {
$typemessage = "No Calculator";
}
else {
$typemessage = "Calculator Allowed";
}
echo '
<div>
<input type="radio" id="answerA-' . $counter . '" name= "answer-' . $counter .'" value="answerA" required>
<label style="font-size:0.40em;" for="answerA-' . $counter . '"> ' . $row['answerA'] . ' </label><br>
<input type="radio" id="answerB-' . $counter . '" name="answer-' . $counter .'" value="answerB">
<label style="font-size:0.40em;" for="answerB-' . $counter . '"> ' . $row['answerB'] . ' </label><br>
<input type="radio" id="answerC-' . $counter . '" name="answer-' . $counter .'" value="answerC">
<label style="font-size:0.40em;" for="answerC-' . $counter . '"> ' . $row['answerC'] . ' </label><br>
<input type="radio" id="answerD-' . $counter . '" name="answer-' . $counter .'" value="answerD">
<label style="font-size:0.40em;" for="answerD-' . $counter . '"> ' . $row['answerD'] . ' </label><br>
<input type="hidden" name="id" value="' . $counter . '">
<p style="font-size:0.45em;"> ' . $typemessage . '</p>
</div>
';
echo'</div>';
$counter++;
} //while end
echo '</div>';
echo '<div class="flex-container">';
echo '<h3>CALULATOR SECTION</h3>';
while($row = mysqli_fetch_array($CalcResult)) {
echo "<div class='flex-child'>";
echo "<p style='font-size:0.50em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
$correctanswer = $row['correctanswer'];
$temptype = $row['type'];
$temptopic = $row['Topic'];
$tempskill = $row['Skill'];
array_push($answerkey, "$correctanswer");
array_push($typelist, "$temptype");
array_push($topiclist, "$temptopic");
array_push($skilllist, "$tempskill");
if (empty($row['imagename'])) {
}
else {
echo '
<tr>
<td>
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
</td>
</tr>
';
}
if ($row['type'] == 'mathnocalc') {
$typemessage = "No Calculator";
}
else {
$typemessage = "Calculator Allowed";
}
echo '
<div>
<input type="radio" id="answerA-' . $counter . '" name= "answer-' . $counter .'" value="answerA" required>
<label style="font-size:0.40em;" for="answerA-' . $counter . '"> ' . $row['answerA'] . ' </label><br>
<input type="radio" id="answerB-' . $counter . '" name="answer-' . $counter .'" value="answerB">
<label style="font-size:0.40em;" for="answerB-' . $counter . '"> ' . $row['answerB'] . ' </label><br>
<input type="radio" id="answerC-' . $counter . '" name="answer-' . $counter .'" value="answerC">
<label style="font-size:0.40em;" for="answerC-' . $counter . '"> ' . $row['answerC'] . ' </label><br>
<input type="radio" id="answerD-' . $counter . '" name="answer-' . $counter .'" value="answerD">
<label style="font-size:0.40em;" for="answerD-' . $counter . '"> ' . $row['answerD'] . ' </label><br>
<input type="hidden" name="id" value="' . $counter . '">
<p style="font-size:0.45em;"> ' . $typemessage . '</p>
</div>
';
echo'</div>';
$counter++;
}
echo '</div>';
echo '<input type="submit" name="checkanswer" value="Submit">';
echo'</form>';
}
else {
echo "0 results";
}
if(isset($_POST["answer"])) {
foreach($answerkey as $output){
echo $output . "<br>";
}
}
}
?>
<style>
.flex-container {
display: flex;
flex-wrap: wrap;
}
.flex-container > div {
font-size: 30px;
width: calc(50% - 2em);
margin: 1em;
}
.test {
flex: 1 0 41%; /* explanation below */
font-size: 20px;
}
.img {
height: 100px;
flex: 1 0 41%;
width: 100px;
}
</style>
<html>
<head>
<title>Ensemble Education</title>
<a>
<form method ="post" action="#form-anchor" id="form-anchor">
<input type="submit" name="answer" value="Click for Answer Key">
</a>
</form>
</head>
</html>
<?php
if(isset($_POST["checkanswer"])) {
$counter--;
$sql = "SELECT Version FROM mathanswers WHERE email = '$email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
$version = $row['Version'];
}
$version++;
}
else
{
$version = 1;
}
while ($counter != 0) {
$answer = $_POST["answer-$counter"];
$newcorrect = $answerkey[$counter];
if (is_correct($answer, $newcorrect)) {
$type = $typelist[$counter];
$topic = $topiclist[$counter];
$skill = $skilllist[$counter];
$newtype = fix_type($type);
$sql = "INSERT INTO mathanswers (Email, type, Topic, Skill, Correct, Version) VALUES ('$email', '$newtype', '$topic', '$skill', '1', '$version')";
$result = $conn->query($sql);
}
else {
$type = $typelist[$counter];
$topic = $topiclist[$counter];
$skill = $skilllist[$counter];
$newtype = fix_type($type);
$sql = "INSERT INTO mathanswers (Email, type, Topic, Skill, Correct, Version) VALUES ('$email', '$newtype', '$topic', '$skill', '0', '$version')";
$result = $conn->query($sql);
}
$counter--;
}
$URL="../studentprofile.php";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
}
?>
编辑
这是导致的更新格式错误。 Current output
这是我想要的样子。 Desired Output
【问题讨论】:
-
当前输出有什么问题?您的预期输出如何?
-
@tcadidot0 当前输出在没有任何结构的情况下同时输出 mathcalc 和 mathnocalc。我希望我的输出先打印出所有 mathcalc 问题,然后打印出 mathnocalc 问题
-
这应该是你要找的。 stackoverflow.com/questions/28857920/…,你应该使用
UNION -
顺便说一句,请认真考虑您的设计是否是最优的。 “通常”,我们会有一个单独的答案表,每个答案都有一行,一列指示给定答案是否正确,一列指示“A”、“B”、“C”中的哪一个, 或 'D' 答案属于。