简体   繁体   中英

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

I have postgre sql database table in following format

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

And I want to show data in html table in following format

Test    Result     Observation
abc     Result1    observation1

So can any one suggest me how to do this?

Method :

  1. create an array of output
  2. select each row from your table
  3. store them into output array by row and column index

    like :

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

  4. Now you can print them into a html table

    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>'; 

your array will be look like this

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

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

)

Try below code

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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