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
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.
info.txt
file to
obtain the film's" name, year, score.
<title><?= $name ?> - Rancid Tomatoes</title>
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);
“str” . $var . “str” // str catenation
$_REQUEST[“name”];
?film=vampire
, as in this URL:
https://mathlab.utsc.utoronto.ca/cscb20w16/bretsche/a2/fresh.php?film=vampireYour 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);
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) { };
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
$info = array('coffee', 'brown', 'caffeine'); // define individual variables to refer to the elements of array $info list($drink, $color, $power) = $info;
if (cond) { statements };
cond
expression
evaluates to true then perform the statements.
function my_func(parameters) { statements }