PHP Programming Exercise: Basic Report Spring 2015
See the "Programming Examples" for help in answering questions 1 through 8.
1. Assign the pathname "item.txt" to the variable $fName.
2. Show how to issue an open for reading from the file $fName and assign
the return value to a file pointer named $fp. Include a clause to print
the message "File not found/opened" should there be an unsuccessful
attempt to open.
3. Construct a repetition structure that continues while the fgets function
can input a line of text from $fp and assign that string to $lineIn.
Count and print each line of input, then print the line count.
Given "item.txt" has an id in positions 0-4, name in 6-19, cost in 21-26 and
quantity in 28:
4. Use the function substr($fullStringName, $nameStart, $nameLength) to
separate the field components of $lineIn into $id, $name, $cost and $qty.
Then, print a line with the separate components.
5. Instead of the input file "item.txt" use "item.csv", with comma delimiters
between fields. Construct a repetition structure that continues while
fgetcsv($fp, [$delimiter]) inputs a line and stores the components as an
array named $item. Assign these array element values to $id, $name, $cost
and $qty (i.e. $id = $item[0] , $name = $item[1] ...).
6. Prior to the "loop" define and initialize the variable $totalCost.
Accumulate $totalCost from individual $cost amounts, then print $totalCost
after inputs have been processed.
7. Enhance your program with formatted report heading, column headings, and
and footing line with $totalCost (labeled; right-aligned).
8. Close the file.