Class Work Get the examples programs to work Homework Use the file_exists function to test that the orders files exists in the page 67 version of process order Create an inventory file for use by processorder.php. Modify processorder.php so that it updates the contents of the file every time processorder successfully carries out a transaction. The inventory file will contain three lines – the first line is the number of tires on hand, second line is the number of units of oil on hand; the third line is the number of spark plugs on hand. Modify the program so that the price of each product as well as the number of items of the product are stored in the array. Write a program that adds and removes produces from the inventory file. Write one version of the program for an inventory file that contains no prices and one version for an inventory file that contains prices Chapter 2 Storing and Retrieving Data Saving Data For Later Why do you want to save data for later? Processing Files A sales transaction reads the order from the user, finds information about the item to be sold from a database, and updates the data base to reflect the purchase Opening a File Opening a File connects your program to the Operating System’s file manager. The File appears to the program as a File Name. The file manager uses the File Name to create a resource about by means of which the program can get detailed information about the file and view and modify the contents of the file File Mode A File Mode determines the operations that the program can apply to the file. See page 61. We will always open files as binary The fopen function $_SERVER[‘DOCUMENT_ROOT’] $_SERVER[‘DOCUMENT_ROOT’] makes possible the use of relative paths in your program What happens if you don’t specify a path? Doing this sometimes is the easiest way to programmatically manipulate files We will not open files through FTP or HTTP Problems with open files We will not have problems with file permissions because the host and the client are on the same computer and because we are running our programs as administrators. The error suppression symbol on page 64 suppresses displays of error messages. We test the file resource to find out if the operation succeeded or failed Writing to a File fwrite See prototype on Page 66 File Formats You can create your own file formats. You will use this freedom when you start using MySQL. The example on page 66 uses tab characters to delimit data fields. The explode function can recover the fields from the output string Closing a file disconnects the program from the operating system; the program releases the resources that it required when it opened the file fclose Knowing When to Stop feof feof returns if a program has reached the end of a file. The file resource maintains a variable called a file pointer. Every time the program reads data from a file the read function add the number of bytes that it read to the file pointer. The program is at the end of the file when the file pointer equals the number of bytes in the file. Reading a Line at a Time:fgets, fgetss, and fgetcsv Reading a line at a time is a good idea when you are reading very large files and you don’t think you can read the whole file into memory fgets is good for reading lines that you are not going to format fgetcsv is good for reading lines that are made up of character delimited fields. fgetcsv stores the fields for a line in an array. Please explain the syntax diagram for fgetscv on page 71 Reading the Whole File:readfile, fpassthru, and file We are not too interested in readfile We are not too interested in fpassthru We like the file function. The file function returns the contents of the file as an array. Each line of the file is an element of the array Reading a Character:fgetc We could read the file character by character but then we would have to rebuild the data from the strings of characters. We do not like the fgetc function Reading an Arbitrary Length: fread We are not too interested in fread either. People usually read and write text files one line at a time or, if the text file is small, in one chunk Using Other Useful File Functions file_exists, filesize, unlink, rewind, fseek, ftell file_exists or something like it is very useful; filesize is not very useful; unlink may prove useful in the future; fseek and ftell are not very useful in variable line-length text files; in fixed line-length text files they are very useful Locking Files Locking is meaningless on our computers because no one else will try to access the files at the same time that we access them. File Locking manages concurrent access to resources. We will look at locks again when we get into MySQL. We will not be able to test file locking as long as we cannot put our files on a remote server. Doing It a Better Way: Database Management Systems Problems with Using Flat Files Our flat files will not grow too large; we can do simple linear searches of arrays that we build from flat files – this would be impossible for very large flat files; again we can do random processing of arrays How RDBMSs Solve These Problems We will worry about this when we get to our own RDBMS MySQL. Chapter 3 Using Arrays Arrays interest primarily because we will use them to store the contents of database records in memory What is an Array? An array is a collection of data that is stored contiguously in memory Numerically Index Arrays How do you create an array? See page 80 and page 81 Accessing Array Contents How do you access an array’s contents? You apply the array operator [] to the array name and the element index. An array consists of data that we access by a number called an index. If you want the first element of the array fred then you write fred[0]. Our book shows us how to create an initialized array. How do we create an initialized array? How do we increase or decrease the size of an array? Using Loops to Access the Array Arrays with Different Indices These are called associative arrays. The indices are strings called keys; the elements are of arbitrary type and they are called values Using Loops foreach operator The code on page 84 is very complex. The loop is doing a lot of work behind the scenes. It updates $element until it looks at every element in the array. The each function is called an iterator list operator Array Operators See page 85 Multidimensional Arrays PHP implements multidimensional arrays as arrays of arrays The array on page 86 has numeric indices. The value of $products[0][0] is ‘TIR’; the value of $products[0][2] is 100. Why? You can mix numeric indexed arrays and string indexed arrays Sorting Arrays We do not care about sorting arrays Using assort and ksort to Sort Arrays Sorting in Reverse Sorting Multidimensional Arrays User-Defined Sorts Reverse User Sorts Reordering Arrays We don’t care about reordering arrays Using shuffle We don’t care about using shuffle Using array_reverse We don’t care about using array reverse Loading Arrays from Files We do care about loading arrays from files The example in the book uses the file function to create an array of orders from the contents of a file and then echo the array to the standard output Please explain the count function on page 97 Please explain the explode function on page 98 Performing Other Array Manipulations Navigating Within an Array: each, current, reset, end, next, pos, and prev These are interesting functions. They below to a class of functions called iterators. We are not going to do too much with these functions Applying Any Function to Each Element in an Array: array_walk array_walk does not interest us very much Counting Elements in an Array: count, sizeof, and array_count_values Converting Arrays to Scalar Variables:extract extract is an interesting function but we will not use it very much Chapter 4 String Manipulation and Regular Expressions Do you remember the string concatenation operator? We don’t care very much about this chapter right now. We will come back to it later Formatting Strings Trimming Strings: chop, ltrim, rtrim, and trim Formatting Strings for Presentation Using HTML Formatting: The n12br function Formatting a String for Printing What is the difference between sprintf and printf? Please explain the conversion specification. See page 110 We will have much need of sprintf Changing the Case of a String strtoupper, strtolower, ucfirst, ucwords What is the difference between ucfirst and ucwords? Formatting Strings for Storage:addslashes and stripslashes These functions convert PHP strings to MySQL strings. MySQL cannot on its own distinguish between some control characters and some literal characters. We use an escape character \ to tell MySQL that the character that appears after the escape character is a literal character. Use addslashes to format the string for the database. Use stripslashes to remove the slashes magic_quotes_gpc is off for our system. We should find out how to turn it on Joining and Splitting Strings with String Functions Using explode, implode, and join Using strtok strok is a string iterator Using substr string substr(string string, int start[, length’) Comparing Strings strcmp, strcasecm, and strnatcmp, strnatcasecmp What is natural order of striings? Matching and Replacing Substrings with String Functions Finding Strings in Strings:strstr, strchr, strrchr, and stristr Finding the Position of a Substring:strops and strrpos Replacing Substrings:str_replace and substr_replace Introducing Regular Expressions Regular expression are very interesting but we will skip them for now. Chapter 2 What is a text file? What does it mean to open a file? How do you open a file in PHP? What is a File Mode? What is the significance of the document root directory? What is the significance of the parent directory of the document root directory? Why do you want to avoid putting absolute paths in your PHP programs? How do you read data from a file? How do you write data to a file? Why do you close files? How does the feof function work? How does PHP test for a file’s existence? How does PHP test for a file’s size? How does PHP delete a file? How does PHP navigate inside files? What is the file pointer? How does rewind work? How does fseek work? How does ftell work? What is a resource? How do you lock files? Why do you lock files? What problems are associated with using flat files? Chapter 3 What is an array? What is an array index? What is an associative array? What is the range function? What is the relation between loops and arrays? How does PHP assign initial values to arrays? What is the relation between arrays and files? Can you explain the array operators? What is multidimensional array? How does PHP sort arrays? How does PHP sort multidimensional arrays? How does PHP allow a programmer to apply custom sorts to arrays? How does the shuffle function work? How does the array_reverse function work? How does PHP load arrays from files? How does PHP uses the each, current, reset, end, next, pos , and prev functions? How does PHP use the array_walk function? How does PHP count the elements of an array? How does PHP convert arrays to scalars? Chapter 4 What does the term “trim” mean as it applies to string? How does PHP trim strings? What does the n12br function work? How does PHP format strings for printing? How does PHP change the case of a string? How does PHP use addslashes to format strings? How does PHP use stripslashes to format strings? How does PHP join strings? How does PHP split strings? How does PHP compare strings? How does PHP determine a string’s length? How does PHP match substrings? How does PHP replace sub strings? How does PHP search sub strings? What is a regular expression? How does PHP use regular expressions to match string patterns?