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:
timetable

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):
[php]
CREATE TABLE IF NOT EXISTS timetbl (
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,
PRIMARY KEY (FlightNo),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
[/php]

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:
[php]
print("<table cellspacing="0" cellpadding="0" border="1">
<tr>
<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>
</tr>");

$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
$count++;

//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
print("<tr><td>&nbsp;</td><td>$Frequency</td><td>$DepTime</td><td>$ArrTime</td><td>$FlightNo</td></tr>");
}

print("</table>");
[/php]

Don’f forget to change MYSQL_DB_SERVER, MYSQL_DB_USER, MYSQL_DB_PASSWORD, MYSQL_DATABASE with yours configuration, e.g. (sequentially) 127.0.0.1, 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.

11 thoughts on “PHP Tutorial: 10 ways to make a traveler timetable

  1. Fantastic! Is it possible to put some more informations such as delay status, departed, arrived or canceled. and can we tracking this information through mobile device such as email,etc?
    Btw, if this program can link to person who has incoming flight schedule, it will be awesome. So the person who will board to the aircraft will receive notification through their mobile device such as text messege or email as a reminder likes for example 3 hours before.
    Thanks

    • Yes it’s possible but it’s already a big-scale system. I’d like to make such that things…
      Would you help me to develop the system, Micha (in the others word, De) ? 😛

  2. Nope, I am investor. I prefer to invest on your project instead of making it. I am not s/w guy. But I know alot of them. Just wanna see how creative and brilliant you are to make me may invest on your idea. keep going. 🙂

  3. this is the code i have put in the timetble.php file (xampp). can you pls help me what mistake im doing here. is the sql db username password correct? pls help. thanks


    < ?php print(" Frequency Dep. Time Arr. Time Flight No. “); $connection = mysql_connect(127.0.0.1, admin, admin); $database = mysql_select_db(“mydatabase”, $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 $count++; //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(“$Departure”); } 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(“ $Arrival”); } //10. Show the supporting information, Frequency, Departure Time, // Arrival Time, and Flight Number print(“ $Frequency$DepTime$ArrTime$FlightNo”); } print(“”); ?>

    • Hi Eccol,
      it’s seems you have a wrong character there:
      fixed this one “ with " and ’ with '
      Be careful when you copying the code, make sure that two symbol is right.

      Let me know if it’s work or any other question.
      Thanks.

  4. Hi sir. I am unable to run this code. can you pls help. i have corrected double quotes and single quotes. but im still getting wrong output. thanks

    print(”

     
    Frequency
    Dep. Time
    Arr. Time
    Flight No.
    “);

    <?php
    $connection = mysql_connect(127.0.0.1, admin, admin);
    $database = mysql_select_db("timetbl", $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
    $count++;

    //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("$Departure”);
    } 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(” $Arrival”);
    }

    //10. Show the supporting information, Frequency, Departure Time,
    // Arrival Time, and Flight Number
    print(” $Frequency$DepTime$ArrTime$FlightNo”);
    }
    print(“”);
    ?>

  5. hi im getting this error. are my start and end tags properly placed?
    thanks.

    print(“”); “); } 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(“”); } //10. Show the supporting information, Frequency, Departure Time, // Arrival Time, and Flight Number print(“”); } print(”
    Frequency Dep. Time Arr. Time Flight No.
    $Departure
    $Arrival
    $Frequency $DepTime $ArrTime $FlightNo
    “); ?>

    • Hi Eccol, I mean, what does your error said? not the code. Just like:

      Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in...
      Warning: mysql_select_db() expects parameter 2 to be resource, null given in...
      Fatal error: Call to undefined function mysql_selesct_db() in...

      etc…

      Did you get what I mean?
      Or, please check the code again, because your code doesn’t include any opening tag for php.

      Let me know it =) … Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

*