从MySQL动态生成HTML表

时间:2014-10-12 00:38:34

标签: php html mysql

我有三个mysql表科目,考试,考试信息

表 - 主题

  1. subjectid
  2. 主旨名称
  3. subjectExamid
  4. 表 - EXAMINFO

    1. examid
    2. 表格考试

      1. FNAME
      2. L-NAME
      3. studentid
      4. 得分
      5. subjectid

        ON subject.subjectExamid = exam.examid

        ON examination.subjectid = subject.subjectid

      6. 现在我想生成一个HTML表格,显示学生每篇论文针对主题的分数

        STRUCTURE TABLE OUTPUT 针对每个科目分数的学生详细信息

          

        编辑代码示例

        <?php
          $examinid = 3;
          $subjects = mysqli_query(
              $con," 
                  SELECT * FROM subjects
                  WHERE examid = '$examinid'
                  ORDER BY shortname ASC
        
          ");
          $content = mysqli_query(
              $con," 
                  SELECT DISTINCT exam.idcandidate, exam.sex, exam.fname, exam.lname
                  FROM examinations 
                  AS exam
                  INNER JOIN examinfo
                  AS info
                  ON exam.id_subject = info.idsubject
                  WHERE info.idexam = '$examinid'         
          ");
        ?>
        <div id="table_1">
          <table cellpadding="0" cellspacing="0" border="0">
              <tr>
                  <td class="table1tr">#</td> 
                    <td class="table1tr">Candidate</td>   
                    <td class="table1tr">ID</td>  
                    <td class="table1tr">Sex</td> 
                  <?php
                      // output subjects 
                      while($subRow = mysqli_fetch_array($subjects)){
                          $arbv = strtoupper($subRow['shortname']);
                          $subjectname = ucwords(strtolower($subRow['subjectname']." - ".$subRow['subjectid'].""));
                  ?>
                  <td class="table1tr" title="<?php echo $subjectname; ?>">
                      <?php echo $arbv; ?>
                    </td>
                  <?php   
                      }
                  ?>                  
                    <td class="table1tr">Exam</td>
                </tr>
              <?php
                  while($stdnt = mysqli_fetch_array($content)){
                      $fullname = ucwords(strtolower("$stdnt[lname] $stdnt[fname]"));
                      $studentid = str_replace(array('/', 'M', 'W', 'S', 'F', '-'), "",$stdnt['idcandidate']);
                      if($sex = $stdnt['sex'] == Male){
                          $sex = M;
                      }else{ $sex = F; }
                      $id_subject = $stdnt['id_subject'];
                      $x++;
                      $zebra_1 = ($x%2)? 'TableZebra_1': 'TableZebra_2';
              ?>        
              <tr>
                  <td class="<?Php echo $zebra_1; ?>"><?php echo $count++; ?></td>    
                    <td class="<?Php echo $zebra_1; ?>"><?Php echo $fullname; ?></td> 
                    <td class="<?Php echo $zebra_1; ?>"><?php echo $studentid; ?></td>    
                    <td class="<?Php echo $zebra_1; ?>"><?php echo $sex; ?></td>
                  <td class="<?Php echo $zebra_1; ?>">
                    <!-- Problem is here how to output the subject grades $grade -->
                    <!-- 
                        My first unsuccessful approach  
        
                        SELECT score
                        FROM examinations AS test
                        INNER JOIN examinfo AS testinfo ON testinfo.idsubject = test.id_subject
                        WHERE testinfo.idexam
                        IN (
        
                        SELECT idexam
                        FROM examinfo
                        WHERE idexam = $examinid
                        )
                        AND test.id_subject = $id_subject AND test.idcandidate = '$studentid'
        
                        Then output results - But this falls it shows one student subjects in one cell
                    -->
                    </td> 
                    <td class="<?Php echo $zebra_1; ?>">Exam</td>
                </tr>        
                  <?php   
                      } // loop content
                  ?>      
        
            </table>  
         </div>
        

1 个答案:

答案 0 :(得分:0)

如果您的解决方案不是concat();你可能先

按照简单的步骤

1 loop $contents // to get info such as studentid
2 inside the loop of $contents loop $subjects // to get all subjects including subjectids
3 inside $subject loop, loop examinations table where studentid = '$studentid' AND subjectid = '$subjectid'
if step three return null echo empty cell otherwise echo cell with score

我没有时间对此进行测试,但您可以按照步骤进行操作,否则请尝试谷歌搜索