簡體   English   中英

我得到一個空白的動態html表

[英]I am getting a blank dynamic html table

我正在嘗試創建動態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代碼,為什么它顯示如下表:

表未正確顯示

include('connect.php');之前檢查缺少的php打開標記<?php include('connect.php');

還要避免使用<?=類的短標簽,並用<?php echo替換它們

<tbody>放置在foreach循環之外

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

?>
<tbody>

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

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

?>

除非此處未顯示某些代碼,否則可能就像未設置$ assessment變量一樣簡單,因此沒有任何內容發送給查詢嗎?

另外,要確認您實際上是在返回結果並將它們綁定好,我將對諸如searchQuestionContent之類的數組執行var_dump,以確保它們具有您期望的內容,如果不是,那么您就知道問題出在查詢/綁定數據。 如果它們確實具有您期望的內容,那么您就知道問題出在表輸出中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM