Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and files BIS1523 – Lecture 15.

Similar presentations


Presentation on theme: "Arrays and files BIS1523 – Lecture 15."— Presentation transcript:

1 Arrays and files BIS1523 – Lecture 15

2 Why Files Rather Than Code
There are a number of reasons to store data in files, rather than just including the specific values directly in your code (often called hard-coding). Easier to Change: Changing a PHP program is trickier than just changing a text list. A programmer would need to be involved to make sure the syntax was correct on all accounts. Text lists can be changed easier. Centralization: If several pages need to use the same data, updating the data would now only need to be done in one place. So if there were several pizza-related systems, by having them reference the single file “toppings.txt”, updates are then applied to every system. Easier to Read: If you quickly need to see a list of toppings available ‘in your system’, viewing a text file is much more readable than viewing PHP source. Even other programs could be used to print out summaries, or update the information

3 Directories and Paths A file on your computer is referenced by its name, as well as its path. The Path describes which folders, and drive the file is located in. In windows, path has the form of: driveletter:\Folder1\Folder2\...\Filename Drive letter is usually C: For hard drives, and later letters for removable drives and/or network drives. So a path that looks like this: C:\Documents and Settings\Student\VB1733\hw01\hw01.vbproj would reference a file named hw01.vbproj, that was located in the hw01 folder, which itself was located in the VB1733 folder, etc.

4 Directories and Paths Unix/Linux based systems store files in a similar manner, with a few small differences The slashes between the file names go the other way, they are ‘forward’ slashes instead of backslashes There is normally not a drive letter involved. For example: /home/users/scanfield/public_html/UI The ~ character can also be used in a path, and that specifies a users ‘home’ folder as the starting point. You may have noticed this in the URL’s for the course web site: The path and file name part is: ~scanfield/UI/index.php

5 Files in PHP If you want to reference a file in PHP, you have 2 choices. You can either specify the full path name for the file, OR just the file name, if the file is in the same folder as your php script. This is the common way of doing things. Examples: $data = file_get_contents(“blah.txt”) File exists in the same folder as PHP $data = file_get_contents(“files/blah.txt”) There is a folder in the main folder called “files”, and blah.txt is in that folder $data = file_get_contents(“~scanfield/blah.txt”) Blah.txt exists in the user “scanfield”’s home folder

6 .. Another special set of characters can be used in referencing files, and that is the double dot (..) When included in a file name, it means “the containing” folder. $data = file_get_contents(“../blah.txt”) In the above example the file “blah.txt” would appear in the folder that contains the folder that contains your web page. If the file structure was similar to this: and your php script was in UI, then blah.txt would appear in public_html

7 Case Sensitivity Important note:
File names in unix/linux based systems (like our course web page) are CASE-SENSITIVE. This means that upper case and lower case are different, and you must use the correct names, including upper/lower case, exactly for your program to work. The file name “UI.txt” is different than “ui.txt”

8 Reading a File into an Array
The command to read a file into an array, is file(). The syntax is $arrayname = file(filename) The file will be read into the array, each row of the file will have a corresponding row in the array. The array will be numerically indexed. Example (toppings.txt): $toppings = file(“toppings.txt”); Sausage 1 Pepperoni 2 Ham 3 Bacon 4 Chicken 5 Pineapple 6 Mushrooms 7 Olives 8 Sun-Dried Tomatoes

9 Example: File Echo Example program that reads and displays a file.

10 Example Continued In the previous example, we just echo’d what was in the file on the screen. But our output is HTML, so we could actually use that information to build a form with checkboxes.

11 Example: HTML Generated

12 Another Example: States
Let’s examine an example with multiple points of information for each state. We start with a file that contains the name of every state: states.names.txt Displaying the names in a table.

13 States Output Output HTML

14 Area and Population Now, what if we had additional files (states.pop.txt, states.area.txt) that stored the population and area of each state. We specifically create the files so that they are in the exact same order. Then, the state name on row 1 matches the area on row 1 of the area file, and the population of row one in the population file

15 States & Pops To read in all 3 arrays, we use the file command three times This creates three arrays, where the data on the rows line up. The name at $states[1] matches the population at $pops[1] which matches the area at $areas[1] To print these out then, we need to access the row number. Thus we will have to use a normal for rather than a foreach.

16 Foreach To use a foreach loop, we would need to include the row number, to access the other arrays.

17 Ignoring New Lines The normal file() command includes new lines in the strings read. If you want to avoid that behavior, you can add a parameter to it to tell it to not include those. This is sometime useful when doing numeric comparisons to values you’ve read from arrays.

18 Output The HTML table output is in the same order as the original files. Borders and style were set by CSS

19 Reading A File into a String
The command to read a file into an string, is file_get_contents(). The syntax is $stringname = file_get_contents(filename) The file will be read into a single string variable. One way we could use this is to initialize the starting values of a <textarea>. Any text that appears between the start and end textarea tags is placed in the textarea.

20 Textarea Example Notice:
We specify the size of the textarea with rows and cols attributes. We also need to escape the double quotes with the \ character, so they are printed. The resulting HTML will look like:

21 Writing to a File We can write to a file in PHP using the file_put_contents() function. The syntax is: file_put_contents(filename, string_variable); The data should be a string, not an array If the file doesn’t exist, it will be created. The standard version of this command erases the file, and replaces it with the new contents that were in the string variable. To leave the current data in the file in place, use the form: file_put_contents(filename, string_variable, FILE_APPEND);

22 Textarea to file For example, we could retrieve the value of a textarea the user typed in, and save it to a file: So if the user had typed in this in the input form, it would save that list into the file:

23 Simple Text editor We can combine these 2 examples into a simple text editor. The single form loads the file into a textarea for editing. When the submit button is clicked, it calls the same form. The form also checks to see if the submit button was clicked, and if so, it reads the textarea, and saves it to the same file

24 Array to File There are 2 methods we could use to write an array to a file. We could step through the array one element at a time, and append each element to an existing file. We could use the implode function to create a single string out of the array, and use one file_put_contents call on that string.

25 Inline vs Block Elements
Elements in HTML can be displayed several different ways. The two basic ways are Inline vs Block. A block element is an element that takes up the full width available, and has a line break before and after it. Examples: <p>, <h1>, <div>, <section> An inline element only takes up as much width as necessary, and does not force line breaks. Examples: <span> <a>

26 Display example Examine the following displays: Basic Inline Block
The inline tags don’t really change the way it is displayed, because they are sized to automatically match the width of their contents.

27 Display in CSS You can over-ride the default display characteristic in CSS by setting the “display” property. For example, you could make <span> look like a block element by: CSS HTML Display

28 Inline Block Inline-block is a hybrid version of display that allows elements to be displayed side by side, but then also allows us to fix the width of an element. Notice that this allows us to create a ‘column’ style layout, without using absolute or relative positioning. If we wanted to vary the column widths, we would need to give each of the spans (or divs, or sections) an ID or Class

29 Example HTML CSS Results


Download ppt "Arrays and files BIS1523 – Lecture 15."

Similar presentations


Ads by Google