1 00:00:00,530 --> 00:00:03,200 If you've taken our basic PHP course or 2 00:00:03,200 --> 00:00:08,150 really done almost anything in PHP, you've already been reading files. 3 00:00:08,150 --> 00:00:13,347 They include and require statements read additional files into the current file. 4 00:00:13,347 --> 00:00:18,430 This is a great way to cut back and rewriting the same thing all the time. 5 00:00:18,430 --> 00:00:22,370 We can write things once and include them wherever we want. 6 00:00:22,370 --> 00:00:25,520 But this course is really about giving you control. 7 00:00:25,520 --> 00:00:28,680 We want to be able to do something with those files, 8 00:00:28,680 --> 00:00:31,460 besides just dumping them into the current file. 9 00:00:31,460 --> 00:00:36,390 So, let's take a look at some of the other options we have for reading files. 10 00:00:36,390 --> 00:00:39,080 We're going to create a form, and bring in country, and 11 00:00:39,080 --> 00:00:41,530 state drop-downs from some other files. 12 00:00:41,530 --> 00:00:46,830 If we click on the HTML countries.html, and view the page source. 13 00:00:48,270 --> 00:00:51,360 You can see that this is a list of drop down options. 14 00:00:51,360 --> 00:00:54,250 The other HTML files are formatted the same way, and 15 00:00:54,250 --> 00:00:55,840 you can browse each of them if you'd like. 16 00:01:00,786 --> 00:01:06,294 Back in work spaces create a new file name state_formed.php. 17 00:01:09,932 --> 00:01:13,098 Let's add our form tag and then create a country drop down. 18 00:01:20,490 --> 00:01:30,130 And then, our select. 19 00:01:44,396 --> 00:01:47,920 For country, we're just going to include our countries file. 20 00:01:59,939 --> 00:02:01,500 Let's view this in the browser. 21 00:02:11,040 --> 00:02:13,700 We can see that their drop down is populated. 22 00:02:13,700 --> 00:02:19,340 Let's go back and add a state drop down now using a little more control this time. 23 00:02:23,619 --> 00:02:26,200 We'll add a label for our state. 24 00:02:31,331 --> 00:02:32,300 And then our select. 25 00:02:47,822 --> 00:02:51,710 This time we'll start by reading the file with the fopen function. 26 00:02:53,510 --> 00:02:55,520 This function opens a connection to a file, 27 00:02:55,520 --> 00:03:00,080 and returns a file handler that we can use to connect to the file. 28 00:03:00,080 --> 00:03:04,470 This is the same kind of thing that we do when we set up a connection to a database. 29 00:03:04,470 --> 00:03:08,280 To open a connection, we first tell it, which file we want to open, and 30 00:03:08,280 --> 00:03:11,000 then tell it which mode we want to use. 31 00:03:11,000 --> 00:03:13,020 What are we doing with the file? 32 00:03:13,020 --> 00:03:13,670 Reading? 33 00:03:13,670 --> 00:03:14,470 Writing? 34 00:03:14,470 --> 00:03:15,360 Appending? 35 00:03:15,360 --> 00:03:18,410 Or even creating a file if it doesn’t exist. 36 00:03:18,410 --> 00:03:22,010 Check the notes associated with this video for mode options, and 37 00:03:22,010 --> 00:03:24,940 additional features of fopen. 38 00:03:24,940 --> 00:03:31,240 First we pass data/ html/states.html. 39 00:03:31,240 --> 00:03:35,200 And then, since we want to read the file, we use r as the mode. 40 00:03:36,540 --> 00:03:38,540 We'll wrap this in an F statement, so 41 00:03:38,540 --> 00:03:42,490 we can perform our actions only if we're actually able to open the file. 42 00:03:47,301 --> 00:03:56,750 We set our file handle equal to the fopen And now, we perform our actions. 43 00:03:56,750 --> 00:04:01,990 The first action I want to add within the statement is the action to close the file. 44 00:04:01,990 --> 00:04:09,410 Use fclose and pass the file handler, Before I close the file. 45 00:04:09,410 --> 00:04:14,560 I want to loop over each line, we can create any kind of loop we want. 46 00:04:14,560 --> 00:04:18,120 We could loop through only a specific number of lines, or 47 00:04:18,120 --> 00:04:20,580 until we find a certain character. 48 00:04:20,580 --> 00:04:23,430 I want to loop through all the lines in this file. 49 00:04:23,430 --> 00:04:25,840 So, I will use the end of file function. 50 00:04:27,920 --> 00:04:33,690 While not file, and a file, and our file handler. 51 00:04:38,996 --> 00:04:43,610 To read each line, we use that fgets function. 52 00:04:43,610 --> 00:04:48,030 This returns the current line as a string, and moves the pointer to the next line. 53 00:04:51,980 --> 00:04:54,280 We can now echo that line. 54 00:04:56,600 --> 00:04:58,140 Let's preview this in the browser again. 55 00:05:00,720 --> 00:05:02,720 This time we see the state drop down as well. 56 00:05:04,270 --> 00:05:07,820 But we haven't actually done anything more than include the file. 57 00:05:07,820 --> 00:05:11,840 So, let's say, that I want to default the selection to Oregon. 58 00:05:11,840 --> 00:05:14,610 Let's go back to Workspaces, and add some additional code. 59 00:05:20,801 --> 00:05:25,431 If the string position 60 00:05:25,431 --> 00:05:29,576 of line for Oregon. 61 00:05:34,080 --> 00:05:38,380 This will check if my line contains the string Oregon. 62 00:05:38,380 --> 00:05:42,180 If so, then I want to string replace. 63 00:05:43,867 --> 00:05:48,711 Oregon, with 64 00:05:48,711 --> 00:05:55,172 selected Oregon. 65 00:06:06,766 --> 00:06:08,950 Else, we'll echo the line. 66 00:06:10,600 --> 00:06:11,760 Let's check our browser again. 67 00:06:14,190 --> 00:06:17,030 This time Oregon is selected by default. 68 00:06:17,030 --> 00:06:21,549 We could make any comparisons, or manipulations we wanted to this line. 69 00:06:21,549 --> 00:06:24,430 But let's check out another function for reading files.