当列和行标题重叠时,基于数据库信息创建HTML表内容

时间:2018-11-29 09:29:49

标签: php html mysql

我需要能够根据数据库中的信息系统地创建表。我能够系统地创建列和行标题/标题。我正在努力使表内容正确无误。

在这种情况下,我有一个场景,其中有三个样本,每个样本都必须在表中输入选定的分析结果。有些分析与不同的样本重叠,而另一些则没有。每个分析都对应一个数字。因此,分析1 = 1,分析2 = 2,分析3 = 3。每个样本都有自己的分析,并显示为数组:Sample1 ([0] => 1) sample2 ([0] => 2 [1] => 3)sample3 ([0] => 1 [1] => 2 [2] => 3)。将分析数组合并,并删除重复项以产生列名:

column headings: ([0] => 1 [1] => 2 [2] => 3)

我仅需要表输入才能用于具有特定分析的样本。我的问题是如何过滤数据库信息以产生相关的列输入信息?我在代码中标记了我试图产生相关输入的部分,但是没有产生结果。

   <?php 
    if (!isset($_GET['display_results'])) {
      require '../server/dbh.php';
      header("Location: ../web_page/analysis_results_disp_page.php?display_results=error");
    } else {
      $order_id = $_GET['display_results'];
      $query = "SELECT * FROM samples_database WHERE order_id=$order_id AND micro_analysis<>'';";
      $result = mysqli_query($conn, $query);
      $resultCheck = mysqli_num_rows($result);

      $query2 = "SELECT * FROM samples_database WHERE order_id=$order_id AND micro_analysis<>'';";
      $result2 = mysqli_query($conn, $query2);

      $micro_analysis = '';
      while($input = mysqli_fetch_array($result2)) {
        $micro_analysis .= $input['micro_analysis'] . ',';
      }
      $micro_analysis_arr = array_filter(array_unique(explode(',', $micro_analysis)));

      ?>
      <form action="#" method="POST">
       <table>
        <thead>
          <tr>
           <th colspan="1"></th> 
           <?php
           foreach ($micro_analysis_arr as $row) {
            $query3 = "SELECT * FROM microbiology_analysis_database WHERE id=$row"; 
            $result3 = mysqli_query($conn, $query3);
            $input2 = mysqli_fetch_array($result3);
            $analysis = $input2['m_analysis'];
            ?> 
            <th colspan="2"><?php echo $analysis; ?></th>
          <?php } ?>
        </tr>
      </thead>
      <tbody>
       <?php
       if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result)) { 
          $sample_id = $row['sample_id']; ?> 
          <tr>
            <td><?php echo $row['env_sam_id']; ?></td>
            <?php
//This is the start of the section in question:
            foreach ($micro_analysis_arr as $row) { 
              $query4 = "SELECT * FROM samples_database WHERE sample_id=$sample_id AND m_analysis=$row"; 
              $result4 = mysqli_query($conn, $query4);
              $resultCheck4 = mysqli_num_rows($result4);

              if ($resultCheck4 > 0) { ?>

               <td><select name="<?php echo $row; ?>">
                <option value="" disabled selected>Detected?</option>
                <option value="Yes">Yes</option>
                <option value="No">No</option>
                <option value="ND">Not Determined</option>
              </select></td>
              <td><input type="text" name="#" placeholder="Enter Result"></td>

            <?php } else { ?>

             <td></td>
             <td></td>

           <?php } } ?>
         </tr>
       <?php } } ?>
     </tbody>
    </table>
    </form>      
    <?php   
    } 
    ?>

编辑!

以下是表格外观的示例,而不是每一列的所有输入:

<table>
  <thead>
    <tr>
      <th colspan="1"></th> 
      <th colspan="2">Analysis 1</th>
      <th colspan="2">Analysis 2</th>
      <th colspan="2">Analysis 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Sample 1</td>
      <td><select name="">
        <option value="" disabled selected>Detected?</option>
        <option value="Yes">Yes</option>
        <option value="No">No</option>
        <option value="ND">Not Determined</option>
      </select></td>
      <td><input type="text" name="#" placeholder="Enter Result"></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <td>Sample 2</td>
    <td></td>
    <td></td>
    <td><select name="">
      <option value="" disabled selected>Detected?</option>
      <option value="Yes">Yes</option>
      <option value="No">No</option>
      <option value="ND">Not Determined</option>
    </select></td>
    <td><input type="text" name="#" placeholder="Enter Result"></td>
    <td><select name="">
      <option value="" disabled selected>Detected?</option>
      <option value="Yes">Yes</option>
      <option value="No">No</option>
      <option value="ND">Not Determined</option>
    </select></td>
    <td><input type="text" name="#" placeholder="Enter Result"></td>
  </tr>
  <td>Sample 3</td>
  <td><select name="">
    <option value="" disabled selected>Detected?</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="ND">Not Determined</option>
  </select></td>
  <td><input type="text" name="#" placeholder="Enter Result"></td>
  <td><select name="">
    <option value="" disabled selected>Detected?</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="ND">Not Determined</option>
  </select></td>
  <td><input type="text" name="#" placeholder="Enter Result"></td>
  <td><select name="">
    <option value="" disabled selected>Detected?</option>
    <option value="Yes">Yes</option>
    <option value="No">No</option>
    <option value="ND">Not Determined</option>
  </select></td>
  <td><input type="text" name="#" placeholder="Enter Result"></td>
</tr>
</tbody>
</table>

当将数据插入数据库时​​,将显示以下内容:

INSERT INTO `samples_database` (`sample_id`, `order_id`, `env_sam_id`, `micro_analysis`) VALUES ('1', '1', 'sample 1', '1'), ('2', '1', 'sample 2', '2,3'), ('3', '1', 'sample 3', '1,2,3')

分析数据库信息:

INSERT INTO `microbiology_analysis_database` (`id`, `m_analysis`) VALUES ('1', 'analysis1'), ('2', 'analysis2'), ('3', 'analysis3')

0 个答案:

没有答案