如何从可能的答案列表中检索不正确的答案

如何从可能的答案列表中检索不正确的答案

本文介绍了如何从可能的答案列表中检索不正确的答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我试图通过首先完成问题的Option Type,然后从答案列表中删除正确的答案(Answer字段`)来检索可能答案的列表.

With the code below I am trying to retrieve the list of possible answers by first finishing a question's Option Type and then removing the correct answers (Answer field`) from the list of answers.

我的问题是,我只需要一点帮助就可以完成代码了.我在$row变量中得到了一个通知,我知道我没有在if语句之前调用它来引用它,但是我的问题是$row变量应该设置为或需要什么打电话给$row其他?

My question though is that I just need a little help finishing the code off to be able to do this. I am getting an notices in the $row variable where I know I have not called up on it before the if statement to refer to it but my question on that is what is $row variable suppose to be set as or do I need to call $row something else?

收到示例通知:

注意:试图在...上获取非对象的属性...

Notice: Trying to get property of non-object in ... on line ...

如果您在最底端查看代码,则当我尝试显示不正确的答案时,另一个问题将出现.<?php echo $incorrect_ans[$key]; ?>它始终显示单词Array.我是否错误地调用了阵列?我希望它显示收到的错误答案.

Another is issue if you look at code at the very bottom, when I try to display the incorrect answers <?php echo $incorrect_ans[$key]; ?> It keeps displaying the word Array. Am I calling the array incorrectly? I want it to display the incorrect answers recieved.

下面是完整的代码

    $query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, q.OptionId, o.OptionType
    FROM
    Question q INNER JOIN Answer an ON q.QuestionID = an.QuestionID
    INNER JOIN Option_Table o ON o.OptionID = q.OptionID
    INNER JOIN Session s ON s.Sessionid = q.Sessionid
    WHERE s.SessionName = ?
    ORDER BY q.QuestionId, an.Answer
       ";

           // prepare query
           $stmt=$mysqli->prepare($query);
           // You only need to call bind_param once
           $stmt->bind_param("s", $assessment);
           // execute query
           $stmt->execute();


               // This will hold the search results
            $searchQuestionNo = array();
            $searchQuestionContent = array();
            $totalMarks = array();
            $searchAnswerId = array();
            $searchMarks = array();

            // Fetch the results into an array

           // get result and assign variables (prefix with db)
        $stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);
              while ($stmt->fetch()) {

                   $specialOptionTypes = array(
            'Yes or No' => array( 'Y', 'N' ),
            'True or False' => array( 'T', 'F' ),
        );

        // Do this for each row:
        if ( array_key_exists( $row->OptionType, $specialOptionTypes ) ) {
            $options = $specialOptionTypes[ $row->OptionType ];
        } else if ( preg_match( '/^([A-Z])-([A-Z])$/', $row->OptionType, $match ) ) {
            $options = range( $match[1], $match[2] );
        } else {
            // issue warning about unrecognized option type
            $options = array();
        }
        $right = str_split( $row->Answer );  // or explode() on a delimiter, if any
        $wrong = array_diff( $options, $right );

                $searchQuestionNo[] = $dbQuestionNo;
                $searchQuestionContent[] = $dbQuestionContent;
                $incorrect_ans[] = $wrong;
                $searchAnswerId[] = $dbAnswerId;
                $totalMarks[] = $dbQuestionMarks;
                $searchMarks[] = $dbQuestionMarks;
              }

....

          //table row

          <td class="answertd" name="incorrectanswers[]"><?php
    echo $incorrect_ans[$key];
    ?></td>

如果您想查看数据库表以查看每个表中的内容,请查看以下内容:

If you want to see the database tables to see what is in each table then have a look below:

数据库表结构:

会话表(又名考试表)

SessionId(auto)  SessionName
137              XULWQ

问题表:

SessionId  QuestionId QuestionContent  QuestionNo QuestionMarks  OptionId
137        1          Name 2 Things     1         5               5
137        2          Name 3 Things     2         5               2

Option_Table表:

Option_Table Table:

OptionId  OptionType
1         A-C
2         A-D
3         A-E
4         A-F
5         A-G
6         A-H

答案表:

 AnswerId(auto) SessionId  QuestionId  Answer
   200            137        1           B
   201            137        1           F
   202            137        2           D
   203            137        2           A
   204            137        2           C

更新:

现在唯一的问题是错误答案的布局,我希望它在每个问题的单独行中显示每个错误答案:

Only issue now is the layout of the incorrect answers, I want it to display each incorrect answer in its own row per question:

因此,让我们说以下是每个问题的正确答案和错误答案:

So lets say below are the correct and incorrect answers for each question:

Question Number: 1   Correct Answer(s) B     Incorrect Answers A C D
Question Number: 2   Correct Answer(s) A C   Incorrect Answers B D
Question Number: 3   Correct Answer(s) D     Incorrect Answers A B C

以下显示了当前布局及其布局方式:

Below shows the current layout and the way it should be laid out:

当前输出的代码是这样的:

The code for the current output is this:

<table border='1' id='penaltytbl'>
<thead>
<tr>
<th class='questionth'>Question No.</th>
<th class='answerth'>Incorrect Answer</th></tr>
</thead>
<tbody>
<?php
$row_span = array_count_values($searchQuestionNo);
$prev_ques = '';
foreach($searchQuestionNo as $key=>$questionNo){

?>

<tr class="questiontd">
    <?php
    if($questionNo != $prev_ques){
    ?>
    <td class="questionnumtd q<?php echo$questionNo?>_qnum" rowspan="<?php echo$row_span[$questionNo]?>">
    <?php echo$questionNo?><input type="hidden" name="numQuestion" value="<?php echo$questionNo?>" />
    </td>
    <?php
    }
    ?>
<td class="answertd"><?php echo implode(',', $incorrect_ans[$key]);?></td>
</tr>
<?php
$prev_ques = $questionNo;
}
?>
</tbody>
</table>

推荐答案

$row的问题非常简单-没有定义变量$row.无需获取行,而是将变量绑定到结果值,因此只需将$row->OptionType替换为绑定变量$dbOptionType,将类似的$row->Answer替换为$dbAnswer.要获取行,您应该这样做:

Problem with $row is pretty simple - there is no variable $row defined. Instead of fetching rows you're binding variables to result values, so just replace $row->OptionType with bound variable $dbOptionType and similar $row->Answer with $dbAnswer.To fetch rows you should rather do it this way:

$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_object()) {
    // ...
}

在示例代码中,我也没有看到定义的$incorrect_ans变量,也许您正在其他地方执行此操作,但是如果没有,则在其中定义其他列表的地方添加$incorrect_ans = array()(在// This will hold the search results行下方).

In example code I don't see also $incorrect_ans variable defined, maybe you're doing it somewhere else but if not then add $incorrect_ans = array() for example there where you're defining other lists (below // This will hold the search results line).

尝试在$incorrect_ans上执行print_r,然后您将看到结果数组的结构,这里有数组列表,这就是echo $incorrect_ans[$key];生成Array的原因.您可以执行echo implode(',', $incorrect_ans[$key]);,并且会得到字符串,字符串中的值由,字符分隔.

Try doing print_r on $incorrect_ans then you'll see structure of result array, you've got there list of arrays and thats why echo $incorrect_ans[$key]; is resulting in Array. You can do echo implode(',', $incorrect_ans[$key]); and you'll get string with values separated by , character.

我建议在while循环之前定义$specialOptionTypes,因为您没有在循环内更改该数组,所以我认为没有理由在每次迭代时都重新创建它.

I'd suggest to define $specialOptionTypes before while loop, you're not changing that array inside a loop so I suppose there is no reason to recreate it on every iteration.

我还注意到您正在将name属性设置为<td>,而该属性没有这样的属性(据我所知)-也许您想使用某些<input>?

I noticed also that you're setting name attribute to <td> which does not have such attribute (as far as I know) - maybe you wanted to use some <input>?

<?php

// .........

$query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, q.OptionId, o.OptionType
    FROM
    Question q INNER JOIN Answer an ON q.QuestionID = an.QuestionID
    INNER JOIN Option_Table o ON o.OptionID = q.OptionID
    INNER JOIN Session s ON s.Sessionid = q.Sessionid
    WHERE s.SessionName = ?
    ORDER BY q.QuestionId, an.Answer";

// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s", $assessment);
// execute query
$stmt->execute();


// This will hold the search results
$searchQuestionNo = array();
$searchQuestionContent = array();
$totalMarks = array();
$searchAnswerId = array();
$searchMarks = array();
$incorrect_ans = array();

// Fetch the results into an array

// get result and assign variables (prefix with db)
$stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionNo, $dbQuestionContent, $dbAnswer, $dbAnswerId, $dbQuestionMarks, $dbOptionId, $dbOptionType);
$specialOptionTypes = array(
    'Yes or No' => array('Y', 'N'),
    'True or False' => array('T', 'F'),
);
while ($stmt->fetch()) {
    // Do this for each row:
    if (array_key_exists($dbOptionType, $specialOptionTypes)) {
        $options = $specialOptionTypes[$dbOptionType];
    } else if (preg_match('/^([A-Z])-([A-Z])$/', $dbOptionType, $match)) {
        $options = range($match[1], $match[2]);
    } else {
        // issue warning about unrecognized option type
        $options = array();
    }
    $right = str_split($dbAnswer);  // or explode() on a delimiter, if any
    $wrong = array_diff($options, $right);

    $searchQuestionNo[] = $dbQuestionNo;
    $searchQuestionContent[] = $dbQuestionContent;
    $incorrect_ans[] = $wrong;
    $searchAnswerId[] = $dbAnswerId;
    $totalMarks[] = $dbQuestionMarks;
    $searchMarks[] = $dbQuestionMarks;
}

// .........

for ($key = 0, $cnt = count($incorrect_ans) ; $key < $cnt ; ++$key) :
?>
<td class="answertd">
    <?php echo implode(',', $incorrect_ans[$key]); ?>
</td>
<?php
endfor;
?>

这篇关于如何从可能的答案列表中检索不正确的答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 22:15