0

我正在尝试创建动态 html 表,但问题是它没有在表中显示任何数据。我知道查询是正确的,因为我在 sql 中测试了查询并输出了数据。我猜的问题是动态 html 表本身。下面是代码:

JavaScript/JQuery:

//javascript below will perform calculation between adding numbers between text inputs per each question
//answer for each calculation per question is stored under "Total Marks Remaining" Column
/*If a question only has one answer, then the text input under the "Marks Per Answer" column becomes
read only and displays the same number as the total marks under the "Total Marks Remaining" column
for that question*/

$(function() {   
    //alert("here");         
    var questions = $('#markstbl td[class*="_ans"]').length-1;

    //disable single entry
    for (var i=0;i<=questions;i++){   
        if($("[class*=q"+i+"_mark]").length ==1){
            var t_marks = $("[class*=q"+i+"_ans]").html();
            //alert(t_marks);
            $("[class*=q"+i+"_mark]").val(t_marks).attr("disabled","disabled");
            //$("[class*=q"+i+"_mark]").attr("disabled","disabled");
        }                    
    }

    //find each question set and add listeners
    for (var i=0;i<=questions;i++){                                     
        $('input[class*="q'+i+'"]').keyup(function(){
            var cl = $(this).attr('class').split(" ")[1]
            var questionno = cl.substring(cl.indexOf('q')+1,cl.indexOf('_'));
            var tot_marks = $(".q"+questionno+"_ans_org").val();
            //alert(tot_marks);
            var ans_t=0;
            $("[class*=q"+questionno+"_mark]").each(function(){
                var num = (isNaN(parseInt($(this).val())))?0:parseInt($(this).val());
                ans_t+=parseInt(num);                             
            });
            ans_t=tot_marks-ans_t;                             
            //alert(ans_t);
            //var fixedno = tot_marks;
            var ans = (parseInt(ans_t)<0)?tot_marks:ans_t;
            $(".q"+questionno+"_ans").val(ans);
            $(".q"+questionno+"_ans_text").html(ans);
        });
    }
});

</script>

PHP:

    <?php

if (isset($_POST['id'])) {

$_SESSION['id'] = $_POST['id'];

}

$assessment = $_SESSION['id'];
    include('connect.php');

    $query = "SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionContent, an.Answer, q.QuestionMarks 
    FROM Session s 
    INNER JOIN Question q ON s.SessionId = q.SessionId
    JOIN Answer an ON q.QuestionId = an.QuestionId AND an.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
    $searchQuestionId = array();
    $searchQuestionContent = array();
    $searchAnswer = array();
    $searchMarks = array();

    // Fetch the results into an array

    // get result and assign variables (prefix with db)
    $stmt->bind_result($dbSessionId, $dbSessionName, $dbQuestionId, $dbQuestionContent, $dbAnswer, $dbQuestionMarks);
    while ($stmt->fetch()) {
        $searchQuestionId[] = $dbQuestionId;
        $searchQuestionContent[] = $dbQuestionContent;
        $searchAnswer[] = $dbAnswer;
        $searchMarks[] = $dbQuestionMarks;
    }?>  

HTML:

<form id="Marks" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table border='1' id='markstbl'>
    <thead>
        <tr>
            <th class='questionth'>Question No.</th>
            <th class='questionth'>Question</th>
            <th class='answerth'>Answer</th>
            <th class='answermarksth'>Marks per Answer</th>
            <th class='noofmarksth'>Total Marks</th>
        </tr>
    </thead>
    <?php
    $row_span = array_count_values($searchQuestionId);
    $prev_ques = '';
    foreach($searchQuestionId as $key=>$questionId){?>
        <tbody>   
            <tr class="questiontd">
            <?php
            if($questionId != $prev_ques){?>
                <td class="questionnumtd" name="numQuestion" rowspan="<?=$row_span[$questionId]?>"><?=$questionId?> <input type="hidden" name="q<?=$questionId?>_ans_org" class="q<?=$questionId?>_ans_org" value="<?=$searchMarks[$key]?>"><input type="hidden" name="q<?=$questionId?>_ans" class="q<?=$questionId?>_ans" value="<?=$searchMarks[$key]?>"></td>
                <td class="questioncontenttd" rowspan="<?=$row_span[$questionId]?>"><?=$searchQuestionContent[$key]?> </td>
            <?php
            }else{?>
                <td class="questionnumtd" name="numQuestion" ></td>
                <td class="questioncontenttd" ></td>
            <?php
            }?>
                <td class="answertd" name="answers[]"><?=$searchAnswer[$key]?></td>
                <td class="answermarkstd">
                <input class="individualMarks q<?=$questionId?>_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
                </td>
            <?php
            if($questionId != $prev_ques){?>
                <td class="noofmarkstd q<?=$questionId?>_ans_text"  q_group="1" rowspan="<?=$row_span[$questionId]?>"><?=$searchMarks[$key]?></td>
            <?php
            }else{?>
                <td class="noofmarkstd"  q_group="1"></td>
            <?php
            }?>
            </tr>
        <?php
        $prev_ques = $questionId;
    }?>
    </tbody>
</table>
</form>

下面是它显示的屏幕截图:

空表

以下是表格应显示的内容(每个答案列的标记包含每行的文本输入)

桌子应该是什么样子

以下是数据库设计,因此您可以查看数据的来源:

会话表:(存储考试详细信息的位置)

SessionId  SessionName
1          AAA

问题表:(存储每个考试的问题)

SessionId   QuestionId       QuestionContent                Total Marks
1                 1          Name three features in a ROM        5 
1                 2          Here is a single answer             5     

答案表:(存储每个考试中每个问题的答案)

AnswerId(auto)  SessionId QuestionId  Answer
1               1         1           A
2               1         1           B
3               1         1           D
4               1         2           True

Individual_Answer 表:(存储每个单独答案的每个单独标记)

AnswerId   AnswerMarks
1          2
2          2
3          1
4          5

更新:

查看我的 html 代码,为什么它显示如下表格:

表格显示不正确

4

3 回答 3

1

<?php之前检查缺少的 php 打开标记include('connect.php');

还要避免使用短标签<?=并将其替换为<?php echo

于 2012-11-16T04:15:33.090 回答
0

放置<tbody>在 foreach 循环之外

foreach($searchQuestionId as $key=>$questionId){

?>
<tbody>

</thead>
<tbody>
....
....

foreach($searchQuestionId as $key=>$questionId){

?>
于 2012-11-16T04:05:36.920 回答
0

除非这里没有显示一些代码,否则可能就像没有设置 $assesment 变量一样简单,所以没有任何东西被发送到查询?

此外,为了确认您实际上得到了返回结果并将它们绑定好,我将对像 searchQuestionContent 这样的数组进行 var_dump 以确保它们具有您期望的内容,如果没有,您知道您的问题出在查询/绑定数据。如果它们确实具有您期望的内容,那么您就知道问题出在表输出中。

于 2012-11-16T04:13:36.013 回答