简体   繁体   中英

I am getting a blank dynamic html table

I am trying to create dynamic html table but the problem is that it is not displaying any data in the table. I know the query is correct because I tested the query in sql and it outputs the data. The problem I have I am guessing is the dynamic html table itself. Below is the code:

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>

Below is the screenshot of what it is displaying:

空表

Below is what the table should display (The Marks per Answer Column contains text inputs for each row)

什么样的桌子应该看起来像

Below is database design so you can see where the data is coming from:

Session Table: (Where the exam details is stored)

SessionId  SessionName
1          AAA

Question Table: (Where questions for each exams are stored)

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

Answer Table: (Stores answers for each question in each exam)

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

Individual_Answer Table: (Stores each individual mark for each individual answer)

AnswerId   AnswerMarks
1          2
2          2
3          1
4          5

UPDATE:

Looking at my html code, why is it displaying the table like this below:

表未正确显示

Check for missing php open tag <?php before include('connect.php');

Also avoid short tags like <?= and replace them with <?php echo

place <tbody> outside the foreach loop

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

?>
<tbody>

to

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

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

?>

Unless there is some code that is not being displayed here, perhaps it's as simple as the $assessment variable not being set, so nothing is being sent to the query?

Also, to confirm that you are actually getting the results back AND binding them OK, I'd be doing a var_dump on the arrays like searchQuestionContent to make sure they have the content you expect in them, if not you know your problem is in the query / binding data. If they do have the content you expect, then you know the problem lies in the table output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM