【问题标题】:how to split sql request into two sections (challenge)如何将sql请求分成两部分(挑战)
【发布时间】: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' 答案属于。

标签: php html css mysql sql


【解决方案1】:

基本上,我们使用 2 个SELECT 语句并使用UNION 将它们组合成一个结果集。

SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10

上述查询将返回 10 行类型为 mathnocalc 的随机行。你会确定的。我将上述查询作为嵌套查询与另一个 UNION 一起使用。

SELECT * FROM (SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10) as cat1
UNION
SELECT * FROM (SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathcalc' ORDER BY RAND() LIMIT 0,10) as cat2

试试上面的查询,让我知道结果。

为了根据类别显示结果,您可以简单地将查询一分为二:

示例逻辑:

$queryNoCalc = 'SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10';
$noCalcResult variable will store the result set of above query.

Similarly,
$queryCalc = 'SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathcalc' ORDER BY RAND() LIMIT 0,10';
$calcResult variable will store the result set of above query.

现在,您可以在不同的 DIV 中使用单独的结果。

<div class="no-calc">
  <h3>NO CALULATOR ALLOWED</h3>
  while ($noCalcResult):
    Do the stuff;
  endwhile;
</div>

<div class="calc">
  <h3>CALULATOR ALLOWED</h3>
  while ($queryCalc):
    Do the stuff;
  endwhile;
</div>

希望你理解这个概念。

【讨论】:

  • 查看我的评论
  • 如果您在执行嵌套查询时遇到问题,那么为什么不尝试对每种类型进行单独查询并将其显示在相应的 Div 中呢?就像 Create Two Divs with Headings 一样,然后将查询结果存储在单独的变量中并分别循环遍历结果。我知道这不是优化的解决方案,但它会使它变得简单易懂,以便您可以在它之上构建。如果您有任何疑问,请告诉我。
  • 不太确定我会怎么做。你介意编辑吗?
  • 更新了答案。请检查
  • 查看我上面的评论
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 2014-09-02
  • 1970-01-01
  • 2022-01-09
相关资源
最近更新 更多