1

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

我的问题是,我只需要一点帮助来完成代码就可以做到这一点。我在$row变量中收到通知,我知道我没有在 if 语句之前调用它来引用它,但我的问题是什么是$row变量假设设置为或者我需要调用$row其他东西?

收到的示例通知:

注意:未定义的变量:row in ... on line ...

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

另一个问题是,如果您查看最底部的代码,当我尝试显示不正确的答案时<?php echo $incorrect_ans[$key]; ?>,它会一直显示单词Array。我是否错误地调用了数组?我希望它显示收到的错误答案。

以下是完整代码

    $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> 

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

数据库表结构:

会话表(又名考试表)

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 表:

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   

更新:

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

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

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

下面显示了当前的布局和它的布局方式:

在此处输入图像描述

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

<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>
4

1 回答 1

2

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

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

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

尝试做print_r然后$incorrect_ans你会看到结果数组的结构,你有数组列表,这就是为什么echo $incorrect_ans[$key];导致Array. 你可以这样做echo implode(',', $incorrect_ans[$key]);,你会得到字符串,其值由,字符分隔。

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

我还注意到您正在设置没有此类属性的属性(据我所知)-也许您想使用name一些?<td><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; 
?>
于 2013-01-09T21:23:11.767 回答