0

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.

1 Answer 1

1

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']?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.