简体   繁体   English

php/html中多列显示单列mysql数据

[英]Display single column mysql data in multiple columns in php/html

I want to display MySQL data in HTML page, which looks something like the table below.我想在 HTML 页面中显示 MySQL 数据,如下表所示。

I want something like this:                
 +-------------+-------+-------------+------------+--------------+-----------+
 | Train Type  |TraNo  | Dep. Station| Dep. Time  | Arr. Station | Arr. Time |
 +-------------+-------+-------------+------------+--------------+-----------+
 | Fast        | 81    |Taipei       | 7:48       | Taitung      | 13:04     |
 +-------------+-------+-------------+------------+--------------+-----------+

But I have this:
 +-------------+-------+-------------+------------+--------------+-----------+
 | Train Type  |TraNo  | Dep. Station| Dep. Time  | Arr. Station | Arr. Time |
 +-------------+-------+-------------+------------+--------------+-----------+
 | Fast        | 81    |Taitung      | 13:04      | Taitung      | 13:04     |
 +-------------+-------+-------------+------------+--------------+-----------+

I have confirmed that the SQL query is correct.我已经确认 SQL 查询是正确的。 here's the reference这是参考

$sql = "SELECT ttype.TyName, train.TraNo, a.StaName, c.Time,b.StaName , d.Time
                FROM (((((ttype RIGHT JOIN train
                ON ttype.TyNo=train.TyNo)
                RIGHT JOIN pass c
                ON train.TraNo=c.TraNo)
                RIGHT JOIN station a
                ON a.StaNo=c.StaNo)
                RIGHT JOIN pass d
                ON train.TraNo=d.TraNo)
                RIGHT JOIN station b
                ON b.StaNo=d.StaNo)      

                WHERE c.Time < d.Time
                AND a.StaName='Taipei' 
                AND b.StaName='Taitung' ";
        $result = mysqli_query($link, $sql) or die("can't reach" . mysqli_error( ));

        $data = array(); // create a variable to hold the information

        while ($row = mysqli_fetch_array($result)){
          $data[] = $row; // add the row in to the results (data) array
          ?>
          <?php print_r($data); ?>
            <!--<tr>
                <td><?php echo $data ?></td> 
                <td><?php echo $row['TyName']?></td>
                <td><?php echo $row['TraNo']?></td>
                <td><?php echo $row['StaName']?></td>
                <td><?php echo $row['Time']?></td>
                <td><?php echo $row['StaName']?></td>
                <td><?php echo $row['Time']?></td>
            </tr>
            -->

        <?php
            }
        ?>

I have tried the PHP array but the printed info shows that PHP overlaps the departure station with arrival station data.我尝试过 PHP 数组,但打印的信息显示 PHP 将出发站与到达站数据重叠。 array shows overlapping Also, I tried the HTML table method, but the result is overlapping too.数组显示重叠另外,我尝试了 HTML table 方法,但结果也重叠了。

You could give alias at the sql stmt.您可以在 sql stmt 中提供别名。

SELECT ttype.TyName, train.TraNo, a.StaName as dstation, c.Time as dtime,b.StaName as astation , d.Time as atime

When retrieve, retrieve using检索时,检索使用

 <?php echo $row['dstation']?>
 <?php echo $row['dTime']?>
 <?php echo $row['astation']?>
 <?php echo $row['aTime']?>

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

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