Download presentation
Presentation is loading. Please wait.
1
In Class Program: Today in History
BIS1523 – Lecture 19
2
Today In History Todays program will allow the user to enter in a day, and month. Then, it will search through a file containing a list of events and the dates they happened on, and print out the results of that search.
3
Program Design This program is designed as a single page PHP.
Notice the POST action calls this same file We use option-lists to allow the user to make the selection, and then send them as month and day when the program posts.
4
Selecting Days The starting program only has the option for one day of selection. To expand that list, w need to have 31 different options That’s more typing that I’d like to do, so let’s automate it with some PHP and a loop.
5
Functions.php I’ve actually included that automation in a file called “functions.php”. And put it in a function called day_options() If we include that in our main program, we can have access to that function. Then, we do another PHP snippet to call the function. There is also a month_options function included.
6
Single Page Check Since we are using a single PHP page that calls itself, we need to make sure the month and day POST values were passed. If not, then we didn’t get here through the submit button, and we don’t need to do anything. We do that with an IF
7
The Trivia.Dat File The file ‘trivia.dat’ contains all our data on the events that happened throughout history. It is a ‘comma-separated-values’ file. Each line contains 4 pieces of information, separated by commas. The month, day, year, and an event that happened on that day.
8
Searching the File Searching the file is the normal algorithm we use.
Read the file into an array Use a loop to go through each element of the array Inside the loop, use an if statement to see if we found what we are looking for. Let’s start with the basic “read the file, loop through it” loop. At each iteration of the loop, the variable $row will be a row from the file.
9
Splitting up $row Now, $row is a string that contains 4 pieces of information. We need to split it up to access each individual piece. To do that, we can use the “explode” function. We tell it to explode it based on the comma, and it will create an array of pieces, breaking it up using the comma as the marker for each piece. So Becomes: Fields 1 2 1801 3 Guiseppe Piazzi …..
10
Finding the Specific Date
Now that we can access the day in $fields[1], and the month in $fields[2], we could print out everything that happened on January 1st by doing an if statement like: To search for the specific day the user entered, instead of using 1 and 1 for jan first, we need to use the day number and month number they selected. The day number is stored in the variable $day (we read it in from the $_POST)
11
Searching The month however was submitted using characters like “jan” and “feb”. The data in the file uses a number, so we need to convert it. There is a conversion function included in functions.php, that uses a switch to figure out which month is which.
12
Searching Using $day and our month function:
All that is left is to add a little more HTML to round out the output.
13
Finished Program
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.