动态html表格未正确显示

时间:2012-11-16 13:29:50

标签: php html

查看我的动态html表代码,它当前正在显示如下表:

table not displayed correctly

这显然是不正确的,它应该显示如下表:

what table should look like

我猜测代码的标记有问题,但我似乎无法看到问题所在。有没有人知道问题出在哪里才能获得正确的表格布局? 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>

以下是数组的print_r显示的内容:

  

数组([0] =&gt; 1 [1] =&gt; 1 2 =&gt; 1 3 =&gt; 2)数组([0] =&gt;名称   ROM中的三个特征[1] =&gt;在ROM 2 =&gt;中命名三个要素   在ROM 3 =&gt;中命名三个要素这是一个答案)数组(   [0] =&gt; A [1] =&gt; B 2 =&gt; D 3 =&gt;真)数组([0] =&gt; 5 [1] =&gt; 5 2   =&GT; 5 3 =&gt; 5)

2 个答案:

答案 0 :(得分:1)

这只是一个疯狂的猜测.. 如果删除

会发生什么
else{?>
                <td class="questionnumtd" name="numQuestion" ></td>
                <td class="questioncontenttd" ></td>
            <?php
            }

else{?>
                <td class="noofmarkstd"  q_group="1"></td>
            <?php
            }

来自您的代码..

答案 1 :(得分:1)

因为你的标题有一个rowpan,我不确定你为什么要添加

<td class="questionnumtd" name="numQuestion" ></td>
<td class="questioncontenttd" ></td>

标题不是新的时候。我只是删除这个else语句以及上面两行。

第二个else语句及其html代码相同。删除它。

<tr class="questiontd">
<?php
if($questionId != $prev_ques){?>
     <td class="questionnumtd" name="numQuestion" ...></td>
     <td class="questioncontenttd" rowspan="<?=$row_span[$questionId]?>"><?=$searchQuestionContent[$key]?> </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 }?>
</tr>