PHP Programming                                                      Spring 2015

                       Exercises: File Search and Update

0. Retrieve:
   Prepare a script for reading through a file and printing all of the records.
   This operation involves opening [i.e. fopen($fileName, $mode)] the file for
   input and returning a file pointer(e.g. $fp), utilizing an iteration control
   structure (loop) controlled by the outcome of an attempt to get a file string
   (e.g. fgets($fp), and with statement body containing statements to extract 
   each a field [e.g. use substr($string, $start, $length) ] for printing. In
   addition to the fields, print the record starting position [i.e. use a
   statement such as $pos = ftell($fp)] and record length [i.e. a statement 
   such as $len = strlen($recordVar)]. Note the program example RetrieveMoto.php
   performs this operation.

   
   
1. Search:
   Prepare a script for reading through a file to locate and print a record
   having a particular key value. This operation involves opening [i.e. fopen(
   $fileName, $mode) ] the file for input and returning a file pointer(e.g. $fp),
   utilizing an iteration control structure (loop) controlled by the outcome of
   an attempt to get a file string (e.g. fgets($fp), and with statement body
   containing statements to extract a record key [e.g. use substr($string, $start,
   $length) ], test whether that record key equals a search key value, and when
   true, echo or print( ) the record data. Afterwards, the file should be closed
   [i.e. fclose($fp)].
   Note: the program example SearchMoto.php performs this operation.

   

2. Update:
   Prepare a script for reading through a file to locate and update a record
   having a particular key value. This operation involves opening [i.e. fopen(
   $fileName, $mode) ] the file for input and returning a file pointer(e.g. $fp),
   computing the record starting position [$pos = ftell($fp)], utilizing an
   iteration control structure (loop) controlled by the outcome of an attempt
   to get a file string (e.g. fgets($fp), and with statement body containing
   statements to extract a record key [e.g. use substr($string,$start,$length)],
   test whether that record key equals a search key value, and when true,
   modify the record (fields) with substr_replace($string,$replacement,$start,
   $length), then rewrite the record. To rewrite in the original location
   requires seeking the record start [i.e. fseek($fp,$pos)] before replacing
   the record [fputs($fp,$string)]. Before the next iteration begins, recompute
   the record position [$pos = ftell($fp)]. After the loop terminates, the file
   should be closed [i.e. fclose($fp)]. The program example UpdateOneMoto.php
   performs this operation.
   
   

3. Modify UpdateOneMoto.php to assign record values posted from a form. The form
   is to have text box for the key, button for field to update, a replacement,
   value plus a submit button initiating the action to invoke the php code to
   access the relevant record, and replace the price(cost) value before 
   replacing the record in its original place relative to the file starting
   position. The program example UpdateMoto.php performs this operation.