eg-259:homework:php-files
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
eg-259:homework:php-files [2008/12/05 14:43] – eechris | eg-259:homework:php-files [2011/05/05 17:36] (current) – [Additional Exercise] eechris | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Files and PHP ====== | ||
+ | Hints for Coursework 2, prepared by Dr C.P. Jobling for Examples Class on Monday 9th December 2008. | ||
+ | |||
+ | ===== Getting Started ===== | ||
+ | |||
+ | The starting point for the coursework is the // | ||
+ | <code html> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | " | ||
+ | <html xmlns=" | ||
+ | < | ||
+ | < | ||
+ | Song-o-Matic: | ||
+ | </ | ||
+ | </ | ||
+ | <!-- Write an XHTML document to create a form that collects | ||
+ | | ||
+ | | ||
+ | must call one CGI program when the form is submitted and | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | Song-o-Matic: | ||
+ | </ | ||
+ | < | ||
+ | Add your favourite song to the World Wide Web's database of favourite | ||
+ | | ||
+ | </ | ||
+ | < | ||
+ | id=" | ||
+ | < | ||
+ | < | ||
+ | <td> | ||
+ | <input type=" | ||
+ | | ||
+ | </td> | ||
+ | </tr> | ||
+ | < | ||
+ | <td> | ||
+ | <input type=" | ||
+ | | ||
+ | </td> | ||
+ | </tr> | ||
+ | < | ||
+ | <td> | ||
+ | <input type=" | ||
+ | | ||
+ | </td> | ||
+ | </tr> | ||
+ | </ | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | To convert this to PHP, simply rename the file to // | ||
+ | <code html> | ||
+ | <?xml version=" | ||
+ | </ | ||
+ | it says | ||
+ | <code php> | ||
+ | <?php echo("<? | ||
+ | </ | ||
+ | Also, you should remove the //value// attributes from each of the text fields. These were for the mock-up and your form will be getting real song information from its user. Copy your new file // | ||
+ | |||
+ | In the demo, I shall probably use Netbeans to create a new project and complete this step using copy and paste. | ||
+ | |||
+ | ===== Create a data file ===== | ||
+ | |||
+ | To create this example web application we shall be building up the songs listing part first. So, the next thing we need is a file format. Last time,. we agreed that it would be a text file, one song per line and we'd allow comments (lines starting with ''#'' | ||
+ | < | ||
+ | # A comment | ||
+ | # Format: Songtitle|Composer(s)|Artist or Group | ||
+ | Don't sleep in the subway darling!|Tony and Jackie Trent|Petula Clark | ||
+ | Help!|John Lennon and Paul McCartney|The Beatles | ||
+ | |||
+ | A Song|A Composer|An Artist or Group | ||
+ | </ | ||
+ | Save this to the project directory. If you are running a web server on Linux, make sure that the file is writable by the web server. There are a couple of ways to do this. The easiest way is to make the file writable by others: '' | ||
+ | ==== Create a File Reader/ | ||
+ | |||
+ | Opening a file for reading and displaying it is probably less challenging that extracting data from a web form and writing it into a file. So we'll start there. In this example, I will build up a file reader displayer in stages. This reflects my way of working, but it is not a bad way I think. We start small, building functionality step-by step and having working, testable code at each stage. I find that this approach works better than trying to plan a complete solution in advance and then struggling to implement it because there are too many unanswered questions, and opportunities for introducing coding errors. Also, I find that taking small steps gives me more of a sense of achievement and the impetus to keep going. | ||
+ | |||
+ | === Step 1: Open a file, and read it line by line === | ||
+ | |||
+ | Note we don't have to open the file, simply call '' | ||
+ | <code php> | ||
+ | <? | ||
+ | // Read file into an array (one element per line) | ||
+ | $lines = file(" | ||
+ | |||
+ | // print every line | ||
+ | $line_no = 1; | ||
+ | foreach ($lines as $line) { | ||
+ | print " | ||
+ | $line_no++; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | === Step 2: Extract the data in a line into an array === | ||
+ | |||
+ | For thsi step, we take the line data and //explode// it on the '' | ||
+ | |||
+ | <code php> | ||
+ | <?php | ||
+ | // Read file into an array (one element per line) | ||
+ | ... | ||
+ | |||
+ | // print every line | ||
+ | foreach($lines as $line) { | ||
+ | .... | ||
+ | // explode each line into records | ||
+ | $data = explode(" | ||
+ | print " | ||
+ | foreach($data as $value) { | ||
+ | print " | ||
+ | } | ||
+ | print "< | ||
+ | $line_no++; | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | |||
+ | === Step 3: Skip blank lines and comments === | ||
+ | |||
+ | To skip blank lines or lines that start with a comment we use a regular expression: | ||
+ | <code php> | ||
+ | <?php | ||
+ | // Read file into an array (one element per line) | ||
+ | ... | ||
+ | |||
+ | // print every line | ||
+ | foreach($lines as $line) { | ||
+ | // slip lines that start with a comment | ||
+ | // or empty lines | ||
+ | if (! preg_match("/ | ||
+ | // explode each line into records | ||
+ | ... | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | === Step 4: Extract array data into variables for printing === | ||
+ | |||
+ | Once you have the data exploded into an array, it is trivial to assign the values to variables: | ||
+ | <code php> | ||
+ | <?php | ||
+ | // Read file into an array (one element per line) | ||
+ | ... | ||
+ | |||
+ | // print every line | ||
+ | foreach($lines as $line) { | ||
+ | .... | ||
+ | // explode each line into records | ||
+ | // if data valid | ||
+ | $title = $data[0]; | ||
+ | $composer = $data[1]; | ||
+ | $artist = $data[2]; | ||
+ | print " | ||
+ | "< | ||
+ | "< | ||
+ | "< | ||
+ | "< | ||
+ | $line_no++; | ||
+ | ... | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | === Step 5: Format data as an HTML table === | ||
+ | |||
+ | The task is almost complete, simply surround the loop with a table to get the list output. | ||
+ | <code php> | ||
+ | <? | ||
+ | // Read file into an array (one element per line | ||
+ | ?> | ||
+ | <table border=" | ||
+ | < | ||
+ | <?php | ||
+ | // Operate on every line | ||
+ | foreach ($lines as $line) { | ||
+ | // Ignore any line that starts with a comment or empty lines | ||
+ | ... | ||
+ | // Extract data values | ||
+ | $data = explode(" | ||
+ | $title = $data[0]; | ||
+ | $composer = $data[1]; | ||
+ | $artist = $data[2]; | ||
+ | print "< | ||
+ | } | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | ==== Processing the web form data ==== | ||
+ | |||
+ | We have written code to read data in the format we have specified. Now we need to write some PHP to handle the form data and write it into a file. We do this in the //action// target of the form // | ||
+ | <code html> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | " | ||
+ | <html xmlns=" | ||
+ | < | ||
+ | < | ||
+ | Song-o-Matic: | ||
+ | </ | ||
+ | </ | ||
+ | <!-- Returned to the user as a receipt for their input --> | ||
+ | < | ||
+ | < | ||
+ | Song-o-Matic: | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | < | ||
+ | as performed by The Henry Mancini Orchestra to the Song-o-Matic database of the Web's favourite | ||
+ | | ||
+ | < | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Extraction of the data is easy: | ||
+ | <code php> | ||
+ | <? | ||
+ | $title = $_POST[" | ||
+ | $composer = $_POST[" | ||
+ | $artist_or_group = $_POST[" | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | So is adding the data to the output. Here's one way to do it:" . | ||
+ | <code php> | ||
+ | print "< | ||
+ | " | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | If you are a java programmer you will be suprised to see that writing the data is as simple as: | ||
+ | <code php> | ||
+ | <?php | ||
+ | $songs = fopen(" | ||
+ | fwrite($songs, | ||
+ | ?> | ||
+ | </ | ||
+ | |||
+ | On testing, the only likely problem will be with the file permissions: | ||
+ | |||
+ | In production, you would need to //lock// the file for exclusive access using '' | ||
+ | |||
+ | ===== Summary ===== | ||
+ | |||
+ | I have given you sufficient information here to make the implementation of the Course Work 2 exercise trivial. | ||
+ | ===== Additional Exercise ===== | ||
+ | |||
+ | |||
+ | - Use the ideas presented in the [[eg-259: | ||
+ | - Extend the code to allow comments that do not start at the start of the line: e.g. '' | ||
+ | - How could you deal with a comment symbol (hash ''#'' | ||
+ | - Try implementing the song database in PHP with MySQL. | ||
+ | |||
+ | |||
+ | ---- | ||
+ | |||
+ | [[eg-259: |