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

标签 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

下面是数据库设计,以便您可以看到数据来自哪里:

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

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

最佳答案

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

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

关于php - 我得到一个空白的动态 html 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410204/

相关文章:

php - 将数组键递增一个以提供更多功能

打印多行时PHP控制台反转滚动

css - flex 属性与按钮高度混淆?

javascript - 为什么使用 ng-value ="true"和值 ="1"选择两个单选按钮

html - 我如何在输入标签中插入图像

php - 图片上传插件(php jquery flash)

php - 用于处理 session 的不同数据库...我做的正确吗?

javascript - 使用 JS mousemove 性能更改 CSS 属性

javascript - jQuery 获取所有动态生成的选择

javascript - jQuery load() 没有在 url 中传递完整的字符串