通过使用行和列索引以表格格式显示数据库表中的数据


Show data from database table in tabular format by using row and column index

我有以下格式的postgre sql数据库表

Row_no  Col_no  Name
1        1      Test
1        2      Result
1        3      Observation
2        1      abc
2        2      Result1
2        3      observation1

我想在html表格中以以下格式显示数据

Test    Result     Observation
abc     Result1    observation1

有谁能告诉我怎么做吗?

方法:

  1. 创建输出数组
  2. 从表
  3. 中选择每一行
  4. 按行和列索引存储到输出数组

    like:

    $ARR_OUPUT[$row_no][$col_no] = $name;

  5. 现在可以打印到html表格

    like:

    echo '<table>';
    foreach($ARR_OUTPUT as $key=>arr_temp)
    {
         echo '<tr>';
         foreach($arr_temp as $name)
         {
              echo '<td>'.$name.'</td>';
         }
          echo '</tr>';
    } 
    echo '<table>';
    

你的数组将是这样的

Array
(
    [1] => Array
        (
            [1] => Test
            [2] => Result
            [3] => Observation
        )
    [2] => Array
        (
            [1] => abc
            [2] => Result1
            [3] => Observation1
        )
)

试试下面的代码

<table>
    <thead>
        <tr>
            <th>Test</th>
            <th>Result</th>
            <th>Observation</th>
        </tr>
    </thead>
    <tbody>
    <?php
        $con=mysqli_connect("localhost","username","password","dbname");
        $sql=mysqli_query($con, "SELECT * from Name");
        while($row=mysqli_fetch_array($sql))
        {
    ?>
    <tr>
        <td><?php echo $row['Test'];?></td>
        <td><?php echo $row['Result'];?></td>
        <td><?php echo $row['Observation'];?></td>
    </tr>
    <?php   }?>
    </tbody>
</table>