在php/html中的多列中显示单列mysql数据 [英] Display single column mysql data in multiple columns in php/html

查看:76
本文介绍了在php/html中的多列中显示单列mysql数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HTML页面中显示MySQL数据,其外观类似于下表.

I want to display MySQL data in HTML page, which looks something like the table below.

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

我已经确认SQL查询正确. 这是参考文献

I have confirmed that the SQL query is correct. 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
            }
        ?>

我尝试了PHP数组,但打印的信息显示PHP与出发站数据与出发站数据重叠. 数组显示重叠 另外,我尝试了HTML table方法,但是结果也是重叠的.

I have tried the PHP array but the printed info shows that PHP overlaps the departure station with arrival station data. array shows overlapping Also, I tried the HTML table method, but the result is overlapping too.

推荐答案

您可以在sql stmt处提供别名.

You could give alias at the sql stmt.

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

检索时,使用

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

这篇关于在php/html中的多列中显示单列mysql数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆