php - 如何使用td id插入数据

标签 php html mysql

我的 PHP 代码如下。

<table width="100%" >                   
    <tr  width="100%" class="bgcolor_02">                       
        <th ROWSPAN="2" >SUBJECTS </th>                     
        <th COLSPAN="2" >EXAMS </th>                        
        <th COLSPAN="2" >PROJECTS </th>                     
    </tr>
    <tr class="bgcolor_02">
        <th id="1E"> 1.EXAM</th>
        <th id="2E"> 2.EXAM</th>
        <th id="1P"> 1.PROJECT</th>           
        <th id="2P"> 2.PROJECT</th>                           
    </tr>                

<?php
    $myexamresults = $db-> getrows (" SELECT * FROM `es_exam_result` WHERE `es_studentid` = '".$_SESSION['eschools']['user_id']."'  ORDER BY `es_examorder` ASC  ");    
    $pre_subjects=subjectnameByClass($es_subjectshortname);         
    foreach ($pre_subjects as $ndsdersliste ) {
        $subjeid=$ndsdersliste['es_subjectid']; ?>

       <tr > <td ><?php echo $ndsdersliste['es_subjectname']; ?></td>        
       <?php       
       foreach ($myexamresults as $ndsmyexam) {         
          $puan="";
          if( $ndsdersliste['es_subjectname'] == $ndsmyexam['es_subjectname'] )     {                   
                $examname = $ndsmyexam['es_examname'];
                $puan  =  $ndsmyexam['es_puan'];            
                if ($examname =="1.Exam"){ ?> <td  headers="1E"  ><?php {echo $puan  ;} ?> </td>  <?php  } 
                if ($examname =="2.Exam"){ ?> <td  headers="2E" ><?php {echo $puan  ;} ?> </td>  <?php  }   
                if ($examname =="1.Project"){ ?> <td   headers="1P" ><?php {echo $puan  ;} ?> </td>  <?php  }   
                if ($examname =="2.Project"){ ?> <td   headers="2P" ><?php {echo $puan  ;} ?> </td>  <?php  } 
            } }  ?>               
        </tr>
    <?php      } ?>
            </table>    


我可以从数据库中获取考试结果。但我无法在标题下插入正确的数据。如果学生只有“1.Project”成绩,则显示在“1.Exam”下。如何在正确的标题下插入。谢谢..

最佳答案

您必须创建空 <td>如果没有结果具有相同数量的<td>对于每个 <tr> :

<tr > <td ><?php echo $ndsdersliste['es_subjectname']; ?></td>  
<?php
$td = [];
foreach ($myexamresults as $ndsmyexam) {         
    $puan="";
    if( $ndsdersliste['es_subjectname'] == $ndsmyexam['es_subjectname'] )     
    {                      
        $examname = $ndsmyexam['es_examname'];
        $puan  =  $ndsmyexam['es_puan'];            

        if ($examname =="1.Exam") { $td['1E'] = $puan ; }
        if ($examname =="2.Exam") { $td['2E'] = $puan ; }
        if ($examname =="1.Project") { $td['1P'] = $puan ; }
        if ($examname =="2.Project") { $td['2P'] = $puan ; }
    } 
}

if (isset($td['1E'])) echo '<td headers="1E">'.$td['1E'] .'</td>' ; else echo '<td headers="1E"></td>' ;
if (isset($td['2E'])) echo '<td headers="2E">'.$td['2E'] .'</td>' ; else echo '<td headers="2E"></td>' ;
if (isset($td['1P'])) echo '<td headers="1P">'.$td['1P'] .'</td>' ; else echo '<td headers="1P"></td>' ;
if (isset($td['2P'])) echo '<td headers="2P">'.$td['2P'] .'</td>' ; else echo '<td headers="2P"></td>' ;

?>
</tr>

关于php - 如何使用td id插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49077065/

相关文章:

javascript - 需要 Jquery 按钮鼠标悬停滚动到顶部

sql - Mysql查询以便更好地排序结果

mysql 磁盘 I/O 100%

javascript - 无法运行包含的js文件

php - Laravel:使用自定义请求 header 测试路由

javascript - 在不影响第一行的情况下,每隔一行缩进表格。 html/css/js/jQuery

MySQL 行号与 ORDER BY

php - 使用默认样式值编辑段落标签

php - 通过 Composer 学说mongodb安装错误

html - CSS 作用域和多个类