我得到一个空白的动态html表

时间:2012-11-16 04:02:16

标签: php jquery html mysql

我正在尝试创建动态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>

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

empty table

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

what table should look like

以下是数据库设计,以便您可以看到数据的来源:

会话表:(存储考试详情的地方)

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代码,为什么会显示如下表格:

table not displayed correctly

3 个答案:

答案 0 :(得分:1)

<?php

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

同时避免使用<?=等短标记,并将其替换为<?php echo

答案 1 :(得分:0)

<tbody>置于foreach循环之外

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

?>
<tbody>

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

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

?>

答案 2 :(得分:0)

除非这里没有显示某些代码,否则它可能就像未设置的$ assessment变量一样简单,所以没有任何内容被发送到查询中?

另外,为了确认你实际上得到了结果并将它们绑定好了,我会在像searchQuestionContent这样的数组上做一个var_dump,以确保它们有你期望的内容,如果不是你知道你的问题在查询/绑定数据中。如果他们确实拥有您期望的内容,那么您知道问题出在表输出中。