简体   繁体   中英

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.

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. 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. array shows overlapping Also, I tried the HTML table method, but the result is overlapping too.

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

When retrieve, retrieve using

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

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