繁体   English   中英

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

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

我有以下格式的PostgreSQL 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. 按行和列索引将它们存储到输出数组中

    喜欢 :

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

  4. 现在您可以将它们打印到html表中

    喜欢 :

     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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM