Laboratory 2: Introduction to PHP


The aim of this lab is for you to start modifying and writing simple PHP applications. Assistance will be given in labs.

Tasks

  1. (Re)read this week's lecture notes on PHP.
  2. Download the zipped archive details.zip containing the personal details application discussed in lectures onto your PC and unzip it.

    1. Study every line of the two documents until you understand its purpose.
    2. Don't be lazy, We really mean every line! Discuss with a neighbour. Quiz each other.
    3. Upload the resulting details directory to dwarf so you can run the application from your own file system there, using a URL of the form http://dwarf.ict.griffith.edu.au/~sxxxxxxx/wp/labs/lab2/details/get_details.html. Look at the URL used to call the PHP script show_details.php; it should have the form http://dwarf.ict.griffith.edu.au/~sxxxxxxx/wp/labs/lab2/details/show_details.html?name=Someone&age=21-30.
    4. Check that you can call the PHP script show_details.php directly by typing a URL with a nonempty query part, as indicated immediately above, without entering data into the form at all.
  3. Download the zipped archive factorise.zip containing the factorisation example discussed in lectures onto your PC, unzip it, and transfer the resulting directory to a directory .../wp/labs/lab2/factorise/ on dwarf, and test that it works.

    The next step is to edit this factoristion example.

    (Some members of the teaching team recommend the use of the NetBeans IDE to edit PHP (and Smarty) scripts. An useful feature of NetBeans is that it allows you to work on scripts that resides on a remote server. How this works is that you need to first specify the remote server address and directory. Then NetBeans makes a copy of the files in that directory to your local computer, and creates a NetBeans project. Whenever, you run the project, NetBeans will upload the files to the server, and run the scripts from the server. If you would like to use this handy feature, follow the link to Setup NetBeans Remote Connection. )

    Modify the script factorise.php so that it behaves like this improved factorisation example. In particular, note the following features of this improved example:

    • After doing (or attempting) each factorisation, a form for a new factorisation is displayed at the bottom of the same page.
    • After doing (or attempting) each factorisation, the form retains the value last entered in the input field, so it can be easily modified (or corrected).
    • If an empty, invalid (e.g., abc) or out-of-range (e.g., 1) number is entered, an appropriate error message is displayed on the same page instead of the factorisation.

    Hint 1. If the user input is invalid, instead of printing an error message immediately, assign the error message to a PHP variable, $error, say. Later, if the variable $error is not empty, compute the number's factors and print the factorisation as before; otherwise print the value of $error.

    Hint 2. To display the value of variable $number in the form's input field, use the value attribute as follows:

     <input type="text" name="number" value="<?= $number ?>" >
    
  4. Download the zipped archive assoc.zip containing the Prime Ministers database example discussed in lectures onto your PC, unzip it, and transfer the resulting directory to a directory .../wp/labs/lab2/assoc/ on dwarf, and test that it works. (The name assoc is used because information about each Prime Minister is stored in an associative array.)

    Modify the form in index.html so that it contains a single text field. Modify the PHP script results.php and the function search() in defs.php so that it takes a single query from the text field and shows all Prime Ministers whose name or year or state matches that query.

    Hint 3. Loop through the list of Prime Pinisters once only. Test whether each Prime Minister has a name or year or state that matches the given query, and if so add the Prime Minister to an initially empty result list. Finally, return the result list.

Remember to give every new directory permissions 705 and every new HTML or PHP file permissions 604.

Note that you can execute a PHP script script.php from the (dwarf) command line by typing "php script.php". This is very useful for debugging, particularly for finding PHP syntax errors.

Milestone

Demonstrate working PHP solutions to Exercises 3 and 4 above. All HTML used or generated by your solution should be valid HTML5.

Extension

If you have successfully submitted the milestone and would like to learn additional skills, further modify your solution to Exercise 3 by appending each factorisation (12 = 2 . 2 . 3) to a text file so you can display the list of all previous factorisations below the current one. This extension is not required.