CSCI 2006 - Spring 2024 - Server-Side ProgrammingLab #5 - PHP Classes

Lab #5 - PHP Classes

Purpose: Use Classes to implement aspects of a page

Instructions

  1. This lab will be partially reimplementing lab #3, but with an expansion towards the class model, as well as how we might engage with a database source. If you are not confident in your work from Lab #3, you may use the lab #3 solution as a starting point.
  2. Create two PHP files: "index.php" and "language.php"
  3. [language.php] This will only contain the "Language" class you are going to define
  4. [language.php] require or include the following file in your file: "/var/www/html/resources/csci2006data.php"
  5. [language.php] In the "Language" class, add the following functions:
  6. [language.php] __construct function, that takes a single parameter. This parameter is the "key" that identifies which language the class should represent.This function should call the function getCSCI2006Data that I have defined, in order to get the data for this particular language and should store the data in some class attribute(s) in order to reference the data in other functions. "getCSCI2006Data" takes up to two parameters. The first parameter is the "topic" for the dataset, in this case, that topic is "language". The second parameter is the "key" for which language data you want to retrieve. If nothing is provided for the second parameter, a list of key=>name pairs will be returned for all of the available languages
  7. [language.php] isDefined function, that looks at the data defined for this language instance and returns true if the data represents a language, and false if the data does not represent a language. The "getCSCI2006Data" function returns null if the "key" can not be found in the dataset
  8. [language.php] getName function that returns the name of the Language from the dataset
  9. [language.php] toHTML function that converts the language dataset into HTML following the same pattern of HTML from before. I recommend checking out the contents of of the data that is returned from "getCSCI2006Data" using the var_dump function
  10. [language.php] add a static class function called getAllLanguages that will return a list of key=>name pairs of the languages that are available. Remember that this list can be found by calling the "getCSCI2006Data" with the topic of "language" but with no second parameter.
  11. [index.php] require or include the language.php file you created
  12. [index.php] Create a new instance of the Language class using the query string parameter you have been using for your page declaration in the URL for the last few labs
  13. [index.php] Print out the HTML for the page, including the header and footer HTML. Note, you will need to call the various class methods you defined for language in order to know whether the page is invalid, and you need to print the 404 message; or to get the title of the page (via the language name); or to get the HTML body contents (via the toHTML method); or the list of pages for the navigation bar (via the static method you created -- note the syntax change for calling static methods of classes)
  14. Your site should look similar to the example site, but might have different extra languages
  15. Upload your PHP files (if you have not been doing that to test your code as you go), to the remote server. Please put them in the folder "public_html/csci2006/lab05/"
  16. In a web-browser, go to the URL below
  17. Ensure that your code is not producing any errors or warnings, by using the log-access tool. If you have trouble understanding the log messages, please email me for assistance. Note the log does not reset, so you need to look at when any errors/warnings occurred and which HTTP request they were for to better understand whether you have already fixed that issue

Submitting Instructions