CSCB20 Tutorial Mar 6

Objectives for this tutorial:

  1. Work on PHP related to assignment 2.
  2. PHP Coding Tips

  1. Asst 2 Part 2

    Today we're going to spend some time working with PHP, to give you ideas for how to approach part 2 of assignment 2.

    The idea for part 2 is to generalize your solution for part 1, so that rather than just displaying a nice Web page for one particular movie, you allow the user to choose from among a (predefined) set of movies, and using PHP, you'll show the user a nice Web page for their chosen movie.

    The raw material from which your PHP script will operate is comprised of a set of files, stored in a directory whose name is derived from a movie title; for example, the vampire directory contains these files:

    info.txt      overview.txt  review2.txt  review4.txt  review6.txt  review8.txt
    overview.png  review1.txt   review3.txt  review5.txt  review7.txt
    

    Let's consider each of these files based on their content, as detailed in the assignment handout, and see how they are related to the rendered Web page.

    OK, now how are you supposed to obtain a copy of these files? Linked to the handout is a reference to a a2.zip file; this is a compressed archive containing all the movie description files for part 2. Clicking on the link will download the files to your local machine. To extract the contents of the archive use the unzip command; on mathlab/cmslab (note this will extract into the directory where you run the command):

    unzip a2.zip
    

    Part 2 Structural Outline

    Your solution to Part 2 will be organized as a set of PHP definitions followed by HTML code. This HTML is carried over from Part 1, but with injections of PHP code to supply values relevant to the user-selected movie replacing fixed references to the vampire movie used in Part 1.

    PHP tasks:
    In your HTML code:
  2. PHP Coding Tips

    First, a word of general advice: develop your PHP code in small, incremental steps. PHP is relatively easy to learn and write, but when you combine PHP with a browser, it can be difficult to debug errors, since the browser shows only the HTML rendered by your PHP.

    Work on one feature at a time, and get that part working before moving on to the next. When you change your code, do so in small steps, testing after each change that you have not broken your code.

    Common errors when writing PHP code include forgetting to close a PHP code block with "?>" (you'll see an error about "unexpected $end"), and forgetting to use an "=" when you want to output a PHP expression value (the value won't appear in your output) or forgetting the ";".

    Let's recall some PHP constructs from class that you may find useful for Asst 2.

    $my_var; $my_primes=array(2,3,5,7,11);
    Declares variable $my_var. Declares and initializes array variable $my_primes. PHP variables always begin with the character "$". Like Python, PHP variables can have different types at different points in execution; arrays can have a mix of content types.

    “str” . $var . “str” // str catenation
    String catenation is denoted by a period (".") in PHP (not "+").

    $_REQUEST[“name”];
    The user will invoke your PHP script with a URL parameter, denoted by a string like ?film=vampire, as in this URL:
    https://mathlab.utsc.utoronto.ca/cscb20w16/bretsche/a2/fresh.php?film=vampire
    
    Your PHP code will use $_REQUEST to read the film parameter, and could assign it to a variable as in:
    $film = $_REQUEST["film"];
    $array = file(string);
    You need to be able to read the content of the various film-information files described above. file(string) returns the content of the string-named file as an array of lines. For example, to read the text of overview.txt into array $synopsis:
    $synopsis = file("overview.txt");
    
    $array = glob(string)
    glob(string) lets you pattern-match on filenames. PHP provides several different pattern-defining strings, including "*" which matches 0 or more characters, "?" which matches any single character, [abcd] which matches any single character listed between the brackets. For example, to get an array of the filenames review1.txt, review2.txt, ... in a directory, you could write:
    $reviews = glob("review*.txt");
    
    for (init; end; delta) { };
    A for loop initializes a control variable, gives a final value for the control to exit the loop, and indicates how the control changes on each iteration. For example:
    for ($i=0; $i<10; $i++) { statements which may reference $i }
    
    foreach ($array as $item) { };
    foreach is a simplified form of for loop that works on the elements of an array. Like the Python for element in list: structure, during the i'th iteration of the loop, the value of $item will be the element of the list indexed by i-1; for example, on the 2nd iteration, $item will be the $array entry at index 1.

    Consider the problem of reading multiple review files for Asst 2. To do that, glob() to get an array of filenames, foreach() to iterate through the glob() results, and file() to read the review file contents into the an array variable.

    list($a,$b,$c) = array
    Creates named variables to refer to the elements of an array. E.g.
    $info = array('coffee', 'brown', 'caffeine');
    
    // define individual variables to refer to the elements of array $info
    list($drink, $color, $power) = $info;
    
    if (cond) { statements };
    If the cond expression evaluates to true then perform the statements.

    function my_func(parameters) { statements }
    define a function, that would be called by it's name.
  3. Sample PHP Code

    overview.php provides a snippet of PHP code that follows the general pattern you'll use for your fresh.php in Part 2. The code is not complete, but the omitted parts (marked by "...") should be fairly straightforward to complete.