PHP Tutorial: 10 ways to make a traveler timetable

Hi there, today I wanna share something about PHP. And it’s about, how to joining the redundant header-text become one, like often used in timetable, usually in airlines for the flight or ship schedule. Look at the figure below:

Firstly, let’s make a database. Note the figure above, there are 6 variables, that is: Departure Place, Arrival Place, Frequency, Departure Time, Arrival Time, and Flight Number. Execute query below on your database (use your phpmyadmin):
FlightNo varchar(10) NOT NULL,
Departure varchar(20) NOT NULL,
Arrival varchar(20) NOT NULL,
DepTime varchar(5) NOT NULL,
ArrTime varchar(5) NOT NULL,
Frequency varchar(7) NOT NULL,

Insert some schedule to the database (look at the figure). Then, here we go! ^^

Get all of your data from ‘timetbl’ table, try this code:
print("<table cellspacing="0" cellpadding="0" border="1">
<td width="20px">&nbsp;</td>
<td width="50px">Frequency</td>
<td width="60px">Dep. Time</td>
<td width="60px">Arr. Time</td>
<td width="100px">Flight No.</td>

$connection = mysql_connect(MYSQL_DB_SERVER, MYSQL_DB_USER, MYSQL_DB_PASSWORD);
$database = mysql_select_db("MYSQL_DATABASE", $connection);

//2. getting data from the ‘timetbl’ table
// Note that it must order by Departure and Arrival,
// in order to make the algorithm more simple.
$getdata = "SELECT FlightNo, Departure, Arrival, DepTime, ArrTime, Frequency " .
"FROM timetbl ORDER BY Departure ASC, Arrival ASC, DepTime ASC";
$execute = mysql_query($getdata, $connection);

//3. create a counter for the data
$count = "0";

//4. move it to temporary variable
while($result = mysql_fetch_array($execute)) {
//5. Add the value of the counter as the data have been added

//The sequence of the number 0-5 accordance
//with the getdata query sequence above
$FlightNo = $result[0];
$Departure = $result[1];
$Arrival = $result[2];
$DepTime = $result[3];
$ArrTime = $result[4];
$Frequency = $result[5];

//6. Create a mark to show there is no redundant
// data for departure have been showed before
if($count == ‘1’) {
$dep_temp = $Departure;
$dep_prev = "";
} else {
$dep_prev = $dep_temp;
$dep_temp = $Departure;
//7. Print the departure information
if($Departure != $dep_prev) {
$count_dep = "0";
print("<tr><td colspan="5">$Departure</td></tr>");
} else $count_dep++;

//8. Create a mark to show there is no redundant
// data for arrival have been showed before
if($count == ‘1’ || $count_dep == "0") {
$arr_temp = $Arrival;
$arr_prev = "";
} else {
$arr_prev = $arr_temp;
$arr_temp = $Arrival;
//9. Print the arrival information
if($Arrival != $arr_temp || $Arrival != $arr_prev) {
print("<tr><td>&nbsp;</td><td colspan="4">$Arrival</td></tr>");

//10. Show the supporting information, Frequency, Departure Time,
// Arrival Time, and Flight Number


Don’f forget to change MYSQL_DB_SERVER, MYSQL_DB_USER, MYSQL_DB_PASSWORD, MYSQL_DATABASE with yours configuration, e.g. (sequentially), admin, admin, mydatabase. Or, you can create a new file, named config.php to separate the configuration from your page. You can add css file to the page. ^^

Finally, there is 10 ways to make a better timetable. If you have any questions, do not hesitate to comment on the box provide below or send me an e-mail. Okey? Hopefully this article useful. Cu next time in the next code.