1 00:00:00,000 --> 00:00:08,070 2 00:00:08,070 --> 00:00:10,430 >> R.J. AQUINO: Let's just get started. 3 00:00:10,430 --> 00:00:12,310 So this is Quiz 1. 4 00:00:12,310 --> 00:00:14,890 Here is some high-level information. 5 00:00:14,890 --> 00:00:19,915 The About page for the quiz is at this URL, no longer CS50.net, although that 6 00:00:19,915 --> 00:00:21,080 will still work. 7 00:00:21,080 --> 00:00:26,920 It's CS50.harvard.edu/quizzes/2013/1. 8 00:00:26,920 --> 00:00:31,070 It's the big About page, telling you where and when, namely next Wednesday 9 00:00:31,070 --> 00:00:32,100 in a bunch of rooms. 10 00:00:32,100 --> 00:00:36,120 And by next Wednesday, I mean two days from now. 11 00:00:36,120 --> 00:00:37,890 All this information is there. 12 00:00:37,890 --> 00:00:39,110 But it is cumulative. 13 00:00:39,110 --> 00:00:43,790 >> So everything from the first half of the year is potentially on the quiz, 14 00:00:43,790 --> 00:00:50,780 because you can't really do advanced things in C without if conditions and 15 00:00:50,780 --> 00:00:51,920 for loops and the like. 16 00:00:51,920 --> 00:00:55,580 But there will be an emphasis on the material covered since Quiz 0, 17 00:00:55,580 --> 00:00:59,570 starting with structs and File I/O. It's typically more 18 00:00:59,570 --> 00:01:01,620 challenging than Quiz 0. 19 00:01:01,620 --> 00:01:03,870 The average score is typically lower. 20 00:01:03,870 --> 00:01:05,980 Study hard. 21 00:01:05,980 --> 00:01:09,340 >> While you're studying, be sure to use CS50/discuss to post your questions 22 00:01:09,340 --> 00:01:10,830 and read other people's questions. 23 00:01:10,830 --> 00:01:13,550 So if you don't have any questions, log in and read 24 00:01:13,550 --> 00:01:14,580 your friends' questions. 25 00:01:14,580 --> 00:01:16,560 They're probably good questions. 26 00:01:16,560 --> 00:01:17,730 And take the practice quizzes. 27 00:01:17,730 --> 00:01:20,750 We've been giving quizzes for seven or eight years now. 28 00:01:20,750 --> 00:01:22,180 They're all online. 29 00:01:22,180 --> 00:01:25,540 Future questions are similar to old questions. 30 00:01:25,540 --> 00:01:26,550 That's how we make them. 31 00:01:26,550 --> 00:01:27,740 >> The quiz doesn't exist yet. 32 00:01:27,740 --> 00:01:28,670 None of us have seen it. 33 00:01:28,670 --> 00:01:32,496 But it will look like previous quizzes. 34 00:01:32,496 --> 00:01:36,500 For this review session, this is not an exhaustive list of topics. 35 00:01:36,500 --> 00:01:40,740 You can't just attend this and then be perfectly ready for the quiz. 36 00:01:40,740 --> 00:01:43,330 Otherwise, it wouldn't be that much of a quiz. 37 00:01:43,330 --> 00:01:46,270 And this is also not necessarily everything you need to know about any 38 00:01:46,270 --> 00:01:46,970 given topic. 39 00:01:46,970 --> 00:01:50,520 It's meant to expose you to the things we've covered, remind you what we 40 00:01:50,520 --> 00:01:53,070 covered, and the way in which we covered it. 41 00:01:53,070 --> 00:01:57,030 But you will have to go further and deeper when you study to double-check 42 00:01:57,030 --> 00:02:00,230 that you know everything about any given topic and that you've filled in 43 00:02:00,230 --> 00:02:03,320 all the corners that were covered in lecture. 44 00:02:03,320 --> 00:02:07,980 >> The quiz notes tell you to go to the scribe notes, watch lecture videos. 45 00:02:07,980 --> 00:02:10,155 That's a good way to make sure you've covered all your bases. 46 00:02:10,155 --> 00:02:12,670 47 00:02:12,670 --> 00:02:17,340 So getting started, when I made these slides, I tried to put where I found 48 00:02:17,340 --> 00:02:18,350 information. 49 00:02:18,350 --> 00:02:22,890 So for File I/O, for instance, Week 7, Monday's lecture, and the posted 50 00:02:22,890 --> 00:02:27,960 Section 6 and Problem Set all have information about File I/O. I've done 51 00:02:27,960 --> 00:02:28,840 this for every topic. 52 00:02:28,840 --> 00:02:33,010 So those title slides may be helpful to you. 53 00:02:33,010 --> 00:02:38,950 >> So here we have File I/O. Remember, in Problem Set 5, we used fopen, fclose, 54 00:02:38,950 --> 00:02:41,210 fwrite, fread, and fseek. 55 00:02:41,210 --> 00:02:48,090 Having recovered 30ish JPEGs and having resized and messed with 56 00:02:48,090 --> 00:02:50,320 bitmaps, you should be pretty familiar with these functions 57 00:02:50,320 --> 00:02:51,830 and how they work. 58 00:02:51,830 --> 00:02:54,420 If you are no longer familiar, definitely review them. 59 00:02:54,420 --> 00:02:56,860 And make sure you understand what the different arguments are, 60 00:02:56,860 --> 00:02:58,260 when they're used. 61 00:02:58,260 --> 00:03:03,620 >> But the common file-related bugs you may be asked about-- 62 00:03:03,620 --> 00:03:07,270 well, if you've forgotten to check if fopen actually worked before you go to 63 00:03:07,270 --> 00:03:08,350 modify a file. 64 00:03:08,350 --> 00:03:09,760 That could be bad. 65 00:03:09,760 --> 00:03:13,560 If you've forgotten to fclose a file that you've fopened, that's similar to 66 00:03:13,560 --> 00:03:14,400 a memory leak. 67 00:03:14,400 --> 00:03:15,980 That's pretty bad. 68 00:03:15,980 --> 00:03:18,670 And forgetting to check if you've reached the end of the file before you 69 00:03:18,670 --> 00:03:19,790 start writing to it. 70 00:03:19,790 --> 00:03:22,320 >> So if you say, hey, I'm at the end of the file. 71 00:03:22,320 --> 00:03:23,750 Give me 5 more bytes. 72 00:03:23,750 --> 00:03:27,370 Well, that's probably not going to work out the way you expect. 73 00:03:27,370 --> 00:03:30,930 That's really it for File I/O, because we did so much of it 74 00:03:30,930 --> 00:03:32,300 with the problem set. 75 00:03:32,300 --> 00:03:36,000 So if you understood what was going on in Problem Set 5, remember the bitmats 76 00:03:36,000 --> 00:03:40,090 and the JPEGs, then you're probably all set for File I/O. If that's a bit 77 00:03:40,090 --> 00:03:44,770 fuzzy, definitely review that problem set and the associated material. 78 00:03:44,770 --> 00:03:51,110 >> Structs were the topic that were on the line between Quiz 0 and Quiz 1. 79 00:03:51,110 --> 00:03:53,090 Didn't quite make the cut for Quiz 0. 80 00:03:53,090 --> 00:03:57,040 So they'll definitely be on Quiz 1, Week 7, Monday. 81 00:03:57,040 --> 00:03:58,150 What is a struct? 82 00:03:58,150 --> 00:04:00,250 Here we show a struct. 83 00:04:00,250 --> 00:04:03,140 It's like a new type. 84 00:04:03,140 --> 00:04:07,940 It's like a container for multiple fields. 85 00:04:07,940 --> 00:04:12,970 >> In this case, we've declared a struct student that has two fields-- 86 00:04:12,970 --> 00:04:17,750 a string that we're calling name and an int that we're calling age. 87 00:04:17,750 --> 00:04:21,450 So when I pass around students or I modify students, I'll be able to 88 00:04:21,450 --> 00:04:24,430 access their name and their age. 89 00:04:24,430 --> 00:04:26,670 Let's look at some code for that. 90 00:04:26,670 --> 00:04:29,090 Here we see that I've declared a student s, just like 91 00:04:29,090 --> 00:04:30,300 I declare any variable-- 92 00:04:30,300 --> 00:04:32,430 int x, int y, et cetera. 93 00:04:32,430 --> 00:04:34,180 >> Here's student s. 94 00:04:34,180 --> 00:04:37,370 He starts with nothing in his fields. 95 00:04:37,370 --> 00:04:38,240 So let's set them. 96 00:04:38,240 --> 00:04:40,681 You set fields of a struct with dot. 97 00:04:40,681 --> 00:04:43,780 So I've said here that s.name = RJ. 98 00:04:43,780 --> 00:04:46,470 And s.age = 21. 99 00:04:46,470 --> 00:04:48,500 You can also update fields the same way you'd update 100 00:04:48,500 --> 00:04:49,550 the value of a variable. 101 00:04:49,550 --> 00:04:53,270 So I want to change my name from RJ with no periods to R.J. spelled the 102 00:04:53,270 --> 00:04:54,540 correct way. 103 00:04:54,540 --> 00:04:58,890 It would be s.name = RJ, the same as we said it originally. 104 00:04:58,890 --> 00:05:00,030 And then you can access them. 105 00:05:00,030 --> 00:05:00,930 >> So we've set them. 106 00:05:00,930 --> 00:05:01,840 We've updated them. 107 00:05:01,840 --> 00:05:03,890 You can also access them in the very same way. 108 00:05:03,890 --> 00:05:09,330 So here, I'm printing out R.J. Is 21 years old. 109 00:05:09,330 --> 00:05:14,700 And I'm accessing those values with s.name and s.age. 110 00:05:14,700 --> 00:05:17,040 So that's accessing structs with the dot notation. 111 00:05:17,040 --> 00:05:17,850 Yep, question? 112 00:05:17,850 --> 00:05:21,176 >> AUDIENCE: Is there a reason on the previous slide that you didn't put 113 00:05:21,176 --> 00:05:24,848 student on the top line, like typedef struct student and then 114 00:05:24,848 --> 00:05:25,840 student at the end? 115 00:05:25,840 --> 00:05:29,040 >> R.J. AQUINO: So the question was, on this slide, we've typically seen 116 00:05:29,040 --> 00:05:32,400 typedef struct node and then the fields of the struct and 117 00:05:32,400 --> 00:05:34,250 then the word node. 118 00:05:34,250 --> 00:05:37,790 And how come here I didn't say, typedef struct student and then the 119 00:05:37,790 --> 00:05:39,820 fields of the struct and then student? 120 00:05:39,820 --> 00:05:44,310 The reason is that I don't need to access it inside of the struct. 121 00:05:44,310 --> 00:05:46,270 So it's OK to leave it without a name. 122 00:05:46,270 --> 00:05:49,210 I can just leave it as an anonymous struct. 123 00:05:49,210 --> 00:05:53,130 >> The reason we do it for linked lists and things is because inside you need 124 00:05:53,130 --> 00:05:55,360 to reference a struct nodes star. 125 00:05:55,360 --> 00:05:58,220 So the struct has to have a name, so you can access it later. 126 00:05:58,220 --> 00:05:59,540 It's a minor detail. 127 00:05:59,540 --> 00:06:04,750 But you'll typically see typedef struct curly braces if you don't need 128 00:06:04,750 --> 00:06:08,720 the name and typedef struct some name followed by curly braces if you will 129 00:06:08,720 --> 00:06:09,520 need the name. 130 00:06:09,520 --> 00:06:12,070 So that's a good question. 131 00:06:12,070 --> 00:06:17,000 >> And on that point, we tend to modify structs and pass around structs by 132 00:06:17,000 --> 00:06:18,680 reference, not by value. 133 00:06:18,680 --> 00:06:21,940 So we'll just pass around pointers to structs instead of passing around the 134 00:06:21,940 --> 00:06:23,150 structs themselves. 135 00:06:23,150 --> 00:06:28,050 So you're very frequently going to be using, in this case, student* or 136 00:06:28,050 --> 00:06:34,150 struct node* or node* instead out students or nodes. 137 00:06:34,150 --> 00:06:39,350 So here, I've said, OK, the variable ptr is going to be the address of s. 138 00:06:39,350 --> 00:06:45,570 It's going to be the pointer to the student R.J. 139 00:06:45,570 --> 00:06:48,965 >> So we can get at those fields the same as we get anything. 140 00:06:48,965 --> 00:06:51,460 First, ID reference the pointer to get the struct. 141 00:06:51,460 --> 00:06:55,530 That's *ptr and then a dot and then age. 142 00:06:55,530 --> 00:06:58,790 So to access the field, and I've updated it now to 22, because, let's 143 00:06:58,790 --> 00:07:00,860 say, it was my birthday. 144 00:07:00,860 --> 00:07:03,990 There's a shortcut syntax using the arrow here. 145 00:07:03,990 --> 00:07:07,060 So ptr arrow age is just the same as *ptr.age. 146 00:07:07,060 --> 00:07:10,150 147 00:07:10,150 --> 00:07:11,550 Now, that's something you'll have to memorize and remember. 148 00:07:11,550 --> 00:07:15,010 >> You used it a lot in pset6, the speller pset. 149 00:07:15,010 --> 00:07:18,350 But this is actually what's going on underneath the hood. 150 00:07:18,350 --> 00:07:20,500 It's dereferencing the pointer and then accessing it. 151 00:07:20,500 --> 00:07:21,432 Question? 152 00:07:21,432 --> 00:07:22,682 >> AUDIENCE: [INAUDIBLE]. 153 00:07:22,682 --> 00:07:25,860 154 00:07:25,860 --> 00:07:28,060 >> R.J. AQUINO: So why are we using pointers as structs instead of the 155 00:07:28,060 --> 00:07:29,500 structs themselves? 156 00:07:29,500 --> 00:07:33,740 The reason would be if you're passing a struct to a function, you probably 157 00:07:33,740 --> 00:07:36,900 want to pass around just the 4 or so bytes that represent the pointer, as 158 00:07:36,900 --> 00:07:40,375 opposed to the potentially 30 or 40 bytes that are the struct. 159 00:07:40,375 --> 00:07:44,410 So passing something to a function is easier when the thing 160 00:07:44,410 --> 00:07:48,230 is smaller in short. 161 00:07:48,230 --> 00:07:49,074 Question? 162 00:07:49,074 --> 00:07:53,026 >> AUDIENCE: You might have mentioned this in the beginning, but are there 163 00:07:53,026 --> 00:07:56,000 other slides up on [INAUDIBLE]? 164 00:07:56,000 --> 00:07:58,960 >> R.J. AQUINO: These slides will be up after the review session. 165 00:07:58,960 --> 00:08:00,210 We'll post them on the website. 166 00:08:00,210 --> 00:08:02,450 167 00:08:02,450 --> 00:08:06,870 So moving on and moving on slightly faster, we're going to talk about data 168 00:08:06,870 --> 00:08:07,350 structures. 169 00:08:07,350 --> 00:08:08,040 There are a lot. 170 00:08:08,040 --> 00:08:10,080 We covered a bunch of them. 171 00:08:10,080 --> 00:08:12,500 Here's what you should understand about data structure. 172 00:08:12,500 --> 00:08:15,590 >> You should really understand at a high level what each structure is. 173 00:08:15,590 --> 00:08:21,190 Can you explain in English to your friend that hasn't taken CS50 how 174 00:08:21,190 --> 00:08:25,580 we're organizing our data and why we'd be using something in this way? 175 00:08:25,580 --> 00:08:26,990 That's thing one. 176 00:08:26,990 --> 00:08:29,650 Thing two, understand the implementation. 177 00:08:29,650 --> 00:08:34,270 So understand how to use these things in C. And we'll be going over this. 178 00:08:34,270 --> 00:08:39,030 >> And then thing three would be know the run times and the limitations of the 179 00:08:39,030 --> 00:08:40,470 various structures you're using. 180 00:08:40,470 --> 00:08:44,059 So understand why you would use a hash table instead of an array. 181 00:08:44,059 --> 00:08:49,570 Understand how fast, on average, accessing a hash table is. 182 00:08:49,570 --> 00:08:54,010 Understand what operations are fast on linked list but slow on arrays and 183 00:08:54,010 --> 00:08:56,080 vice versa. 184 00:08:56,080 --> 00:08:59,780 So to understand that, you'll have to understand Big-O notation just to know 185 00:08:59,780 --> 00:09:01,310 how to talk about these sorts of things. 186 00:09:01,310 --> 00:09:02,700 And we'll talk about that. 187 00:09:02,700 --> 00:09:06,040 >> So first thing, linked lists. 188 00:09:06,040 --> 00:09:07,770 Here is a high-level picture of a linked list. 189 00:09:07,770 --> 00:09:08,830 We show this in class. 190 00:09:08,830 --> 00:09:11,670 We typically have 10 people standing on the stage. 191 00:09:11,670 --> 00:09:16,790 But we have a series of nodes where each node has some value and a pointer 192 00:09:16,790 --> 00:09:18,610 to its next value. 193 00:09:18,610 --> 00:09:21,730 So to get from one node to the next, you just say, give me the next node. 194 00:09:21,730 --> 00:09:22,530 You have that node. 195 00:09:22,530 --> 00:09:23,770 Give me the next node. 196 00:09:23,770 --> 00:09:24,400 You have that node. 197 00:09:24,400 --> 00:09:28,790 Give me the next node and so on until there's no node left. 198 00:09:28,790 --> 00:09:31,850 >> So continue to talk about it at a high level. 199 00:09:31,850 --> 00:09:34,100 It's very easy to insert things into a linked list. 200 00:09:34,100 --> 00:09:36,010 If you don't care about the order, you can just drop it 201 00:09:36,010 --> 00:09:36,840 right at the beginning. 202 00:09:36,840 --> 00:09:38,520 That's constant time. 203 00:09:38,520 --> 00:09:39,900 But it's hard to find a value. 204 00:09:39,900 --> 00:09:43,060 If you're trying to ask, is seven in my list? 205 00:09:43,060 --> 00:09:44,740 You have to go through every single value. 206 00:09:44,740 --> 00:09:45,680 Is this seven? 207 00:09:45,680 --> 00:09:46,610 Is this seven? 208 00:09:46,610 --> 00:09:47,770 Is this seven? 209 00:09:47,770 --> 00:09:48,690 Is this seven? 210 00:09:48,690 --> 00:09:49,830 Over and over again. 211 00:09:49,830 --> 00:09:51,520 And that's O(n). 212 00:09:51,520 --> 00:09:53,800 So when studying for the quiz, compare this with arrays. 213 00:09:53,800 --> 00:09:57,010 214 00:09:57,010 --> 00:09:58,220 >> Is that OK? 215 00:09:58,220 --> 00:09:59,220 The lights went dim. 216 00:09:59,220 --> 00:10:01,110 OK. 217 00:10:01,110 --> 00:10:02,400 When is a linked list better? 218 00:10:02,400 --> 00:10:04,640 When is an array better? 219 00:10:04,640 --> 00:10:06,670 So let's look at some code. 220 00:10:06,670 --> 00:10:08,960 Here is a potential node. 221 00:10:08,960 --> 00:10:09,770 It's a struct. 222 00:10:09,770 --> 00:10:12,220 It has an int n, which will be our value. 223 00:10:12,220 --> 00:10:17,780 And it has a struct node* next, which is our pointer to the next node. 224 00:10:17,780 --> 00:10:23,040 So here, we can see that we happened to have put an int in our node. 225 00:10:23,040 --> 00:10:27,850 But if this were a linked list of char stars or a linked list of floats, we 226 00:10:27,850 --> 00:10:28,820 totally could do that too. 227 00:10:28,820 --> 00:10:33,110 >> Remember in pset6, you probably had a linked list of char stars or just 228 00:10:33,110 --> 00:10:34,360 static char arrays. 229 00:10:34,360 --> 00:10:37,370 230 00:10:37,370 --> 00:10:39,040 Let's look here at an operation. 231 00:10:39,040 --> 00:10:43,570 So we want to insert a new n into our linked list. 232 00:10:43,570 --> 00:10:48,940 We start out with a head pointer that is a pointer to this node that has the 233 00:10:48,940 --> 00:10:53,460 value of n and a next of a pointer that points to this node is a value of 234 00:10:53,460 --> 00:10:57,760 n and a next of null, because it's the last node. 235 00:10:57,760 --> 00:11:00,440 So in the interest of time, I will put all the code on the screen. 236 00:11:00,440 --> 00:11:03,130 And we'll walk through it a few lines at a time. 237 00:11:03,130 --> 00:11:05,790 >> So here's the code. 238 00:11:05,790 --> 00:11:07,560 I hope it's readable. 239 00:11:07,560 --> 00:11:11,275 The first thing we do is we malloc a new node. 240 00:11:11,275 --> 00:11:15,105 So it makes a pointer to a new node that doesn't quite have anything set 241 00:11:15,105 --> 00:11:16,450 up in it yet. 242 00:11:16,450 --> 00:11:19,580 We check to make sure that the new node isn't null. 243 00:11:19,580 --> 00:11:22,220 Otherwise, we have to give up. 244 00:11:22,220 --> 00:11:27,680 So having checked that, we now set the values in the node. 245 00:11:27,680 --> 00:11:31,520 So we put the new n into our n field. 246 00:11:31,520 --> 00:11:36,050 And we set the next pointer to point to the original head, so that we can 247 00:11:36,050 --> 00:11:38,900 now have inserted this node into our list. 248 00:11:38,900 --> 00:11:44,600 >> Finally, we have the global head point to our new node, so that if we were to 249 00:11:44,600 --> 00:11:50,300 start at head, we would be at this new first node instead of 250 00:11:50,300 --> 00:11:51,560 the old first node. 251 00:11:51,560 --> 00:11:55,320 And when this function exits, the variable new node no longer exists, 252 00:11:55,320 --> 00:11:57,130 because it was local to the function. 253 00:11:57,130 --> 00:11:59,770 So this is the state of the world. 254 00:11:59,770 --> 00:12:03,570 Our global head points to our new first node, which points to our 255 00:12:03,570 --> 00:12:06,346 original first node, which points to the node after that. 256 00:12:06,346 --> 00:12:09,790 >> That was insertion. 257 00:12:09,790 --> 00:12:12,150 I hope that was relatively straightforward to follow. 258 00:12:12,150 --> 00:12:14,300 When in doubt, draw a picture. 259 00:12:14,300 --> 00:12:17,820 So I find that talking about linked lists and looking at 260 00:12:17,820 --> 00:12:19,870 code is very not helpful. 261 00:12:19,870 --> 00:12:23,790 Whereas looking at a picture of a linked list allows me to think, oh, so 262 00:12:23,790 --> 00:12:24,970 I have this node here. 263 00:12:24,970 --> 00:12:28,980 But if I update that pointer, it ends up disconnected. 264 00:12:28,980 --> 00:12:34,340 And I've forgotten where the node goes. 265 00:12:34,340 --> 00:12:35,390 And the code exits. 266 00:12:35,390 --> 00:12:37,830 And you have multiple nodes that are disconnected. 267 00:12:37,830 --> 00:12:39,970 And you don't end up with the list you want. 268 00:12:39,970 --> 00:12:43,320 >> So if you draw the picture and do it step by step, hopefully, you'll see 269 00:12:43,320 --> 00:12:46,840 the correct order of things in terms of updating the pointers to make sure 270 00:12:46,840 --> 00:12:48,830 that the list comes together. 271 00:12:48,830 --> 00:12:51,240 Insert is relatively straightforward. 272 00:12:51,240 --> 00:12:55,210 A more complicated one would be insertion into a sorted list. 273 00:12:55,210 --> 00:12:59,980 A more complicated function is delete and find, so looking through a list to 274 00:12:59,980 --> 00:13:03,030 see if something is there. 275 00:13:03,030 --> 00:13:07,220 Perhaps you did this in pset6 when you got into your hash table and you said, 276 00:13:07,220 --> 00:13:10,460 well, is the word apple in my linked list? 277 00:13:10,460 --> 00:13:11,440 >> So you may have already done this. 278 00:13:11,440 --> 00:13:15,530 But definitely, refresh your memory and try to reimplement find and 279 00:13:15,530 --> 00:13:19,150 reimplement delete for a linked list. 280 00:13:19,150 --> 00:13:22,850 Fun side note, there also doubly-linked lists, where you have 281 00:13:22,850 --> 00:13:27,490 pointers that point both forward and backward, so that you could go to the 282 00:13:27,490 --> 00:13:29,270 next node and to the previous node. 283 00:13:29,270 --> 00:13:33,860 And there was a question on last year's quiz of that type, talking 284 00:13:33,860 --> 00:13:36,190 about doubly-linked lists. 285 00:13:36,190 --> 00:13:40,070 >> Now, that's a structure that you're relatively familiar with, because most 286 00:13:40,070 --> 00:13:42,520 of you probably used them on pset6. 287 00:13:42,520 --> 00:13:45,680 Here's one that's a bit less familiar. 288 00:13:45,680 --> 00:13:51,020 As a side note, I think that Quiz 1 is primarily harder than Quiz 0, because 289 00:13:51,020 --> 00:13:54,600 the stuff you're doing, you have not done as much. 290 00:13:54,600 --> 00:13:58,100 To put that another way, for Quiz 0, you had written a lot of C. And we 291 00:13:58,100 --> 00:13:58,880 asked you about C. 292 00:13:58,880 --> 00:14:02,310 >> For Quiz 1, we're going to ask you about PHP and JavaScript, which you 293 00:14:02,310 --> 00:14:03,490 haven't written as much of. 294 00:14:03,490 --> 00:14:07,590 We're going to ask you about C-code that you haven't written as much of, 295 00:14:07,590 --> 00:14:09,130 this advanced C stuff. 296 00:14:09,130 --> 00:14:11,520 So definitely, practice the stuff we talked about in lecture that you 297 00:14:11,520 --> 00:14:15,260 didn't necessarily do on the problem set. 298 00:14:15,260 --> 00:14:17,870 >> Speaking of which, you haven't written a stack on a problem set. 299 00:14:17,870 --> 00:14:19,610 But it was in lecture. 300 00:14:19,610 --> 00:14:22,530 Here's the high-level picture of stacks that we show every year. 301 00:14:22,530 --> 00:14:26,180 It's the stack of trays in the Mather dining hall. 302 00:14:26,180 --> 00:14:30,570 At a high level, stacks are a last in, first out data structure. 303 00:14:30,570 --> 00:14:32,040 That means you're going to put things in-- 304 00:14:32,040 --> 00:14:36,400 1, 3, 7, 12, 14, negative 0. 305 00:14:36,400 --> 00:14:39,180 The one thing I couldn't have said-- negative 3, 0. 306 00:14:39,180 --> 00:14:40,250 You put all these things in. 307 00:14:40,250 --> 00:14:42,940 And the last one you put in is the first one that's going to come out. 308 00:14:42,940 --> 00:14:44,170 >> So you have two operations-- 309 00:14:44,170 --> 00:14:45,260 push and pop. 310 00:14:45,260 --> 00:14:49,180 All of the putting in that I was gesturing like this are push. 311 00:14:49,180 --> 00:14:52,020 And then when I reach in to grab something or reach on top to grab 312 00:14:52,020 --> 00:14:53,940 something, that's pop. 313 00:14:53,940 --> 00:14:55,540 So we're going to implement stacks. 314 00:14:55,540 --> 00:14:57,870 And we showed them in lecture using arrays. 315 00:14:57,870 --> 00:14:59,550 But you could do them using linked lists. 316 00:14:59,550 --> 00:15:01,770 A stack is a conceptual data structure, not like an 317 00:15:01,770 --> 00:15:03,760 implementation-specific one. 318 00:15:03,760 --> 00:15:06,420 >> So what would that look like? 319 00:15:06,420 --> 00:15:07,970 It would look kind of like this. 320 00:15:07,970 --> 00:15:10,840 You'd have an integer size. 321 00:15:10,840 --> 00:15:16,000 And you'd have an array of values that we're calling trays, because that's 322 00:15:16,000 --> 00:15:18,570 what the picture was for us-- int trays-- 323 00:15:18,570 --> 00:15:21,740 and then some maximum capacity. 324 00:15:21,740 --> 00:15:27,100 So what would push look like? 325 00:15:27,100 --> 00:15:33,250 Well, if we have a stack s, then to push something onto s, we would get 326 00:15:33,250 --> 00:15:34,620 the size of s. 327 00:15:34,620 --> 00:15:42,270 And that would be the next open spot of our array. 328 00:15:42,270 --> 00:15:50,510 So if we have three things in our stack, then trays 3 would be the next 329 00:15:50,510 --> 00:15:54,290 open spot, because 0, 1, and 2 are already filled up. 330 00:15:54,290 --> 00:16:01,790 >> So we put the value into s.trays[s.size], the third spot. 331 00:16:01,790 --> 00:16:06,290 And then we increment s.size to say, hey, we had three things before. 332 00:16:06,290 --> 00:16:07,400 Now, we have four. 333 00:16:07,400 --> 00:16:10,180 So the next time you push, you're going to put something into 4. 334 00:16:10,180 --> 00:16:15,560 Or the next time you pop, you're going to look at 3 instead of 4 or whatever. 335 00:16:15,560 --> 00:16:18,000 And then we'll return true to say, hey, we succeeded. 336 00:16:18,000 --> 00:16:19,160 This worked. 337 00:16:19,160 --> 00:16:22,060 As a rule of thumb, if a function that's supposed to return true or 338 00:16:22,060 --> 00:16:26,370 false always returns true, you may have done something wrong. 339 00:16:26,370 --> 00:16:28,350 So does this work? 340 00:16:28,350 --> 00:16:32,400 >> Well, it works fine for 1, and 2, and 3, and 4, and five. 341 00:16:32,400 --> 00:16:34,640 But let's say I reach my capacity. 342 00:16:34,640 --> 00:16:38,750 I've then run into a problem, because if size is the same as capacity, I'm 343 00:16:38,750 --> 00:16:43,340 now trying to put something into an array where I don't have space. 344 00:16:43,340 --> 00:16:46,980 So a short check to fix this. 345 00:16:46,980 --> 00:16:51,630 If s.size == CAPACITY, return false. 346 00:16:51,630 --> 00:16:54,130 Otherwise, go and do what we did. 347 00:16:54,130 --> 00:16:55,660 So what else could we ask about for stacks? 348 00:16:55,660 --> 00:16:56,460 What else should you study? 349 00:16:56,460 --> 00:16:57,690 What else should you practice? 350 00:16:57,690 --> 00:17:01,030 >> Well, implementing pop. 351 00:17:01,030 --> 00:17:02,370 We already did push. 352 00:17:02,370 --> 00:17:04,280 I'll fix that. 353 00:17:04,280 --> 00:17:08,180 A non-array implementation, where you use a linked list, perhaps. 354 00:17:08,180 --> 00:17:09,390 A non-int implementation. 355 00:17:09,390 --> 00:17:10,940 We did ints here. 356 00:17:10,940 --> 00:17:11,880 But it could have been floats. 357 00:17:11,880 --> 00:17:13,010 I could have been strings. 358 00:17:13,010 --> 00:17:14,290 It could have been char stars. 359 00:17:14,290 --> 00:17:17,960 Look at past quizzes for the kinds of questions we've asked about stacks. 360 00:17:17,960 --> 00:17:20,000 >> I'll say that we covered stacks around the same as we've covered them in 361 00:17:20,000 --> 00:17:20,540 years past. 362 00:17:20,540 --> 00:17:24,400 So the quiz questions should be a good indication. 363 00:17:24,400 --> 00:17:27,010 Moving forward even faster, queues. 364 00:17:27,010 --> 00:17:28,200 They're like stacks. 365 00:17:28,200 --> 00:17:29,960 But they're first in, first out. 366 00:17:29,960 --> 00:17:33,530 If you're British, the word queue probably made a lot of sense to you. 367 00:17:33,530 --> 00:17:36,390 Otherwise, you may have heard of it as a line. 368 00:17:36,390 --> 00:17:38,120 They work like the line at the Apple store. 369 00:17:38,120 --> 00:17:40,740 The first person to show up at 3:00 in the morning is the first 370 00:17:40,740 --> 00:17:42,880 person to buy his iPad. 371 00:17:42,880 --> 00:17:44,260 >> So we have two operations-- 372 00:17:44,260 --> 00:17:45,720 enqueue and dequeue. 373 00:17:45,720 --> 00:17:47,560 Enqueue puts someone in the line. 374 00:17:47,560 --> 00:17:50,070 Dequeue pulls the first person off the line. 375 00:17:50,070 --> 00:17:52,640 Again, we can implement this with an array. 376 00:17:52,640 --> 00:17:54,880 So what is the struct we showed in lecture? 377 00:17:54,880 --> 00:17:57,660 It was this one. 378 00:17:57,660 --> 00:17:59,300 Again, numbers. 379 00:17:59,300 --> 00:18:02,020 Again, size and this new thing front. 380 00:18:02,020 --> 00:18:04,880 Why is there something called front? 381 00:18:04,880 --> 00:18:07,050 It's the index of the next element to dequeue. 382 00:18:07,050 --> 00:18:12,210 It's just internally keeping track of the first guy to show up, so that we 383 00:18:12,210 --> 00:18:15,005 can pull it out when we need to. 384 00:18:15,005 --> 00:18:19,322 >> Definitely look at lecture notes and try to implement enqueue and dequeue 385 00:18:19,322 --> 00:18:21,700 when studying for the quiz. 386 00:18:21,700 --> 00:18:23,190 Important things to think about. 387 00:18:23,190 --> 00:18:27,260 Wrapping around if the front plus the size ends up bigger than capacity. 388 00:18:27,260 --> 00:18:32,670 Again, if your structure is full, you're going to have a problem. 389 00:18:32,670 --> 00:18:34,780 Hash tables you've seen before. 390 00:18:34,780 --> 00:18:39,820 Most of you probably implemented these on pset6. 391 00:18:39,820 --> 00:18:44,210 It's a structure that aims for O(1) constant time insertion and O(1) 392 00:18:44,210 --> 00:18:46,430 constant time lookup. 393 00:18:46,430 --> 00:18:49,760 >> In CS50, we implemented this as an array of linked lists. 394 00:18:49,760 --> 00:18:53,690 The key component to a hash table is the hash function. 395 00:18:53,690 --> 00:18:58,350 So it converts your input, let's say, a dictionary word, into a number, 396 00:18:58,350 --> 00:18:59,560 which is going to be our index. 397 00:18:59,560 --> 00:19:01,410 And we'll use that index into our array. 398 00:19:01,410 --> 00:19:05,374 So here's a cute little picture from study.50.net. 399 00:19:05,374 --> 00:19:08,060 We throw all the words into our hash function. 400 00:19:08,060 --> 00:19:10,950 And the hash function tells us where to put these words. 401 00:19:10,950 --> 00:19:15,650 >> This is all great in the land where there's only one word for every slot. 402 00:19:15,650 --> 00:19:20,480 But as you remember from pset6, there are more words than slots. 403 00:19:20,480 --> 00:19:23,080 So what happens when you get a collision? 404 00:19:23,080 --> 00:19:26,730 Instead of storing one value in, let's say, hash table 3, you 405 00:19:26,730 --> 00:19:27,990 store a linked list. 406 00:19:27,990 --> 00:19:32,900 And so instead of having cantaloupe here, you would have a linked list, 407 00:19:32,900 --> 00:19:34,190 where the first node is cantaloupe. 408 00:19:34,190 --> 00:19:35,260 And the next node is cat. 409 00:19:35,260 --> 00:19:38,970 And the third node is collision, let's say, because all these words start 410 00:19:38,970 --> 00:19:41,110 with C. 411 00:19:41,110 --> 00:19:42,700 >> So most of you did this for pset6. 412 00:19:42,700 --> 00:19:45,685 If you did not do a hash table on pset6 and you attempted something like 413 00:19:45,685 --> 00:19:47,720 a trie, definitely review hash tables. 414 00:19:47,720 --> 00:19:50,650 If you did do it on pset6, definitely review hash tables. 415 00:19:50,650 --> 00:19:53,610 And if you did it on pset6 and it didn't work out quite right and you 416 00:19:53,610 --> 00:19:56,150 had a lot of trouble with it, definitely review hash tables. 417 00:19:56,150 --> 00:20:01,610 So the lesson really is definitely review hash tables. 418 00:20:01,610 --> 00:20:07,130 >> The vast minority of you tried out tries on pset6. 419 00:20:07,130 --> 00:20:08,570 High-level picture. 420 00:20:08,570 --> 00:20:15,150 It's something like this, where each node has a set of children, where each 421 00:20:15,150 --> 00:20:17,100 child corresponds to a letter. 422 00:20:17,100 --> 00:20:20,520 And every node also says, hey, I am a word. 423 00:20:20,520 --> 00:20:25,933 So in this instance, the word Maxwell, if you follow the M to the A to the 424 00:20:25,933 --> 00:20:28,530 X-W-E-L-L and then follow it one more. 425 00:20:28,530 --> 00:20:32,800 And you get this symbol, delta, which we signify to mean this is a word. 426 00:20:32,800 --> 00:20:34,780 So Maxwell is a word. 427 00:20:34,780 --> 00:20:38,430 These deltas are throughout signifying which things are words and which 428 00:20:38,430 --> 00:20:40,360 things are not. 429 00:20:40,360 --> 00:20:46,400 >> So in pset6, the data we stored alongside any of our nodes was "I am a 430 00:20:46,400 --> 00:20:52,630 word." And the cool thing about tries is they demonstrate insertion and 431 00:20:52,630 --> 00:20:55,080 lookup in O(length of a word). 432 00:20:55,080 --> 00:20:59,450 So just to get through Maxwell, it's M-A-X-W-E-L-L. So seven or eight-- 433 00:20:59,450 --> 00:21:00,360 I can't count-- 434 00:21:00,360 --> 00:21:03,920 steps to get to the end and check things out. 435 00:21:03,920 --> 00:21:06,800 So quick implementation here. 436 00:21:06,800 --> 00:21:10,230 Rob went through a linked list in his postmortem. 437 00:21:10,230 --> 00:21:11,600 So check that out. 438 00:21:11,600 --> 00:21:11,720 Sorry. 439 00:21:11,720 --> 00:21:13,240 Went through a trie in his postmortem. 440 00:21:13,240 --> 00:21:14,260 So check that out. 441 00:21:14,260 --> 00:21:24,410 >> But you basically have each node has 27 pointers to the next nodes and one 442 00:21:24,410 --> 00:21:27,050 Boolean for am I a word. 443 00:21:27,050 --> 00:21:31,530 Check out Rob's postmortem for how this actually is implemented. 444 00:21:31,530 --> 00:21:34,750 Our final structure, our trees and binary search trees. 445 00:21:34,750 --> 00:21:41,530 So looking at these, these were covered most recently Week 8, Monday. 446 00:21:41,530 --> 00:21:46,960 A tree is similar to a trie, except you don't necessarily have 27 nodes at 447 00:21:46,960 --> 00:21:47,500 each point. 448 00:21:47,500 --> 00:21:52,820 And you don't have this data at each step that signifies whether the-- 449 00:21:52,820 --> 00:21:54,030 the path doesn't matter. 450 00:21:54,030 --> 00:22:00,870 Whereas a trie, the path from top to bottom, Maxwell, was important to us. 451 00:22:00,870 --> 00:22:05,270 >> But each node has multiple children, perhaps. 452 00:22:05,270 --> 00:22:07,290 We have some more vocabulary. 453 00:22:07,290 --> 00:22:09,530 The root of the tree is at the very top. 454 00:22:09,530 --> 00:22:12,520 And we say that the very bottommost nodes that have no 455 00:22:12,520 --> 00:22:14,530 children are leaves. 456 00:22:14,530 --> 00:22:18,040 So like a trie, a tree is a structure of nodes. 457 00:22:18,040 --> 00:22:21,490 A common type of tree that we're going to talk about is a binary tree, where 458 00:22:21,490 --> 00:22:26,040 each node has no children or one child or two children. 459 00:22:26,040 --> 00:22:28,890 So this picture here is not a binary tree, because 460 00:22:28,890 --> 00:22:32,890 node 3 has three children. 461 00:22:32,890 --> 00:22:38,140 >> But if we were to ignore those, the rest of it is a binary tree because it 462 00:22:38,140 --> 00:22:43,200 demonstrates the property that each node has zero, one, or two children. 463 00:22:43,200 --> 00:22:46,400 So how could we express this in code? 464 00:22:46,400 --> 00:22:51,460 We could have a node where each node has an integer inside of it, as well 465 00:22:51,460 --> 00:22:55,590 as a pointer to the tree on the left and a pointer to the tree on the 466 00:22:55,590 --> 00:22:59,510 right, so the two children. 467 00:22:59,510 --> 00:23:00,880 How is this useful? 468 00:23:00,880 --> 00:23:05,740 Well, if we make rules about where we put nodes, we can make search faster. 469 00:23:05,740 --> 00:23:10,630 >> So there's a concept of a binary search tree, where all nodes on the 470 00:23:10,630 --> 00:23:14,420 left subtree have a smaller value than the node we're looking at. 471 00:23:14,420 --> 00:23:17,880 And all nodes on the right subtree have a greater value 472 00:23:17,880 --> 00:23:18,660 than the root node. 473 00:23:18,660 --> 00:23:20,670 Now, that looks like a lot of words. 474 00:23:20,670 --> 00:23:23,770 I'm going to put it inside of double quotes and show you a picture. 475 00:23:23,770 --> 00:23:27,010 So here is an example of a binary search tree. 476 00:23:27,010 --> 00:23:28,770 See that we start with 10. 477 00:23:28,770 --> 00:23:31,780 Everything to the left of 10 is smaller than it. 478 00:23:31,780 --> 00:23:33,130 And everything to the right is bigger than it. 479 00:23:33,130 --> 00:23:37,620 >> But more so than that, each node in the tree expresses this property. 480 00:23:37,620 --> 00:23:42,110 So the node 7 has a 3 to the left and a 9 to the right. 481 00:23:42,110 --> 00:23:44,440 So all of those are smaller than 10. 482 00:23:44,440 --> 00:23:50,470 But looking at just those, the 7 has 3 to its left and 9 to its right. 483 00:23:50,470 --> 00:23:56,100 And similarly on the right, 15 has 14 to its left and 50 to its right. 484 00:23:56,100 --> 00:24:03,770 So the three nodes over there, 15, 14, and 50, are also a valid binary tree 485 00:24:03,770 --> 00:24:05,480 or a valid binary search tree. 486 00:24:05,480 --> 00:24:07,250 And they're all bigger than 10. 487 00:24:07,250 --> 00:24:08,960 So they are allowed to be on the right there. 488 00:24:08,960 --> 00:24:09,940 Is there a question? 489 00:24:09,940 --> 00:24:12,580 >> AUDIENCE: How do you deal when you have two sevens? 490 00:24:12,580 --> 00:24:12,850 >> R.J. AQUINO: Yeah. 491 00:24:12,850 --> 00:24:16,550 How do you deal with two values that are the same? 492 00:24:16,550 --> 00:24:21,465 Some binary search trees say that you ignore duplicates, because the goal is 493 00:24:21,465 --> 00:24:24,280 just to say, I've seen these things so far. 494 00:24:24,280 --> 00:24:28,120 Some binary search trees you could say have a count inside of the node. 495 00:24:28,120 --> 00:24:32,000 Others might say that everything to the left is less than or equal to. 496 00:24:32,000 --> 00:24:33,470 And everything to the right is greater than. 497 00:24:33,470 --> 00:24:36,520 >> It just depends on what the problem is you're solving. 498 00:24:36,520 --> 00:24:40,840 So in a dictionary, for instance, you wouldn't care about duplicates. 499 00:24:40,840 --> 00:24:41,490 You would throw them out. 500 00:24:41,490 --> 00:24:44,719 But some other problem you might care. 501 00:24:44,719 --> 00:24:49,242 >> AUDIENCE: Is it possible to have a 1 to the left of 15, which 502 00:24:49,242 --> 00:24:50,590 is less than 10? 503 00:24:50,590 --> 00:24:51,885 >> R.J. AQUINO: No. 504 00:24:51,885 --> 00:24:56,570 If the 14 here were a 1, this would not be a valid binary search tree, 505 00:24:56,570 --> 00:25:00,840 because everything to the right of 10 has to be bigger than it. 506 00:25:00,840 --> 00:25:02,300 And we'll see why. 507 00:25:02,300 --> 00:25:07,960 If in the land of search my goal is to find 14, I start at the root. 508 00:25:07,960 --> 00:25:08,500 So I look. 509 00:25:08,500 --> 00:25:08,710 OK. 510 00:25:08,710 --> 00:25:10,670 We're going to start at the root. 511 00:25:10,670 --> 00:25:12,500 Look at 10. 512 00:25:12,500 --> 00:25:16,050 >> Well, 14, our target, is bigger than 10. 513 00:25:16,050 --> 00:25:17,370 So it must be on the right. 514 00:25:17,370 --> 00:25:21,780 This is very similar to the whole phone book thing we did, the binary 515 00:25:21,780 --> 00:25:23,720 search there. 516 00:25:23,720 --> 00:25:26,430 But instead of binary searching in an array, we're binary 517 00:25:26,430 --> 00:25:28,490 searching in this tree. 518 00:25:28,490 --> 00:25:31,260 So we're still looking for 14. 519 00:25:31,260 --> 00:25:32,480 Well, 14 is smaller than 15. 520 00:25:32,480 --> 00:25:36,430 So if it's in our tree, it must be in this area here. 521 00:25:36,430 --> 00:25:39,680 It must be to the right of 10 and to the left of 15. 522 00:25:39,680 --> 00:25:42,250 >> And so we check this node. 523 00:25:42,250 --> 00:25:45,790 And yay, we've found 14. 524 00:25:45,790 --> 00:25:46,760 I'm not going to walk through it. 525 00:25:46,760 --> 00:25:48,090 But here's the code. 526 00:25:48,090 --> 00:25:49,690 It's actually relatively straightforward, 527 00:25:49,690 --> 00:25:52,630 because this is recursive. 528 00:25:52,630 --> 00:25:55,420 What could we ask you to do on a quiz? 529 00:25:55,420 --> 00:25:57,000 We could ask you to write this code. 530 00:25:57,000 --> 00:25:59,170 We could ask you to look at this code and modify this code and explain what 531 00:25:59,170 --> 00:26:00,210 it's doing. 532 00:26:00,210 --> 00:26:00,390 Yeah. 533 00:26:00,390 --> 00:26:00,770 Question? 534 00:26:00,770 --> 00:26:04,240 >> AUDIENCE: Are these slides going to be made available as they were last time? 535 00:26:04,240 --> 00:26:04,740 >> R.J. AQUINO: Yes. 536 00:26:04,740 --> 00:26:06,460 So these slides will definitely be posted. 537 00:26:06,460 --> 00:26:08,640 >> AUDIENCE: They're actually posted right now on the website. 538 00:26:08,640 --> 00:26:10,020 David just did that. 539 00:26:10,020 --> 00:26:12,720 >> R.J. AQUINO: The slides are right now on the website. 540 00:26:12,720 --> 00:26:16,420 I'll probably patch up a couple of the typos I noted and fix them. 541 00:26:16,420 --> 00:26:19,940 But there's a current version on the site. 542 00:26:19,940 --> 00:26:21,820 Others things we could ask you to do-- 543 00:26:21,820 --> 00:26:23,790 write insert. 544 00:26:23,790 --> 00:26:27,490 Write an iterative version of the recursive function we just showed you 545 00:26:27,490 --> 00:26:32,520 or talk about these things, like in paragraphs, in words, in sentences. 546 00:26:32,520 --> 00:26:35,760 Comparing the run times and explaining what you would want to use a binary 547 00:26:35,760 --> 00:26:39,200 search tree for instead of a hash table, for instance. 548 00:26:39,200 --> 00:26:43,580 >> So understand these structures at a pretty deep level. 549 00:26:43,580 --> 00:26:47,440 Understand how to write them, how to use them, how to talk about them. 550 00:26:47,440 --> 00:26:50,270 And you'll be all set. 551 00:26:50,270 --> 00:26:50,630 Question? 552 00:26:50,630 --> 00:26:55,070 >> AUDIENCE: When you're writing the binary search tree, how do you 553 00:26:55,070 --> 00:27:01,460 determine what value to make it as the root? 554 00:27:01,460 --> 00:27:06,120 >> R.J. AQUINO: So the question was, what value do you make as the root? 555 00:27:06,120 --> 00:27:08,760 Depending on your code, you may have a global root. 556 00:27:08,760 --> 00:27:14,290 So you may have likely had in pset6 a global hash table. 557 00:27:14,290 --> 00:27:18,640 Or you might pass the root in as an argument. 558 00:27:18,640 --> 00:27:23,810 So this search function here takes an argument a node*. 559 00:27:23,810 --> 00:27:27,420 And so whatever node you happen to be looking at is the one you're treating 560 00:27:27,420 --> 00:27:31,510 as your root when you pass it in. 561 00:27:31,510 --> 00:27:32,320 And I'm all set. 562 00:27:32,320 --> 00:27:33,480 Those are my slides. 563 00:27:33,480 --> 00:27:35,940 The next person can come swap in a laptop and mic. 564 00:27:35,940 --> 00:27:47,390 565 00:27:47,390 --> 00:27:49,760 >> ROB BOWDEN: I think I might have interpreted that question differently. 566 00:27:49,760 --> 00:27:53,826 But I interpreted it as, if you have the numbers 1, 2, and 3, how do we 567 00:27:53,826 --> 00:27:56,720 know to make 2 the root as opposed to 1 or 3? 568 00:27:56,720 --> 00:27:59,480 If we make 2 the root, then it's nicely 1 and 3 to the left and right. 569 00:27:59,480 --> 00:28:04,610 But if 1 is the root, then it's 1 to the top, 2 the right, 3 to the right. 570 00:28:04,610 --> 00:28:10,880 So by default, you don't know what to make the root. 571 00:28:10,880 --> 00:28:15,400 And for any algorithm we're expecting to give you, just the first thing you 572 00:28:15,400 --> 00:28:16,680 insert would be the root. 573 00:28:16,680 --> 00:28:19,890 Or we'd give you a binary tree that already exists that has a root. 574 00:28:19,890 --> 00:28:24,760 But other algorithms exists such that the root will update, so that if you 575 00:28:24,760 --> 00:28:28,370 end up in the situation where it's 1, 2, 3, it would automatically update to 576 00:28:28,370 --> 00:28:30,900 make 2 the new root, so that it's still nicely balanced. 577 00:28:30,900 --> 00:28:33,750 578 00:28:33,750 --> 00:28:34,833 >> ANGELA LI: Cool. 579 00:28:34,833 --> 00:28:36,170 Hey, guys. 580 00:28:36,170 --> 00:28:37,810 I'm Angela. 581 00:28:37,810 --> 00:28:42,490 And I'm going to finish off our C and then go into some of our web 582 00:28:42,490 --> 00:28:43,120 technologies-- 583 00:28:43,120 --> 00:28:46,570 HTTP, HTML, and CSS. 584 00:28:46,570 --> 00:28:49,610 So the first thing is buffer overflow attacks. 585 00:28:49,610 --> 00:28:53,070 So let's take a look at this code. 586 00:28:53,070 --> 00:28:54,260 It's pretty simple. 587 00:28:54,260 --> 00:28:55,460 There's a function foo. 588 00:28:55,460 --> 00:28:56,990 And it doesn't return anything. 589 00:28:56,990 --> 00:29:00,950 But it takes in a pointer to a string called bar. 590 00:29:00,950 --> 00:29:04,920 >> And it's going to declare this buffer, which is a character 591 00:29:04,920 --> 00:29:07,690 array that has 12 slots. 592 00:29:07,690 --> 00:29:11,730 And it uses memcpy, which is just a function that copies from one address 593 00:29:11,730 --> 00:29:12,910 into another. 594 00:29:12,910 --> 00:29:19,400 So this is trying to copy into our buffer from whatever 595 00:29:19,400 --> 00:29:21,140 bar is pointing to. 596 00:29:21,140 --> 00:29:24,640 So any idea what's wrong with this code? 597 00:29:24,640 --> 00:29:27,568 598 00:29:27,568 --> 00:29:30,830 >> AUDIENCE: If bar is longer than C, they will overwrite. 599 00:29:30,830 --> 00:29:31,520 >> ANGELA LI: Yeah, exactly. 600 00:29:31,520 --> 00:29:34,200 We have no guarantee that bar is going to be less than 12. 601 00:29:34,200 --> 00:29:36,080 We just made some arbitrary number 12. 602 00:29:36,080 --> 00:29:38,380 And we were like, let's hope that our user input is less than 603 00:29:38,380 --> 00:29:40,440 12 characters long. 604 00:29:40,440 --> 00:29:46,320 So in an ideal world, if our input is always as expected, then we'll get 605 00:29:46,320 --> 00:29:47,550 something like, hello. 606 00:29:47,550 --> 00:29:48,920 That's less than 12 characters. 607 00:29:48,920 --> 00:29:51,870 It gets read into char c. 608 00:29:51,870 --> 00:29:53,280 And then we do something with it. 609 00:29:53,280 --> 00:29:54,800 It doesn't really matter. 610 00:29:54,800 --> 00:29:59,740 >> But a malicious person could do something more like this, where they 611 00:29:59,740 --> 00:30:04,760 give us whatever bar is pointing to, it's going to point to this huge array 612 00:30:04,760 --> 00:30:06,280 of just A's. 613 00:30:06,280 --> 00:30:10,680 And this is way longer than 12. 614 00:30:10,680 --> 00:30:13,830 So it's going to go all the way down here to where the return 615 00:30:13,830 --> 00:30:15,420 address used to be. 616 00:30:15,420 --> 00:30:17,860 So let's say this function is called foo. 617 00:30:17,860 --> 00:30:20,970 Maybe foo was called by some other function, which was called by main. 618 00:30:20,970 --> 00:30:24,890 So when foo is running, it needs to know where to return to. 619 00:30:24,890 --> 00:30:29,130 >> If foo was called by some function named baz, it has to know that it's 620 00:30:29,130 --> 00:30:30,250 got to go back to baz. 621 00:30:30,250 --> 00:30:34,040 And that's what this return address down here is telling us. 622 00:30:34,040 --> 00:30:38,340 But if we overwrite it with some other address, in this case, this is a 623 00:30:38,340 --> 00:30:42,650 representation of the address at the very beginning of this buffer, then 624 00:30:42,650 --> 00:30:45,240 what's actually going to happen is that instead of returning back to baz, 625 00:30:45,240 --> 00:30:48,470 which called our function, it's just going to go to the front of this code. 626 00:30:48,470 --> 00:30:53,930 >> And if this was there because a malicious hacker dude came and 627 00:30:53,930 --> 00:30:56,820 injected this, then maybe this amount of A's is not actually A's. 628 00:30:56,820 --> 00:31:02,030 And it's actually just code that breaks your computer or something. 629 00:31:02,030 --> 00:31:05,930 So to be defensive about this sort of thing, you have to never assume that 630 00:31:05,930 --> 00:31:09,120 user input is a certain amount of characters. 631 00:31:09,120 --> 00:31:13,310 For example, when you were doing speller, you were told that words were 632 00:31:13,310 --> 00:31:15,580 only going to be 40 characters long maximum. 633 00:31:15,580 --> 00:31:16,570 And that was good. 634 00:31:16,570 --> 00:31:20,150 >> But if not, then you would have to make sure to only read in 45 635 00:31:20,150 --> 00:31:21,520 characters at a time. 636 00:31:21,520 --> 00:31:24,430 Otherwise, you might overwrite your buffer. 637 00:31:24,430 --> 00:31:26,140 Any questions on that. 638 00:31:26,140 --> 00:31:26,733 Yeah. 639 00:31:26,733 --> 00:31:28,850 >> AUDIENCE: Could you just talk a little more about these? 640 00:31:28,850 --> 00:31:29,790 >> ANGELA LI: Sorry. 641 00:31:29,790 --> 00:31:31,040 Yes. 642 00:31:31,040 --> 00:31:32,813 643 00:31:32,813 --> 00:31:35,870 >> AUDIENCE: The mic is just for video. 644 00:31:35,870 --> 00:31:37,640 I will try and project. 645 00:31:37,640 --> 00:31:39,900 Hi, guys. 646 00:31:39,900 --> 00:31:40,920 Sup? 647 00:31:40,920 --> 00:31:45,330 So let's go over a few things in the CS50 library, which you've been using 648 00:31:45,330 --> 00:31:49,072 all semester, mostly to get user input. 649 00:31:49,072 --> 00:31:53,140 As you know, you include the CS50 library by just doing CS50.h, which 650 00:31:53,140 --> 00:31:55,660 contains all the prototypes of the functions that you can use, like 651 00:31:55,660 --> 00:31:58,640 GetString and GetInt, and GetFloat, et cetera. 652 00:31:58,640 --> 00:32:02,870 And there's this one line in the CS50 library which defines a string, which 653 00:32:02,870 --> 00:32:05,380 you guys all know by now is just a char*. 654 00:32:05,380 --> 00:32:07,900 >> But let's take a peek at how GetString works. 655 00:32:07,900 --> 00:32:10,010 This is a very abridged version. 656 00:32:10,010 --> 00:32:15,090 You can pull up the CS50 library files from, I think, manuals.CS50.net. 657 00:32:15,090 --> 00:32:16,750 And you can read through the actual function. 658 00:32:16,750 --> 00:32:19,330 But this covers some of the important parts. 659 00:32:19,330 --> 00:32:23,870 So we've created some buffer with some capacity. 660 00:32:23,870 --> 00:32:27,570 And what we do is we get one character at a time from standard n. 661 00:32:27,570 --> 00:32:30,910 That's where the user inputs text in the console. 662 00:32:30,910 --> 00:32:33,430 >> And so we're going to read in a character so long as it's not a new 663 00:32:33,430 --> 00:32:37,220 line and it's not end of file, which is the end of standard input. 664 00:32:37,220 --> 00:32:45,690 And for every character that we read in, if that character ends up adding 665 00:32:45,690 --> 00:32:50,120 to the number of characters we've read in and that is more than our capacity, 666 00:32:50,120 --> 00:32:53,490 then what we do is we just resize our buffer so that it's twice as long. 667 00:32:53,490 --> 00:32:56,950 So again, this protect against buffer overflow attacks, because you read in 668 00:32:56,950 --> 00:32:58,315 a character at a time. 669 00:32:58,315 --> 00:33:02,290 And if at any point you read in too many, you just expand your buffer. 670 00:33:02,290 --> 00:33:03,330 You multiply it by two. 671 00:33:03,330 --> 00:33:05,510 And then you have more room. 672 00:33:05,510 --> 00:33:09,120 >> Otherwise, you just add a character to buffer. 673 00:33:09,120 --> 00:33:15,080 And after you've read in all the characters, it will shrink the buffer 674 00:33:15,080 --> 00:33:18,510 back down to the normal size, add a null terminator, and then return. 675 00:33:18,510 --> 00:33:21,880 676 00:33:21,880 --> 00:33:24,960 Now, let's look at GetInt. 677 00:33:24,960 --> 00:33:27,700 Can you guys read this? 678 00:33:27,700 --> 00:33:30,710 I can zoom in a bit. 679 00:33:30,710 --> 00:33:33,410 680 00:33:33,410 --> 00:33:34,660 I don't know how computers work. 681 00:33:34,660 --> 00:33:40,840 682 00:33:40,840 --> 00:33:41,270 Never mind. 683 00:33:41,270 --> 00:33:42,520 I can't zoom in properly. 684 00:33:42,520 --> 00:33:47,500 685 00:33:47,500 --> 00:33:48,770 >> This is really hard. 686 00:33:48,770 --> 00:33:49,180 I'm sorry. 687 00:33:49,180 --> 00:33:51,490 Let's just look at this. 688 00:33:51,490 --> 00:33:57,140 So what GetInt does is it first reads in a string from GetString, which 689 00:33:57,140 --> 00:33:59,250 we've implemented before. 690 00:33:59,250 --> 00:34:02,945 And the important part to note here is if this sharing that it ends up 691 00:34:02,945 --> 00:34:06,400 reading is like not actually a string, then we just return INT_MAX to 692 00:34:06,400 --> 00:34:09,409 represent failure. 693 00:34:09,409 --> 00:34:12,645 Why do we return INT_MAX instead of negative 1 or 1? 694 00:34:12,645 --> 00:34:13,895 Any ideas? 695 00:34:13,895 --> 00:34:16,853 696 00:34:16,853 --> 00:34:19,350 >> AUDIENCE: [INAUDIBLE] negative 1 on one. 697 00:34:19,350 --> 00:34:20,070 >> ANGELA LI: Yeah, exactly. 698 00:34:20,070 --> 00:34:24,560 So you're way more likely to just want to input 1 or negative 1 when prompted 699 00:34:24,560 --> 00:34:27,469 for an nth and whatever nth maxes. 700 00:34:27,469 --> 00:34:27,969 It's huge. 701 00:34:27,969 --> 00:34:29,690 You're probably not going to use it. 702 00:34:29,690 --> 00:34:32,690 So this is like a design decision to make sure that you don't accidentally 703 00:34:32,690 --> 00:34:38,540 return an error or you don't return 1, which might be parsed 704 00:34:38,540 --> 00:34:41,199 as a correct answer. 705 00:34:41,199 --> 00:34:45,110 So if a line doesn't exist, we return INT-MAX. 706 00:34:45,110 --> 00:34:48,090 Otherwise, we use sscanf, which is like scanf. 707 00:34:48,090 --> 00:34:49,449 But it reads from a string. 708 00:34:49,449 --> 00:34:54,310 >> And we have this formatted string, which is %i %c. 709 00:34:54,310 --> 00:34:57,440 And we try and match that with whatever the user gave us. 710 00:34:57,440 --> 00:35:01,420 We want the number of matched things to be 1, which means that we only 711 00:35:01,420 --> 00:35:04,940 really want to match an integer surrounded by maybe white 712 00:35:04,940 --> 00:35:06,840 space, maybe not. 713 00:35:06,840 --> 00:35:10,710 In this case, if you put in something like bar, bar doesn't match at all, 714 00:35:10,710 --> 00:35:14,400 because there needs to be an integer at the start. 715 00:35:14,400 --> 00:35:17,060 So sscan never turned 0. 716 00:35:17,060 --> 00:35:19,640 So you don't return that. 717 00:35:19,640 --> 00:35:23,850 >> Alternatively, if you put in something like 1, 2, 3, A, B, C, that matches 718 00:35:23,850 --> 00:35:27,180 both the integer but also the character after it. 719 00:35:27,180 --> 00:35:29,990 So sscanf will return 2, which is also not ideal. 720 00:35:29,990 --> 00:35:34,620 You don't want 1, 2, 3, A, B, C to be a valid int. 721 00:35:34,620 --> 00:35:36,990 So that also doesn't work. 722 00:35:36,990 --> 00:35:38,530 But say you put in something like 50. 723 00:35:38,530 --> 00:35:42,460 That will match the %i, which means it will get read into n. 724 00:35:42,460 --> 00:35:44,790 And now, n will contain the number 50. 725 00:35:44,790 --> 00:35:46,110 And then you can return it. 726 00:35:46,110 --> 00:35:49,270 >> Otherwise, you hit Retry. 727 00:35:49,270 --> 00:35:55,790 And then it just goes over again until you get a proper input from the user. 728 00:35:55,790 --> 00:35:56,891 Any questions on that? 729 00:35:56,891 --> 00:36:02,182 >> AUDIENCE: So if you were to print out the value of the GetInt on [INAUDIBLE] 730 00:36:02,182 --> 00:36:06,250 would it be just the integer and max? 731 00:36:06,250 --> 00:36:07,440 >> ANGELA LI: Yeah. 732 00:36:07,440 --> 00:36:11,780 So if you use GetInt, you should assume that you don't want nth max to 733 00:36:11,780 --> 00:36:15,328 be a valid input, because you're going to assume that that was bad. 734 00:36:15,328 --> 00:36:27,740 >> AUDIENCE: If we didn't have char c and someone put in 1, 2, 3, Sam, would it 735 00:36:27,740 --> 00:36:29,430 still work for 1, 2, 3? 736 00:36:29,430 --> 00:36:29,750 >> ANGELA LI: I think it would work. 737 00:36:29,750 --> 00:36:33,340 But you don't want 123Sam to be a valid input by a user. 738 00:36:33,340 --> 00:36:34,670 That's not really an int. 739 00:36:34,670 --> 00:36:36,840 So it doesn't seem fair to parse it as an int. 740 00:36:36,840 --> 00:36:40,910 741 00:36:40,910 --> 00:36:42,160 OK. 742 00:36:42,160 --> 00:36:45,800 In that case, let's move on to the internet. 743 00:36:45,800 --> 00:36:49,120 So HTTP is not a language. 744 00:36:49,120 --> 00:36:56,060 HTTP is just the set of standards for how you send things from clients, 745 00:36:56,060 --> 00:36:57,280 that's you, to servers. 746 00:36:57,280 --> 00:36:59,730 That's other people on the web. 747 00:36:59,730 --> 00:37:02,900 >> So HTTP stands for Hypertext Transfer Protocol. 748 00:37:02,900 --> 00:37:04,610 It's the heart and soul of the whole web. 749 00:37:04,610 --> 00:37:07,050 The hypertext part just refers to HTML. 750 00:37:07,050 --> 00:37:10,690 The transfer is clients like you will send requests to 751 00:37:10,690 --> 00:37:13,060 servers, which give responses. 752 00:37:13,060 --> 00:37:16,380 And the protocol is just, how do you expect a server to behave? 753 00:37:16,380 --> 00:37:19,960 And how are you supposed to behave such that you can streamline this 754 00:37:19,960 --> 00:37:21,920 communication process? 755 00:37:21,920 --> 00:37:26,650 >> So HTTP requests look a lot like this. 756 00:37:26,650 --> 00:37:28,070 GET is the type of request. 757 00:37:28,070 --> 00:37:31,220 You guys have seen GET requests and POST requests. 758 00:37:31,220 --> 00:37:36,690 That second thing there, /me, that's just the URI or the URL of where you 759 00:37:36,690 --> 00:37:38,140 want to go within the host. 760 00:37:38,140 --> 00:37:44,140 So this request is asking for the page, like www.facebook.com/me. 761 00:37:44,140 --> 00:37:45,300 And it's a GET request. 762 00:37:45,300 --> 00:37:51,020 And then this HTTP/1.1, that's just the version of HTTP you're using. 763 00:37:51,020 --> 00:37:55,020 It's almost always 1.1. 764 00:37:55,020 --> 00:37:56,880 >> And then there's a bunch of other stuff too. 765 00:37:56,880 --> 00:38:02,510 You can actually see these if you open up your console when you're 766 00:38:02,510 --> 00:38:03,770 browsing the web. 767 00:38:03,770 --> 00:38:07,290 Responses look something more like this. 768 00:38:07,290 --> 00:38:09,620 The top part is, again, the type of HTTP you're using 769 00:38:09,620 --> 00:38:12,310 followed by a status code. 770 00:38:12,310 --> 00:38:14,700 So 200 OK is everything worked out. 771 00:38:14,700 --> 00:38:16,200 Here is your content. 772 00:38:16,200 --> 00:38:17,390 Your content is going to follow. 773 00:38:17,390 --> 00:38:21,730 And then it will tell you what kind of content and other stuff too. 774 00:38:21,730 --> 00:38:24,620 >> The status codes, there are a few important ones that you should know. 775 00:38:24,620 --> 00:38:26,460 200 OK is like everything's golden. 776 00:38:26,460 --> 00:38:28,490 Everything works. 777 00:38:28,490 --> 00:38:29,710 403 Forbidden. 778 00:38:29,710 --> 00:38:32,910 This you've probably seen if you forgot to chmod something properly. 779 00:38:32,910 --> 00:38:34,510 It means that you don't have the right permissions to 780 00:38:34,510 --> 00:38:36,210 access that on the server. 781 00:38:36,210 --> 00:38:38,110 It's like, no, you can't see it. 782 00:38:38,110 --> 00:38:39,780 404 means that thing doesn't exist. 783 00:38:39,780 --> 00:38:40,400 Not found. 784 00:38:40,400 --> 00:38:41,640 You've probably seen that a lot. 785 00:38:41,640 --> 00:38:45,510 >> 500 Internal Server Error is usually like something went wrong on the side 786 00:38:45,510 --> 00:38:46,460 of the server. 787 00:38:46,460 --> 00:38:50,830 So when you were implementing pset7, if you had PHP errors, you could 788 00:38:50,830 --> 00:38:53,890 actually go to the page and see a whole bunch of PHP error stuff. 789 00:38:53,890 --> 00:38:56,900 But that doesn't normally happen, because websites don't really want to 790 00:38:56,900 --> 00:38:58,830 tell you why their site is broken. 791 00:38:58,830 --> 00:39:03,370 They'll probably just return a 500 Internal Server Error. 792 00:39:03,370 --> 00:39:06,120 >> And then there's 418 I'm a teapot. 793 00:39:06,120 --> 00:39:07,910 There's a whole story about why that's a thing. 794 00:39:07,910 --> 00:39:09,860 But you can read about that on your own time. 795 00:39:09,860 --> 00:39:11,450 There's a whole bunch of other status codes too. 796 00:39:11,450 --> 00:39:12,700 But these are the ones you should know. 797 00:39:12,700 --> 00:39:15,660 798 00:39:15,660 --> 00:39:18,610 So let's talk about HTML. 799 00:39:18,610 --> 00:39:22,180 HTML, remember, is not a programming language. 800 00:39:22,180 --> 00:39:23,510 It's a markup language. 801 00:39:23,510 --> 00:39:25,210 That means it describes content. 802 00:39:25,210 --> 00:39:30,440 It tells you what an HTML document looks like or not what it looks like 803 00:39:30,440 --> 00:39:32,230 but how it's structured. 804 00:39:32,230 --> 00:39:36,110 >> So it defines a structure and semantics of web pages. 805 00:39:36,110 --> 00:39:37,830 It's like, this is a paragraph. 806 00:39:37,830 --> 00:39:40,060 This is an ordered list. 807 00:39:40,060 --> 00:39:42,360 This is like a section of my page. 808 00:39:42,360 --> 00:39:43,260 Here's the title. 809 00:39:43,260 --> 00:39:44,310 It does stuff like that. 810 00:39:44,310 --> 00:39:48,770 It doesn't style any of that, because that's what you do in CSS. 811 00:39:48,770 --> 00:39:50,270 And it looks like a series of nested tags. 812 00:39:50,270 --> 00:39:54,720 So to use an example of a really basic HTML page, you have the DOCTYPE 813 00:39:54,720 --> 00:39:56,720 declaration up there. 814 00:39:56,720 --> 00:40:00,940 >> This DOCTYPE declaration is saying, we're using HTML5. 815 00:40:00,940 --> 00:40:03,370 Then you have the big HTML tag. 816 00:40:03,370 --> 00:40:05,230 It contains a head and a body. 817 00:40:05,230 --> 00:40:06,970 Inside the head, you have the title. 818 00:40:06,970 --> 00:40:12,950 That's what goes in the title bar of your browser. 819 00:40:12,950 --> 00:40:15,810 We have a link tag that links in an external style sheet. 820 00:40:15,810 --> 00:40:19,880 And then we have a script that pulls from an external JavaScript as well. 821 00:40:19,880 --> 00:40:23,750 >> And then inside our body is actually what gets shown on the page. 822 00:40:23,750 --> 00:40:28,210 We've got a paragraph and then an image inside that paragraph. 823 00:40:28,210 --> 00:40:32,000 This one is a picture of kittens. 824 00:40:32,000 --> 00:40:35,840 Notice that the image tag closes itself. 825 00:40:35,840 --> 00:40:41,760 So instead of opening with an image and then doing another /image, you 826 00:40:41,760 --> 00:40:47,500 just have this little slash here, which closes it. 827 00:40:47,500 --> 00:40:53,670 And the image tag also has this key value attribute called alt. 828 00:40:53,670 --> 00:40:56,970 That's the alternative text that happens when you hover over it. 829 00:40:56,970 --> 00:41:03,170 >> Most HTML elements have some key value things that you can give it, various 830 00:41:03,170 --> 00:41:04,420 customization. 831 00:41:04,420 --> 00:41:06,230 832 00:41:06,230 --> 00:41:08,705 Yeah. 833 00:41:08,705 --> 00:41:09,955 >> AUDIENCE: [INAUDIBLE]. 834 00:41:09,955 --> 00:41:17,510 835 00:41:17,510 --> 00:41:19,680 >> ANGELA LI: Well, so it's an attribute of the tag. 836 00:41:19,680 --> 00:41:25,320 So if you were using jQuery, you could do select image.getAttribute. 837 00:41:25,320 --> 00:41:27,930 And then you can search for get the alt attribute. 838 00:41:27,930 --> 00:41:31,040 And it will give you kittens. 839 00:41:31,040 --> 00:41:37,400 If you remember forms in HTML, input elements will have name attributes. 840 00:41:37,400 --> 00:41:41,870 And that's what PHP uses to send requests when a form is submitted. 841 00:41:41,870 --> 00:41:44,762 842 00:41:44,762 --> 00:41:50,064 >> AUDIENCE: Did you mention something about how if you use kittens.jpg or 843 00:41:50,064 --> 00:41:54,410 something that has the missing file folders or other files? 844 00:41:54,410 --> 00:41:54,750 >> ANGELA LI: Yes. 845 00:41:54,750 --> 00:41:57,010 So this is what's called a relative path, because I'm not giving 846 00:41:57,010 --> 00:41:58,740 you the full path. 847 00:41:58,740 --> 00:42:05,160 This is like when in C if you do fopen some file, if you fopen hi.txt, that 848 00:42:05,160 --> 00:42:09,190 hi.txt is expected to be in the same directory, unless you give it a more 849 00:42:09,190 --> 00:42:11,530 complex path. 850 00:42:11,530 --> 00:42:14,900 >> AUDIENCE: So you could specify which folder [INAUDIBLE]? 851 00:42:14,900 --> 00:42:17,660 >> ANGELA LI: Yeah. 852 00:42:17,660 --> 00:42:19,370 And you can look up how to do that. 853 00:42:19,370 --> 00:42:23,480 But if I wanted to get kittens.jpg out of the parent directory, I would do 854 00:42:23,480 --> 00:42:24,730 ../kittens.jpg. 855 00:42:24,730 --> 00:42:29,680 856 00:42:29,680 --> 00:42:30,930 Yeah. 857 00:42:30,930 --> 00:42:32,960 858 00:42:32,960 --> 00:42:33,760 Sorry. 859 00:42:33,760 --> 00:42:34,045 Yeah. 860 00:42:34,045 --> 00:42:35,700 Oh man, I forgot the question. 861 00:42:35,700 --> 00:42:36,460 What was the question? 862 00:42:36,460 --> 00:42:39,570 Oh, the question was is, kittens.jpg expected to be in the same directory? 863 00:42:39,570 --> 00:42:40,630 And in this case, it is. 864 00:42:40,630 --> 00:42:44,030 But you can also give it a certain path such that it doesn't have to be. 865 00:42:44,030 --> 00:42:47,100 866 00:42:47,100 --> 00:42:48,350 Good? 867 00:42:48,350 --> 00:42:50,190 868 00:42:50,190 --> 00:42:51,350 >> CSS. 869 00:42:51,350 --> 00:42:55,420 So CSS, like HTML, is not a programming language. 870 00:42:55,420 --> 00:42:58,250 CSS is just a series of styling rules. 871 00:42:58,250 --> 00:43:00,130 It stands for Cascading Style Sheets. 872 00:43:00,130 --> 00:43:03,910 And you use it in conjunction with HTML to style pages. 873 00:43:03,910 --> 00:43:08,140 So there are three ways you can include it. 874 00:43:08,140 --> 00:43:11,950 One way you can do it is in the head portion of your HTML, you can just 875 00:43:11,950 --> 00:43:15,410 open a style tag and then stick some CSS rules in there. 876 00:43:15,410 --> 00:43:16,759 It's pretty OK. 877 00:43:16,759 --> 00:43:17,228 Yeah. 878 00:43:17,228 --> 00:43:21,449 >> AUDIENCE: Could you put those style tags in between, let's 879 00:43:21,449 --> 00:43:22,860 say, body and /body. 880 00:43:22,860 --> 00:43:27,400 And then you would be styling only in body. 881 00:43:27,400 --> 00:43:28,840 >> ANGELA LI: You could. 882 00:43:28,840 --> 00:43:29,590 It'll work. 883 00:43:29,590 --> 00:43:33,990 But you shouldn't, because styling is kind of the metadata that should go in 884 00:43:33,990 --> 00:43:35,890 the head of your document. 885 00:43:35,890 --> 00:43:38,280 Body should really only contain what's actually going to 886 00:43:38,280 --> 00:43:39,420 show up on your page. 887 00:43:39,420 --> 00:43:42,155 >> AUDIENCE: So you'd put style in your head to style the 888 00:43:42,155 --> 00:43:43,930 entire web page, right? 889 00:43:43,930 --> 00:43:44,300 >> ANGELA LI: Yeah. 890 00:43:44,300 --> 00:43:50,470 So putting style here, these CSS rules will apply to the whole page based on 891 00:43:50,470 --> 00:43:52,100 their selectors. 892 00:43:52,100 --> 00:43:57,090 So the better way to do it is instead of having a style tag in your head, 893 00:43:57,090 --> 00:44:00,430 you have this link to an external style sheet like I showed you in the 894 00:44:00,430 --> 00:44:01,980 previous example. 895 00:44:01,980 --> 00:44:05,920 What this does is it tries and finds the file style.css and then pulls it 896 00:44:05,920 --> 00:44:08,470 in and uses that as the styles for the page. 897 00:44:08,470 --> 00:44:10,500 And your style.css would just look like this. 898 00:44:10,500 --> 00:44:13,330 It would just be a bunch of CSS. 899 00:44:13,330 --> 00:44:16,210 >> And finally, there's another way you can include CSS, which you really 900 00:44:16,210 --> 00:44:17,480 shouldn't ever do. 901 00:44:17,480 --> 00:44:18,950 It's call inline styling. 902 00:44:18,950 --> 00:44:22,650 And so any HTML element can also take a style attribute. 903 00:44:22,650 --> 00:44:26,320 And then in that style attribute, you can give it CSS rules. 904 00:44:26,320 --> 00:44:29,140 So in this case, whatever div I'm defining right here, it's going to 905 00:44:29,140 --> 00:44:32,580 have a black background and a white text color. 906 00:44:32,580 --> 00:44:35,620 But you shouldn't do this, because what this does is it puts your styling 907 00:44:35,620 --> 00:44:36,850 inside your HTML. 908 00:44:36,850 --> 00:44:40,530 >> And I know we've been talking about HTML is structure and CSS is style. 909 00:44:40,530 --> 00:44:42,790 If you do this, it mixes them together. 910 00:44:42,790 --> 00:44:44,550 And it's not very clean. 911 00:44:44,550 --> 00:44:45,800 So don't do that. 912 00:44:45,800 --> 00:44:47,690 913 00:44:47,690 --> 00:44:52,100 Using an example of CSS, up there, we just select the body of the HTML 914 00:44:52,100 --> 00:44:52,380 documentary. 915 00:44:52,380 --> 00:44:55,110 And we're like, everything's going to be Comic Sans. 916 00:44:55,110 --> 00:44:57,290 I also don't recommend that. 917 00:44:57,290 --> 00:44:59,940 But you could do that. 918 00:44:59,940 --> 00:45:03,140 >> The second rule right here, it's going to select the element on the 919 00:45:03,140 --> 00:45:04,880 page with ID main. 920 00:45:04,880 --> 00:45:11,690 So whatever HTML element, I said ID = main, I'm going to give that a 921 00:45:11,690 --> 00:45:16,020 20-pixel margin and align everything, all the text, to the center. 922 00:45:16,020 --> 00:45:19,030 The last thing selects by CSS class. 923 00:45:19,030 --> 00:45:24,450 So any element on the page that I gave a section class, I'm going to make it 924 00:45:24,450 --> 00:45:26,602 a background color of light blue. 925 00:45:26,602 --> 00:45:29,380 926 00:45:29,380 --> 00:45:30,040 Yep. 927 00:45:30,040 --> 00:45:30,700 That's all I got. 928 00:45:30,700 --> 00:45:30,890 Question? 929 00:45:30,890 --> 00:45:34,020 >> AUDIENCE: What does the hashtag before main do? 930 00:45:34,020 --> 00:45:36,310 >> ANGELA LI: The question is, what does the hashtag before main do? 931 00:45:36,310 --> 00:45:40,770 In this case, the hash in CSS means select by ID. 932 00:45:40,770 --> 00:45:47,490 So if I had some HTML element, like divID = main, this CSS rule selects 933 00:45:47,490 --> 00:45:49,260 the thing with ID main. 934 00:45:49,260 --> 00:45:53,940 And similarly, the period in front of section is select by CSS class or 935 00:45:53,940 --> 00:45:56,558 select by HTML class. 936 00:45:56,558 --> 00:46:00,940 >> AUDIENCE: Why is there a has before 6 in background color? 937 00:46:00,940 --> 00:46:01,270 >> ANGELA LI: Yeah. 938 00:46:01,270 --> 00:46:03,360 So the question is, why is there a hash before the 6? 939 00:46:03,360 --> 00:46:05,320 This is different than that hash. 940 00:46:05,320 --> 00:46:09,500 This means that you're giving a hexadecimal color. 941 00:46:09,500 --> 00:46:14,260 So hex colors, this just represents a color. 942 00:46:14,260 --> 00:46:17,860 And you remember RGB triples when you did the forensics pset? 943 00:46:17,860 --> 00:46:18,770 This is similar. 944 00:46:18,770 --> 00:46:21,590 The first two digits represent how much red is in the color. 945 00:46:21,590 --> 00:46:23,260 The second two represent how much green. 946 00:46:23,260 --> 00:46:25,450 And the third represents how much blue. 947 00:46:25,450 --> 00:46:30,060 And the hash is this is going to represent a color. 948 00:46:30,060 --> 00:46:35,660 >> So anything from 0, 0, 0, 0, 0, 0 up to F, F, F, F, F, F is valid. 949 00:46:35,660 --> 00:46:39,550 It's some valid color that can be displayed by your browser. 950 00:46:39,550 --> 00:46:39,790 Question? 951 00:46:39,790 --> 00:46:43,590 >> AUDIENCE: What's the difference between using by ID and by class? 952 00:46:43,590 --> 00:46:46,470 >> ANGELA LI: The question is what's the difference between 953 00:46:46,470 --> 00:46:48,950 using by ID and class? 954 00:46:48,950 --> 00:46:54,390 You can only have one element in an HTML document that has a given ID. 955 00:46:54,390 --> 00:46:58,660 So only one thing on my page is allowed to have ID main. 956 00:46:58,660 --> 00:47:02,580 So you use it for this is the header. 957 00:47:02,580 --> 00:47:03,850 This is the navigation. 958 00:47:03,850 --> 00:47:05,230 This is the footer. 959 00:47:05,230 --> 00:47:09,070 Classes are different, because you can apply classes to as many HTML elements 960 00:47:09,070 --> 00:47:10,100 as you want. 961 00:47:10,100 --> 00:47:15,860 >> So for example, I did class section, because there's probably more than one 962 00:47:15,860 --> 00:47:17,540 section on my page. 963 00:47:17,540 --> 00:47:20,200 You're just allowed to have as many elements on the page with the same 964 00:47:20,200 --> 00:47:23,190 class but only one with a certain ID. 965 00:47:23,190 --> 00:47:25,600 >> AUDIENCE: So the dot represents the class? 966 00:47:25,600 --> 00:47:26,090 >> ANGELA LI: Yeah. 967 00:47:26,090 --> 00:47:27,380 A dot represents a class. 968 00:47:27,380 --> 00:47:29,990 969 00:47:29,990 --> 00:47:31,540 Cool. 970 00:47:31,540 --> 00:47:32,370 That's all I've got, guys. 971 00:47:32,370 --> 00:47:33,544 Thank you. 972 00:47:33,544 --> 00:48:13,380 >> [APPLAUSE] 973 00:48:13,380 --> 00:48:14,290 >> ZAMYLA CHAN: Hi, everyone. 974 00:48:14,290 --> 00:48:14,880 I'm Zamyla. 975 00:48:14,880 --> 00:48:18,830 I'm going to be covering PHP, MVC, and SQL today. 976 00:48:18,830 --> 00:48:22,350 977 00:48:22,350 --> 00:48:26,110 A lot of the material that I'll be covering is going to be pretty much 978 00:48:26,110 --> 00:48:29,100 right out of pset7. 979 00:48:29,100 --> 00:48:29,700 All right. 980 00:48:29,700 --> 00:48:31,180 So what is PHP? 981 00:48:31,180 --> 00:48:35,150 PHP stands for PHP Hypertext Preprocessor. 982 00:48:35,150 --> 00:48:38,740 So it, in itself, is a recursive name, which is pretty cool. 983 00:48:38,740 --> 00:48:42,220 PHP is a server-side scripting language, and it provides the backend 984 00:48:42,220 --> 00:48:44,610 and the logical underpinnings of our website. 985 00:48:44,610 --> 00:48:48,520 >> So Angela talked a lot about the HTML and CSS that will make the structure 986 00:48:48,520 --> 00:48:49,530 of the website. 987 00:48:49,530 --> 00:48:53,210 But what if you want to change that content dynamically or if it varies 988 00:48:53,210 --> 00:48:55,240 based on the user or certain conditions? 989 00:48:55,240 --> 00:48:57,060 That's where PHP comes in. 990 00:48:57,060 --> 00:49:02,610 Now, typically, PHP might take a few less lines to implement the same thing 991 00:49:02,610 --> 00:49:07,380 in C. That's because PHP handles memory management for the programmer, 992 00:49:07,380 --> 00:49:11,170 as opposed to us having to malloc free, things like that. 993 00:49:11,170 --> 00:49:15,430 >> But since PHP is an interpretive language, typically, it might execute 994 00:49:15,430 --> 00:49:19,540 a bit more slowly than C, which is a compiled language. 995 00:49:19,540 --> 00:49:23,150 Because we're moving programming languages, let's look at how the 996 00:49:23,150 --> 00:49:24,570 syntax will differ. 997 00:49:24,570 --> 00:49:28,770 Let's be very careful not to get confused with this. 998 00:49:28,770 --> 00:49:33,750 So with PHP syntax, whether you are embedding your PHP inside of an HTML 999 00:49:33,750 --> 00:49:40,430 file or within a .php file itself, you need to enclose the code in the open 1000 00:49:40,430 --> 00:49:45,270 PHP and the closed PHP tags like follows, like on the screen. 1001 00:49:45,270 --> 00:49:46,660 >> Variables in PHP. 1002 00:49:46,660 --> 00:49:51,490 Every single variable will start with the $ sign followed by the name of 1003 00:49:51,490 --> 00:49:53,150 your variable. 1004 00:49:53,150 --> 00:49:56,530 Now, variables in PHP are loosely typed, which means that you don't need 1005 00:49:56,530 --> 00:50:00,030 to indicate what the data type is when you're declaring it. 1006 00:50:00,030 --> 00:50:03,505 However, this doesn't mean that they don't have any types at all. 1007 00:50:03,505 --> 00:50:09,370 So if I declare a variable and just set it equal to 1, and then I declare 1008 00:50:09,370 --> 00:50:15,140 another variable, set it equal to "1," and then another one 1.0, well, 1009 00:50:15,140 --> 00:50:19,410 depending on the type of equality operators I use, if I want to compare 1010 00:50:19,410 --> 00:50:21,830 across all types, then they'll be equal. 1011 00:50:21,830 --> 00:50:25,570 But if I want to make sure that the types are equal, PHP can still do 1012 00:50:25,570 --> 00:50:28,690 that, even though we don't indicate what type it is when we 1013 00:50:28,690 --> 00:50:31,170 first make the file. 1014 00:50:31,170 --> 00:50:33,990 >> Now, in PHP, even though we are switching over from programming 1015 00:50:33,990 --> 00:50:39,360 languages from C, we still have our trusty if condition, just like this. 1016 00:50:39,360 --> 00:50:43,270 We still have our while loops, just like this, where you put in your 1017 00:50:43,270 --> 00:50:47,300 condition and then the body of the loop. 1018 00:50:47,300 --> 00:50:50,360 And then we also have our for loop, which typically looks like this. 1019 00:50:50,360 --> 00:50:55,330 So if I wanted to iterate over all nine psets and submit and call a 1020 00:50:55,330 --> 00:50:58,960 function submitPset, then I can do that here, which you guys have all 1021 00:50:58,960 --> 00:50:59,830 done by this point. 1022 00:50:59,830 --> 00:51:01,080 Congratulations, by the way. 1023 00:51:01,080 --> 00:51:04,560 1024 00:51:04,560 --> 00:51:07,550 >> For the camera, people said, thank you. 1025 00:51:07,550 --> 00:51:11,220 Now, if you didn't want to just use this for loop, then PHP actually also 1026 00:51:11,220 --> 00:51:13,580 has things called foreach loops. 1027 00:51:13,580 --> 00:51:22,210 So if I had an array of integers, 0 through 8, stored in the array psets, 1028 00:51:22,210 --> 00:51:27,290 then I could have a foreach loop that iterates over every number in psets. 1029 00:51:27,290 --> 00:51:30,640 And then I could call the same function eight times, 1030 00:51:30,640 --> 00:51:31,910 just like I did before. 1031 00:51:31,910 --> 00:51:36,480 So this for each loop is nice, because you don't have to if you don't know 1032 00:51:36,480 --> 00:51:39,470 the exact length of the array that you have, then using this foreach loop 1033 00:51:39,470 --> 00:51:42,800 will take care of that for you. 1034 00:51:42,800 --> 00:51:45,410 >> So I made psets as an array. 1035 00:51:45,410 --> 00:51:46,700 Let's look at that. 1036 00:51:46,700 --> 00:51:51,290 Arrays in PHP are typically the same as the ones that we've had in C, where 1037 00:51:51,290 --> 00:51:52,960 you can declare an array. 1038 00:51:52,960 --> 00:51:59,200 And here, I can declare an empty array and then build up dynamically by using 1039 00:51:59,200 --> 00:52:00,850 indices as integers. 1040 00:52:00,850 --> 00:52:04,140 So index 0, I'm going to store an integer named 1. 1041 00:52:04,140 --> 00:52:09,210 At index 1 of my list, I'm going to store the value 2. 1042 00:52:09,210 --> 00:52:12,670 And at the third index but the second number, I'm going to 1043 00:52:12,670 --> 00:52:14,870 store the number 12. 1044 00:52:14,870 --> 00:52:17,250 >> Now, this is fine in that works it works well. 1045 00:52:17,250 --> 00:52:21,310 But say it matters to me what each index holds. 1046 00:52:21,310 --> 00:52:24,500 For me, index 0 means how many cats I have. 1047 00:52:24,500 --> 00:52:27,400 And the index 1 means how many owls I have. 1048 00:52:27,400 --> 00:52:29,450 And the next one means how many dogs. 1049 00:52:29,450 --> 00:52:34,140 Well, then it to specify that, instead of having to remember 0 relates to 1050 00:52:34,140 --> 00:52:38,090 cats and 1 to owls, I can use associative arrays, which means that 1051 00:52:38,090 --> 00:52:42,260 instead of integers as my indices, I can actually use strings. 1052 00:52:42,260 --> 00:52:43,290 >> So this is quite useful. 1053 00:52:43,290 --> 00:52:47,130 And you've basically just replaced the integers with strings. 1054 00:52:47,130 --> 00:52:50,074 And there you have an associative array. 1055 00:52:50,074 --> 00:52:51,930 Yeah. 1056 00:52:51,930 --> 00:52:55,800 >> AUDIENCE: Is there a reason why there's an underscore for the second 1057 00:52:55,800 --> 00:52:58,750 part, because my list has the array. 1058 00:52:58,750 --> 00:53:01,330 >> ZAMYLA CHAN: The question was, is there a reason why there's an 1059 00:53:01,330 --> 00:53:03,320 underscore between my and list? 1060 00:53:03,320 --> 00:53:03,610 No. 1061 00:53:03,610 --> 00:53:06,878 That's just how I'm naming my variable. 1062 00:53:06,878 --> 00:53:11,670 >> AUDIENCE: On the first line, it's one word. 1063 00:53:11,670 --> 00:53:12,560 >> ZAMYLA CHAN: My apologies. 1064 00:53:12,560 --> 00:53:13,410 I'll fix that. 1065 00:53:13,410 --> 00:53:13,620 Yeah. 1066 00:53:13,620 --> 00:53:15,460 They should be the same variable name. 1067 00:53:15,460 --> 00:53:16,710 Good catch. 1068 00:53:16,710 --> 00:53:19,640 1069 00:53:19,640 --> 00:53:19,950 OK. 1070 00:53:19,950 --> 00:53:22,610 So let's move on to string concatenation. 1071 00:53:22,610 --> 00:53:27,500 If I wanted to take two strings, then I can concatenate them 1072 00:53:27,500 --> 00:53:28,550 with the dot operator. 1073 00:53:28,550 --> 00:53:32,440 So if I have Milo as a first name and Banana as a last name, then 1074 00:53:32,440 --> 00:53:35,430 concatenating with the dot operator and then putting a space in between 1075 00:53:35,430 --> 00:53:39,210 will make a string that contains Milo Banana, which I can then echo or, 1076 00:53:39,210 --> 00:53:41,280 rather, print out. 1077 00:53:41,280 --> 00:53:44,465 >> Speaking of echo, let's talk about a few useful-- 1078 00:53:44,465 --> 00:53:44,920 oops. 1079 00:53:44,920 --> 00:53:46,030 I'm sorry. 1080 00:53:46,030 --> 00:53:52,920 A few useful PHP functions. 1081 00:53:52,920 --> 00:53:56,240 So we have the-- 1082 00:53:56,240 --> 00:53:57,444 technical difficulties. 1083 00:53:57,444 --> 00:53:58,694 One second. 1084 00:53:58,694 --> 00:54:16,960 1085 00:54:16,960 --> 00:54:19,550 I sent it. 1086 00:54:19,550 --> 00:54:22,320 PowerPoint problems. 1087 00:54:22,320 --> 00:54:29,200 And we are back with PHP functions. 1088 00:54:29,200 --> 00:54:32,010 1089 00:54:32,010 --> 00:54:35,150 And we are back with PHP functions. 1090 00:54:35,150 --> 00:54:39,890 >> So we have the require function, where if you pass in a file, here's is just 1091 00:54:39,890 --> 00:54:43,300 an example of a file that I might pass in. 1092 00:54:43,300 --> 00:54:47,605 Then that will include the PHP code from that file that I indicate. 1093 00:54:47,605 --> 00:54:49,940 And it will evaluate that in. 1094 00:54:49,940 --> 00:54:54,450 Then we also have echo, which is a parallel to printf. 1095 00:54:54,450 --> 00:54:57,710 Exit is a parallel to break, which exits the block of 1096 00:54:57,710 --> 00:54:58,570 code that you're in. 1097 00:54:58,570 --> 00:55:03,180 And then empty checks whether a given variable is like null or zero or 1098 00:55:03,180 --> 00:55:08,482 whatever is equated with being empty. 1099 00:55:08,482 --> 00:55:09,438 Yeah. 1100 00:55:09,438 --> 00:55:15,341 >> AUDIENCE: For the string concatenation dot operator one, in PHP, is that the 1101 00:55:15,341 --> 00:55:20,158 same as in JavaScript where it's using the dot for concatenation means plus? 1102 00:55:20,158 --> 00:55:27,440 So for full name, you could have dollar sign first + and then + last? 1103 00:55:27,440 --> 00:55:27,720 >> ZAMYLA CHAN: Yeah. 1104 00:55:27,720 --> 00:55:32,150 So the question was whether in PHP we can use the same string concatenation 1105 00:55:32,150 --> 00:55:33,890 as in JavaScript with the pluses. 1106 00:55:33,890 --> 00:55:35,410 And Joseph will get into that later. 1107 00:55:35,410 --> 00:55:36,620 I think he has a slide on that. 1108 00:55:36,620 --> 00:55:37,570 Actually, it's different. 1109 00:55:37,570 --> 00:55:41,310 So in JavaScript, you need to use the plus to concatenate strings. 1110 00:55:41,310 --> 00:55:43,280 And in PHP, you have to use the dot operator. 1111 00:55:43,280 --> 00:55:44,530 So they're different. 1112 00:55:44,530 --> 00:55:46,680 1113 00:55:46,680 --> 00:55:46,910 >> OK. 1114 00:55:46,910 --> 00:55:49,500 So now that we've covered all this PHP, where does it 1115 00:55:49,500 --> 00:55:50,490 really come in handy? 1116 00:55:50,490 --> 00:55:54,470 Well, it comes in handy when we can combine it with our HTML. 1117 00:55:54,470 --> 00:55:59,550 So our PHP will give us the power to alter a page's HTML content prior to 1118 00:55:59,550 --> 00:56:00,000 its loading. 1119 00:56:00,000 --> 00:56:04,270 So based on different conditions, usually the specific user that's 1120 00:56:04,270 --> 00:56:07,520 logged in, we can display different information. 1121 00:56:07,520 --> 00:56:08,800 Linda, did you have a question? 1122 00:56:08,800 --> 00:56:15,510 >> AUDIENCE: Can you concatenate an integer also? 1123 00:56:15,510 --> 00:56:16,760 >> ZAMYLA CHAN: Yes, you can. 1124 00:56:16,760 --> 00:56:19,530 1125 00:56:19,530 --> 00:56:23,270 So the question was if you can concatenate integers or other 1126 00:56:23,270 --> 00:56:28,920 variable.s now, we move on to MVC, which is a paradigm that we used in 1127 00:56:28,920 --> 00:56:33,380 pset7 and a lot of web designers use for organizing the code in the files 1128 00:56:33,380 --> 00:56:34,490 in their website. 1129 00:56:34,490 --> 00:56:35,870 M stands for Model. 1130 00:56:35,870 --> 00:56:41,450 And basically, model files will deal with interactions with the database. 1131 00:56:41,450 --> 00:56:44,640 View files, they relate to the aesthetics of the website. 1132 00:56:44,640 --> 00:56:47,550 And the Controller handles user requests, parses 1133 00:56:47,550 --> 00:56:49,230 data, does other logic. 1134 00:56:49,230 --> 00:56:52,520 >> In pset7, we combined the model and the controller. 1135 00:56:52,520 --> 00:56:55,880 And we just called them controllers and put them in the public directory. 1136 00:56:55,880 --> 00:57:01,730 And the view files, we use them as templates in the templates directory. 1137 00:57:01,730 --> 00:57:07,260 So this diagram here also represents that same kind of division with the 1138 00:57:07,260 --> 00:57:10,510 model and the controller in purple here on the left and 1139 00:57:10,510 --> 00:57:12,770 the view on the right. 1140 00:57:12,770 --> 00:57:16,020 So this is a schematic that some of you may have seen at Office Hours or 1141 00:57:16,020 --> 00:57:19,130 diagrams that we were drawing as you were figuring out your pset. 1142 00:57:19,130 --> 00:57:25,030 >> So here, in a given controller, a model controller, we have functions 1143 00:57:25,030 --> 00:57:30,490 that relate to querying the SQL database, executing PHP logic. 1144 00:57:30,490 --> 00:57:32,370 Maybe you would look up a stock in Yahoo! 1145 00:57:32,370 --> 00:57:34,590 Finance. 1146 00:57:34,590 --> 00:57:37,390 Or perhaps, you would just check to see whether a user had submitted a 1147 00:57:37,390 --> 00:57:40,250 form already before having visited your page. 1148 00:57:40,250 --> 00:57:43,390 And then you would render a form over here. 1149 00:57:43,390 --> 00:57:48,210 After that form had been submitted by the user, the action that was 1150 00:57:48,210 --> 00:57:53,470 specified in the form's HTML tag would indicate the page that it 1151 00:57:53,470 --> 00:57:55,620 returns that data to. 1152 00:57:55,620 --> 00:57:59,460 >> So all of that information would be sent back to your controller. 1153 00:57:59,460 --> 00:58:02,620 Then you would probably do a bit more logic on that and maybe execute a few 1154 00:58:02,620 --> 00:58:06,510 more queries in the SQL database and then, finally, come up with a nicely 1155 00:58:06,510 --> 00:58:11,930 packed set of information that you would pass in into some other template 1156 00:58:11,930 --> 00:58:13,950 that displayed that information. 1157 00:58:13,950 --> 00:58:17,030 Now, how do we actually package that information up? 1158 00:58:17,030 --> 00:58:23,980 Well, we have a function called Render that was in the functions.php file in 1159 00:58:23,980 --> 00:58:29,950 pset7, where you pass in the name of a file, the name of a template. 1160 00:58:29,950 --> 00:58:32,160 >> And then you also pass in an associative array. 1161 00:58:32,160 --> 00:58:37,150 And so that associative array represents the different information 1162 00:58:37,150 --> 00:58:39,040 that you want to pass in. 1163 00:58:39,040 --> 00:58:43,460 Now, what's going to be constant in these examples is that the keys or, 1164 00:58:43,460 --> 00:58:47,070 rather, the keys of the associative arrays, those are what's going to be 1165 00:58:47,070 --> 00:58:51,050 expected to be constant by the template, because it knows it needs 1166 00:58:51,050 --> 00:58:53,990 something called message or called name. 1167 00:58:53,990 --> 00:58:56,940 And then the things on the right, the actual values, so in this case, who's 1168 00:58:56,940 --> 00:59:00,750 a good boy and Milo, those are going to be the values that are changing 1169 00:59:00,750 --> 00:59:05,610 that the controller changes every time or based on a certain condition and 1170 00:59:05,610 --> 00:59:07,120 will pass that in. 1171 00:59:07,120 --> 00:59:12,790 >> So here in templates, we see that we are using HTML special characters, 1172 00:59:12,790 --> 00:59:16,370 which just basically means that we want to get the peer string that the 1173 00:59:16,370 --> 00:59:17,580 user put in. 1174 00:59:17,580 --> 00:59:20,880 And we want to substitute message in there. 1175 00:59:20,880 --> 00:59:26,110 So then when we actually view the file, the specific 1176 00:59:26,110 --> 00:59:28,700 information is passed in. 1177 00:59:28,700 --> 00:59:33,850 Note that the key how render works is that the keys of the associative 1178 00:59:33,850 --> 00:59:37,170 arrays, those become variable names here. 1179 00:59:37,170 --> 00:59:40,720 And so the values of that key in the associative array then becomes the 1180 00:59:40,720 --> 00:59:41,970 value of the variable. 1181 00:59:41,970 --> 00:59:44,800 1182 00:59:44,800 --> 00:59:46,040 >> Now, let's move on to SQL. 1183 00:59:46,040 --> 00:59:48,010 It stands for Structured Query Language. 1184 00:59:48,010 --> 00:59:50,460 And so this is just a programming language designed 1185 00:59:50,460 --> 00:59:51,880 for managing databases. 1186 00:59:51,880 --> 00:59:56,860 And it came in handy for us in our pset7 finance website. 1187 00:59:56,860 --> 01:00:00,510 Essentially, it's just an easy way to track and manage objects and tables 1188 01:00:00,510 --> 01:00:02,070 and link them to each other. 1189 01:00:02,070 --> 01:00:06,860 Now, think of your SQL database basically as an Excel file, perhaps, 1190 01:00:06,860 --> 01:00:10,040 with multiple tabbed sheets. 1191 01:00:10,040 --> 01:00:13,820 >> So you could have multiple tables, perhaps, that linked to one another. 1192 01:00:13,820 --> 01:00:19,420 And much like Excel, we have a lot of the functionality that we want. 1193 01:00:19,420 --> 01:00:22,300 For instance, we can select certain rows. 1194 01:00:22,300 --> 01:00:24,110 We can insert information. 1195 01:00:24,110 --> 01:00:25,560 We can update rows. 1196 01:00:25,560 --> 01:00:27,440 And we can also delete things. 1197 01:00:27,440 --> 01:00:30,920 1198 01:00:30,920 --> 01:00:36,560 >> The SQL select works by selecting rows or a row of specified columns from a 1199 01:00:36,560 --> 01:00:39,640 database that match a certain criteria that you indicate. 1200 01:00:39,640 --> 01:00:44,930 So over here when I see select * from wizards where house = Ravenclaw, then 1201 01:00:44,930 --> 01:00:48,340 I'm selecting *, which means I'm selecting every single column in that 1202 01:00:48,340 --> 01:00:56,340 row from the wizards table but only if the house column equals Ravenclaw. 1203 01:00:56,340 --> 01:00:57,840 Now, this is pure or SQL. 1204 01:00:57,840 --> 01:01:02,680 So if I went into PHPmyadmin, which is the specific way that we use to manage 1205 01:01:02,680 --> 01:01:07,040 our SQL databases, then I could insert that into the PHPmyadmin website. 1206 01:01:07,040 --> 01:01:08,290 And that would execute. 1207 01:01:08,290 --> 01:01:11,280 >> But we actually want to do that on the PHP side. 1208 01:01:11,280 --> 01:01:12,580 So how do we do that? 1209 01:01:12,580 --> 01:01:20,180 Well, we use the query function, which basically executes that SQL query. 1210 01:01:20,180 --> 01:01:21,830 Using ? 1211 01:01:21,830 --> 01:01:25,850 as a placeholder, we can pass in certain values to our string that we 1212 01:01:25,850 --> 01:01:26,920 want to replace. 1213 01:01:26,920 --> 01:01:32,110 So perhaps I'm storing different values in the curr_house, which 1214 01:01:32,110 --> 01:01:34,400 represents the current house that I'm going through. 1215 01:01:34,400 --> 01:01:39,040 So I can pass that in as a placeholder with the question mark. 1216 01:01:39,040 --> 01:01:43,290 And then I'll basically execute the same thing as I did before, except 1217 01:01:43,290 --> 01:01:45,550 now, I'm in PHP. 1218 01:01:45,550 --> 01:01:51,300 >> And query will return an associative array. 1219 01:01:51,300 --> 01:01:53,470 And I'm going to store it in rows. 1220 01:01:53,470 --> 01:01:56,880 Now, query can always fail. 1221 01:01:56,880 --> 01:02:02,870 Perhaps the SQL query couldn't execute because the table didn't exist. 1222 01:02:02,870 --> 01:02:04,310 Or perhaps, the column didn't exist. 1223 01:02:04,310 --> 01:02:05,400 Something went wrong. 1224 01:02:05,400 --> 01:02:08,170 Well, in that case, you'll want to make sure that you check whether the 1225 01:02:08,170 --> 01:02:09,700 query returned false. 1226 01:02:09,700 --> 01:02:15,590 And that's by using the triple equals operation there. 1227 01:02:15,590 --> 01:02:19,660 >> And then I apologize, which is another CS50 function, passing in a message. 1228 01:02:19,660 --> 01:02:23,435 And if you look into apologize, all it really does is render apology.php. 1229 01:02:23,435 --> 01:02:26,100 1230 01:02:26,100 --> 01:02:26,410 Yeah. 1231 01:02:26,410 --> 01:02:29,630 >> AUDIENCE: Could you explain what that star does between select and from? 1232 01:02:29,630 --> 01:02:30,710 >> ZAMYLA CHAN: Yeah, absolutely. 1233 01:02:30,710 --> 01:02:35,220 So the star in between select and from means that I want to select the whole 1234 01:02:35,220 --> 01:02:37,440 entire row from my table. 1235 01:02:37,440 --> 01:02:41,900 I could've indicated select name, year, house. 1236 01:02:41,900 --> 01:02:46,160 And I would only get those three columns in my table. 1237 01:02:46,160 --> 01:02:51,560 But if I say select *, then I'll get everything in that column. 1238 01:02:51,560 --> 01:02:53,760 Then I'm going to go you in the back first. 1239 01:02:53,760 --> 01:02:57,656 >> AUDIENCE: So this is still in SQL, right? 1240 01:02:57,656 --> 01:02:59,610 Is this query or is this PHP? 1241 01:02:59,610 --> 01:03:00,550 >> ZAMYLA CHAN: We're in a query. 1242 01:03:00,550 --> 01:03:01,940 So this is in PHP. 1243 01:03:01,940 --> 01:03:06,280 So using the PHP function query, we're executing a SQL query. 1244 01:03:06,280 --> 01:03:11,988 1245 01:03:11,988 --> 01:03:15,364 >> AUDIENCE: Is anything in SQL case-sensitive, like select 1246 01:03:15,364 --> 01:03:17,834 or wizards or house? 1247 01:03:17,834 --> 01:03:20,050 >> ZAMYLA CHAN: Is anything in SQL case-sensitive? 1248 01:03:20,050 --> 01:03:21,760 I believe so, yes. 1249 01:03:21,760 --> 01:03:24,620 I believe that SELECT and FROM and WHERE are case-sensitive. 1250 01:03:24,620 --> 01:03:25,535 No? 1251 01:03:25,535 --> 01:03:27,500 >> ROB BOWDEN: So, it's the opposite. 1252 01:03:27,500 --> 01:03:32,030 The column names and the table means, all of those are case-sensitive. 1253 01:03:32,030 --> 01:03:35,470 But any of the MySQL key words, like SELECT, FROM, and WHERE, those are not 1254 01:03:35,470 --> 01:03:36,140 case-sensitive. 1255 01:03:36,140 --> 01:03:36,420 OK. 1256 01:03:36,420 --> 01:03:37,780 So the opposite of what I said. 1257 01:03:37,780 --> 01:03:40,420 So all of the MySQL keywords-- 1258 01:03:40,420 --> 01:03:42,670 select, from, where-- those are not case-sensitive. 1259 01:03:42,670 --> 01:03:44,630 But everything else is. 1260 01:03:44,630 --> 01:03:45,210 OK. 1261 01:03:45,210 --> 01:03:46,500 You in the front. 1262 01:03:46,500 --> 01:03:52,041 >> AUDIENCE: If I have $ rows in terms of more than one row, does that mean is 1263 01:03:52,041 --> 01:03:53,640 just becomes an associative array? 1264 01:03:53,640 --> 01:03:59,550 >> ZAMYLA CHAN: So the question was if rows has more than one row in it, does 1265 01:03:59,550 --> 01:04:01,800 it become an associative array? 1266 01:04:01,800 --> 01:04:05,680 So it is an array of associative arrays already. 1267 01:04:05,680 --> 01:04:10,730 So even if there's only one row returned, then you'd have to go to 1268 01:04:10,730 --> 01:04:12,690 index 0 of that result. 1269 01:04:12,690 --> 01:04:15,316 And then you'd have that first row. 1270 01:04:15,316 --> 01:04:17,482 Yes, Belinda? 1271 01:04:17,482 --> 01:04:21,258 >> AUDIENCE: When you use ===, is this the only instance? 1272 01:04:21,258 --> 01:04:22,210 Or are there others? 1273 01:04:22,210 --> 01:04:26,815 >> ZAMYLA CHAN: So in this case, === is a comparison across types. 1274 01:04:26,815 --> 01:04:29,870 1275 01:04:29,870 --> 01:04:34,050 Sorry. === is a comparison that compares the types. 1276 01:04:34,050 --> 01:04:37,620 And then == compares across all types. 1277 01:04:37,620 --> 01:04:41,620 >> AUDIENCE: Can you explain what rows is in this situation? 1278 01:04:41,620 --> 01:04:45,120 Is it row of data? 1279 01:04:45,120 --> 01:04:48,100 >> ZAMYLA CHAN: In the next slide, I'm going to explain what rows is. 1280 01:04:48,100 --> 01:04:49,890 So if you don't mind holding off on that. 1281 01:04:49,890 --> 01:04:50,620 And then you in the back? 1282 01:04:50,620 --> 01:04:54,699 >> AUDIENCE: For functions like query, render and apologize [INAUDIBLE]? 1283 01:04:54,699 --> 01:04:59,050 1284 01:04:59,050 --> 01:05:03,050 >> ZAMYLA CHAN: The question was whether these functions-- query, apologize, 1285 01:05:03,050 --> 01:05:04,510 and render-- 1286 01:05:04,510 --> 01:05:05,930 are common across PHP. 1287 01:05:05,930 --> 01:05:09,460 These are ones that CS50 wrote for pset7. 1288 01:05:09,460 --> 01:05:09,910 And Jay? 1289 01:05:09,910 --> 01:05:15,333 >> AUDIENCE: When you need to say $_session, is that only for IDs? 1290 01:05:15,333 --> 01:05:17,310 Or could you have said that here? 1291 01:05:17,310 --> 01:05:23,440 >> ZAMYLA CHAN: So the question was, when we use $_session, that was a specific 1292 01:05:23,440 --> 01:05:25,290 global variable that we're using. 1293 01:05:25,290 --> 01:05:32,080 Here this variable is going to be local to our function. 1294 01:05:32,080 --> 01:05:36,588 So we're just declaring a new variable. 1295 01:05:36,588 --> 01:05:38,460 >> AUDIENCE: How is apologize implemented? 1296 01:05:38,460 --> 01:05:40,960 >> ZAMYLA CHAN: The question was, how is apologize implemented? 1297 01:05:40,960 --> 01:05:44,180 And I think this is actually a pretty good practice for you guys to go into 1298 01:05:44,180 --> 01:05:49,260 the functions.php section and look at apologize and see how you could have 1299 01:05:49,260 --> 01:05:50,670 done it yourself. 1300 01:05:50,670 --> 01:05:55,620 So I may leave that to you but just say that if you look at apologize, 1301 01:05:55,620 --> 01:06:02,110 then it takes the message that you submitted to apologize, and then it 1302 01:06:02,110 --> 01:06:06,570 renders that message. 1303 01:06:06,570 --> 01:06:08,240 Any more questions? 1304 01:06:08,240 --> 01:06:08,710 I love questions. 1305 01:06:08,710 --> 01:06:09,555 So keep them coming. 1306 01:06:09,555 --> 01:06:11,888 >> AUDIENCE: [INAUDIBLE] 1307 01:06:11,888 --> 01:06:13,840 echo or print there? 1308 01:06:13,840 --> 01:06:15,900 >> ZAMYLA CHAN: The question was, could we not just have put 1309 01:06:15,900 --> 01:06:17,000 echo or print there. 1310 01:06:17,000 --> 01:06:19,710 So that would have done something slightly different. 1311 01:06:19,710 --> 01:06:23,750 That would have printed query failed into that-- 1312 01:06:23,750 --> 01:06:27,420 well, right now, we're actually in our controller. 1313 01:06:27,420 --> 01:06:30,350 So we don't actually have HTML set up here. 1314 01:06:30,350 --> 01:06:34,946 Apologize by rendering apologize.php actually redirects you to apology.php. 1315 01:06:34,946 --> 01:06:39,560 1316 01:06:39,560 --> 01:06:42,200 OK. 1317 01:06:42,200 --> 01:06:45,880 >> So now, let's go on to address the question from earlier about what 1318 01:06:45,880 --> 01:06:47,330 really is rows. 1319 01:06:47,330 --> 01:06:51,960 Well, query will return an array of rows. 1320 01:06:51,960 --> 01:06:55,020 And every row is represented by an associative array. 1321 01:06:55,020 --> 01:07:02,840 So if I've executed some SQL query and I've stored the result in rows, then 1322 01:07:02,840 --> 01:07:07,850 using a foreach loop, then the array name is the first one there-- rows. 1323 01:07:07,850 --> 01:07:13,170 And then I'm going to call every row in there $row. 1324 01:07:13,170 --> 01:07:20,060 >> So iterating over that, I can then access the given row's name column, 1325 01:07:20,060 --> 01:07:22,340 year column, and house column. 1326 01:07:22,340 --> 01:07:28,010 Note that I wouldn't have been able to do this with rows, because rows index 1327 01:07:28,010 --> 01:07:29,290 name doesn't exist. 1328 01:07:29,290 --> 01:07:31,970 Rows is just an array of associative arrays. 1329 01:07:31,970 --> 01:07:34,870 So you have two levels there. 1330 01:07:34,870 --> 01:07:37,170 Once you have the array of rows, you have to get into that. 1331 01:07:37,170 --> 01:07:39,110 And then you can access the columns. 1332 01:07:39,110 --> 01:07:41,636 Did that make it clear? 1333 01:07:41,636 --> 01:07:42,520 Yeah, in front? 1334 01:07:42,520 --> 01:07:45,490 >> AUDIENCE: [INAUDIBLE] open brackets for [INAUDIBLE]? 1335 01:07:45,490 --> 01:07:46,220 >> ZAMYLA CHAN: Pardon me? 1336 01:07:46,220 --> 01:07:49,740 >> AUDIENCE: The open brackets. 1337 01:07:49,740 --> 01:07:52,420 >> ZAMYLA CHAN: These here? 1338 01:07:52,420 --> 01:07:58,520 That's allowing me to include that variable. 1339 01:07:58,520 --> 01:07:58,670 Yeah. 1340 01:07:58,670 --> 01:08:01,900 >> AUDIENCE: When you print, are you printing to the HTML code? 1341 01:08:01,900 --> 01:08:03,110 >> ZAMYLA CHAN: Yes. 1342 01:08:03,110 --> 01:08:07,720 When I print, this here is inside my template now, so 1343 01:08:07,720 --> 01:08:10,310 my view of MVC method. 1344 01:08:10,310 --> 01:08:12,750 So I'm printing into the HTML. 1345 01:08:12,750 --> 01:08:16,670 >> AUDIENCE: So if we went into developer tools after running this, we could 1346 01:08:16,670 --> 01:08:17,160 that actually in code? 1347 01:08:17,160 --> 01:08:18,410 >> ZAMYLA CHAN: That's a great question, yeah. 1348 01:08:18,410 --> 01:08:22,359 So if you went into the developer tools in Firefox using Firebug or 1349 01:08:22,359 --> 01:08:26,109 Chrome, then yeah, you could see the specific HTML. 1350 01:08:26,109 --> 01:08:28,470 So it wouldn't show $row["Name"]. 1351 01:08:28,470 --> 01:08:32,524 It would show whichever name is in that row. 1352 01:08:32,524 --> 01:08:36,268 >> AUDIENCE: Just a general issue, what are tr and td defined as? 1353 01:08:36,268 --> 01:08:37,672 Why would we [INAUDIBLE]? 1354 01:08:37,672 --> 01:08:41,850 1355 01:08:41,850 --> 01:08:44,814 >> ZAMYLA CHAN: Table row tr, table then td column. 1356 01:08:44,814 --> 01:08:48,060 1357 01:08:48,060 --> 01:08:49,310 OK. 1358 01:08:49,310 --> 01:08:55,771 1359 01:08:55,771 --> 01:08:56,835 >> AUDIENCE: Yeah, it's table data. 1360 01:08:56,835 --> 01:08:58,770 >> ZAMYLA CHAN: Table data. 1361 01:08:58,770 --> 01:08:59,894 Yeah. 1362 01:08:59,894 --> 01:09:08,670 >> AUDIENCE: It's a row in which the row is treated like a column? 1363 01:09:08,670 --> 01:09:08,910 >> ZAMYLA CHAN: Sorry. 1364 01:09:08,910 --> 01:09:10,570 Can you repeat that? 1365 01:09:10,570 --> 01:09:14,450 >> AUDIENCE: How would you visualize rows? 1366 01:09:14,450 --> 01:09:16,310 >> ZAMYLA CHAN: How would you visualize rows in what kind of way? 1367 01:09:16,310 --> 01:09:21,796 Are you talking about these rows here or the tr rows? 1368 01:09:21,796 --> 01:09:22,630 >> AUDIENCE: The rows. 1369 01:09:22,630 --> 01:09:25,229 >> ZAMYLA CHAN: These rows here? 1370 01:09:25,229 --> 01:09:28,620 I'd visualize this as I execute my query. 1371 01:09:28,620 --> 01:09:38,729 And it says, OK, I have either 0 to n amount of rows that match the criteria 1372 01:09:38,729 --> 01:09:40,510 that you had queried. 1373 01:09:40,510 --> 01:09:43,740 So I have some number of rows. 1374 01:09:43,740 --> 01:09:51,450 So rows, the $rows, stores each one of those rows in an array. 1375 01:09:51,450 --> 01:09:58,110 So even if it's just one of them, it's still an array of rows that match it. 1376 01:09:58,110 --> 01:10:03,010 >> So then, for instance, this is similar to when you fetched 1377 01:10:03,010 --> 01:10:05,390 the cache from users. 1378 01:10:05,390 --> 01:10:10,810 And the criteria there was where ID equals the session ID. 1379 01:10:10,810 --> 01:10:14,250 There really only is one row that could match that. 1380 01:10:14,250 --> 01:10:18,960 But still rows just returned one row. 1381 01:10:18,960 --> 01:10:22,620 So you'd have to go to rows, index 0, index cache to 1382 01:10:22,620 --> 01:10:26,195 actually get to your cache. 1383 01:10:26,195 --> 01:10:29,650 >> AUDIENCE: Is the print function in echo the same thing? 1384 01:10:29,650 --> 01:10:30,670 >> ZAMYLA CHAN: Yes. 1385 01:10:30,670 --> 01:10:31,190 Yes. 1386 01:10:31,190 --> 01:10:33,304 Print an echo of the same. 1387 01:10:33,304 --> 01:10:42,400 >> AUDIENCE: Is the foreach loop the only way to index into rows? 1388 01:10:42,400 --> 01:10:46,110 >> ZAMYLA CHAN: Is a foreach loop the only way that you can 1389 01:10:46,110 --> 01:10:47,030 iterate through rows? 1390 01:10:47,030 --> 01:10:47,180 No. 1391 01:10:47,180 --> 01:10:51,000 You can also use a for loop, provided that you know the length 1392 01:10:51,000 --> 01:10:53,024 of the row's array. 1393 01:10:53,024 --> 01:10:58,500 >> AUDIENCE: Could you access it using a row as [INAUDIBLE]? 1394 01:10:58,500 --> 01:11:01,640 >> ZAMYLA CHAN: So you can't access it just using row if you don't have a 1395 01:11:01,640 --> 01:11:05,160 foreach loop provided that you haven't declared row. 1396 01:11:05,160 --> 01:11:08,150 1397 01:11:08,150 --> 01:11:09,660 Yes. 1398 01:11:09,660 --> 01:11:10,810 Yeah, in the white. 1399 01:11:10,810 --> 01:11:13,990 >> AUDIENCE: So what do tr and td do? 1400 01:11:13,990 --> 01:11:16,790 >> ZAMYLA CHAN: So tr and td are HTML tags. 1401 01:11:16,790 --> 01:11:19,590 tr indicates the beginning of a table row. 1402 01:11:19,590 --> 01:11:26,625 And each td indicates a new table data column. 1403 01:11:26,625 --> 01:11:32,275 >> AUDIENCE: For a visual of what a row is like, just imagine the SQL, how 1404 01:11:32,275 --> 01:11:33,510 they have a row. 1405 01:11:33,510 --> 01:11:35,980 [INAUDIBLE]. 1406 01:11:35,980 --> 01:11:36,390 >> ZAMYLA CHAN: Yeah. 1407 01:11:36,390 --> 01:11:37,630 That's a great point. 1408 01:11:37,630 --> 01:11:41,510 You can visualize rows as just like in an Excel table, just 1409 01:11:41,510 --> 01:11:44,540 the list of the rows. 1410 01:11:44,540 --> 01:11:46,870 OK. 1411 01:11:46,870 --> 01:11:47,230 All right. 1412 01:11:47,230 --> 01:11:50,740 So now that we've gone over select, if there aren't any more questions, we'll 1413 01:11:50,740 --> 01:11:52,970 go over onto insert. 1414 01:11:52,970 --> 01:11:58,220 So if I wanted to insert into some table and insert certain column 1415 01:11:58,220 --> 01:12:02,320 values, I could insert myself into Ravenclaw in year 7. 1416 01:12:02,320 --> 01:12:07,245 But sometimes there might be duplicate values, as we saw in pset7 when we 1417 01:12:07,245 --> 01:12:09,240 were updating our portfolio. 1418 01:12:09,240 --> 01:12:15,610 >> So in this case, we want to use ON DUPLICATE KEY UPDATE, so that we don't 1419 01:12:15,610 --> 01:12:18,280 store multiple rows with the same value but rather update it. 1420 01:12:18,280 --> 01:12:21,780 1421 01:12:21,780 --> 01:12:25,560 Then we actually have update, which isn't an insert. 1422 01:12:25,560 --> 01:12:30,100 It's just an update where you update in a certain table with a given 1423 01:12:30,100 --> 01:12:34,403 criteria and then, finally, delete, which does a very similar thing. 1424 01:12:34,403 --> 01:12:40,200 >> AUDIENCE: Could you briefly go over the duplicate key? 1425 01:12:40,200 --> 01:12:40,850 >> ZAMYLA CHAN: Yeah. 1426 01:12:40,850 --> 01:12:48,340 Essentially here, I have INSERT INTO gringotts, is, galleons, these values. 1427 01:12:48,340 --> 01:12:56,900 But ID, presumably, is a unique key value set up in MySQL table. 1428 01:12:56,900 --> 01:13:06,750 So if I already have that ID set up, then I can't insert a new row. 1429 01:13:06,750 --> 01:13:10,961 So if it doesn't exist already, then I have to update it. 1430 01:13:10,961 --> 01:13:16,040 1431 01:13:16,040 --> 01:13:17,722 In the middle in the white. 1432 01:13:17,722 --> 01:13:21,570 >> AUDIENCE: So insert, update, delete, and select, are those all available 1433 01:13:21,570 --> 01:13:22,875 locally [INAUDIBLE]? 1434 01:13:22,875 --> 01:13:27,320 1435 01:13:27,320 --> 01:13:29,630 >> ZAMYLA CHAN: So insert, update, delete, and 1436 01:13:29,630 --> 01:13:31,120 select are all SQL queries. 1437 01:13:31,120 --> 01:13:34,970 So whenever you're using SQL, you'll have those available. 1438 01:13:34,970 --> 01:13:38,226 1439 01:13:38,226 --> 01:13:40,706 >> AUDIENCE: Back to the past quizzes-- 1440 01:13:40,706 --> 01:13:44,178 there was a question that dealt with if you had a table and wanted to 1441 01:13:44,178 --> 01:13:52,610 insert test scores in one and you insert your name so it won't let you 1442 01:13:52,610 --> 01:13:54,594 [INAUDIBLE] your friend's test score. 1443 01:13:54,594 --> 01:13:59,060 How would you do that with insert? 1444 01:13:59,060 --> 01:14:02,490 >> ZAMYLA CHAN: So the question was about a previous mid-term question. 1445 01:14:02,490 --> 01:14:04,320 I'm not aware of which one it is right now. 1446 01:14:04,320 --> 01:14:06,790 So maybe afterwards, if you want to come up and show me, then I can 1447 01:14:06,790 --> 01:14:08,155 certainly give you tips. 1448 01:14:08,155 --> 01:14:11,250 1449 01:14:11,250 --> 01:14:15,460 But speaking of inserting things, like taking someone's score when you 1450 01:14:15,460 --> 01:14:18,800 shouldn't, let's talk about SQL injection attacks. 1451 01:14:18,800 --> 01:14:22,050 So a SQL injection attack is essentially where someone takes 1452 01:14:22,050 --> 01:14:27,680 advantage of the low security of the way that you are taking in data. 1453 01:14:27,680 --> 01:14:35,660 >> So in here, just like in CS50 finance, when we logged in, we can enter in a 1454 01:14:35,660 --> 01:14:39,510 username in the login form, the first text box, and 1455 01:14:39,510 --> 01:14:41,570 then enter in a password. 1456 01:14:41,570 --> 01:14:45,610 Perhaps our PHP code might look something like this, where $ username 1457 01:14:45,610 --> 01:14:51,410 is the post-data username and password is post-data password. 1458 01:14:51,410 --> 01:14:55,110 And then we just execute our query, say, OK, well, our query is going to 1459 01:14:55,110 --> 01:14:57,990 select from our users, where the username is the 1460 01:14:57,990 --> 01:14:58,970 one that they submitted. 1461 01:14:58,970 --> 01:15:06,870 And the password is the password, meaning that the passwords match. 1462 01:15:06,870 --> 01:15:12,670 >> Now, what if instead of actually submitting an actual password, like 1463 01:15:12,670 --> 01:15:16,850 12345 and guessing at prong that says password and trying to hack their 1464 01:15:16,850 --> 01:15:21,460 account, what if instead they submitted this. 1465 01:15:21,460 --> 01:15:24,380 They could type in maybe a guess at a password. 1466 01:15:24,380 --> 01:15:30,480 And then they would finish the quote then type in or 1 = 1. 1467 01:15:30,480 --> 01:15:35,160 That would pace directly into the SQL query to look something like this. 1468 01:15:35,160 --> 01:15:38,200 Select from users where username = prongs and password 1469 01:15:38,200 --> 01:15:41,450 equals lily or 1 = 1. 1470 01:15:41,450 --> 01:15:45,450 >> So either the password has to be correct or 1 = 1, 1471 01:15:45,450 --> 01:15:47,870 which is always true. 1472 01:15:47,870 --> 01:15:52,780 So in this case, basically, a user can take advantage of this and just log 1473 01:15:52,780 --> 01:15:55,240 themselves in and hack someone's account. 1474 01:15:55,240 --> 01:15:58,950 So that's why we want to avoid someone having to do this. 1475 01:15:58,950 --> 01:16:03,890 But luckily, the query function by passing in the placeholders will take 1476 01:16:03,890 --> 01:16:04,800 care of this for you. 1477 01:16:04,800 --> 01:16:08,570 Also, you'll typically never want to actually submit the passwords 1478 01:16:08,570 --> 01:16:09,310 themselves. 1479 01:16:09,310 --> 01:16:14,115 That's why we hashed or encrypted them in CS50 finance. 1480 01:16:14,115 --> 01:16:17,553 >> AUDIENCE: The past quiz talked about MySQL escape strings. 1481 01:16:17,553 --> 01:16:21,800 Do we have to worry about that? 1482 01:16:21,800 --> 01:16:22,670 >> ZAMYLA CHAN: That's a good question. 1483 01:16:22,670 --> 01:16:29,050 The MySQL escape strings is definitely a function that was used in our query. 1484 01:16:29,050 --> 01:16:30,420 But definitely look into that. 1485 01:16:30,420 --> 01:16:32,950 I'd say that's fair game to know that you'd need to call that 1486 01:16:32,950 --> 01:16:34,370 function on a string. 1487 01:16:34,370 --> 01:16:35,245 Yeah, Belinda? 1488 01:16:35,245 --> 01:16:38,606 >> AUDIENCE: How do you know when it's single quotes or double quotes? 1489 01:16:38,606 --> 01:16:43,516 And also, I feel like in lecture you mentioned something about not having 1490 01:16:43,516 --> 01:16:48,426 the [INAUDIBLE] or something or the second single quote at the end. 1491 01:16:48,426 --> 01:16:56,407 1492 01:16:56,407 --> 01:17:00,825 I think he pointed out in lecture that you're supposed to have apostrophe 1 1493 01:17:00,825 --> 01:17:04,290 and then not have apostrophes or something. 1494 01:17:04,290 --> 01:17:05,540 >> AUDIENCE: [INAUDIBLE]. 1495 01:17:05,540 --> 01:17:14,860 1496 01:17:14,860 --> 01:17:18,657 >> AUDIENCE: The thing is the last single quote in there in that second box 1497 01:17:18,657 --> 01:17:19,391 shouldn't be there. 1498 01:17:19,391 --> 01:17:21,840 [INAUDIBLE] 1499 01:17:21,840 --> 01:17:26,730 Because when you take that last single quote out and match these for content 1500 01:17:26,730 --> 01:17:30,522 where the password is, if you have that query, there's a single quote at 1501 01:17:30,522 --> 01:17:31,340 the end already. 1502 01:17:31,340 --> 01:17:36,580 You want to use that single quote as the one that faces the one 1503 01:17:36,580 --> 01:17:37,574 [INAUDIBLE]. 1504 01:17:37,574 --> 01:17:42,820 So what's actually in that text box should not have that. 1505 01:17:42,820 --> 01:17:44,070 >> ZAMYLA CHAN: I'll change that. 1506 01:17:44,070 --> 01:17:56,490 1507 01:17:56,490 --> 01:17:56,880 OK. 1508 01:17:56,880 --> 01:18:00,435 If there aren't any questions, then I'll pass it over to Joseph to talk 1509 01:18:00,435 --> 01:18:04,332 about JavaScript, et cetera. 1510 01:18:04,332 --> 01:18:09,540 >> [APPLAUSE] 1511 01:18:09,540 --> 01:18:11,660 >> JOSEPH ONG: So we are running a little bit behind. 1512 01:18:11,660 --> 01:18:13,280 So if you have to leave, that's OK. 1513 01:18:13,280 --> 01:18:16,540 But we ask that you keep your heads down if you're in the middle, so you 1514 01:18:16,540 --> 01:18:19,560 don't block the camera and you use the back exit if you have to. 1515 01:18:19,560 --> 01:18:25,010 1516 01:18:25,010 --> 01:18:26,306 I'm Joseph by the way. 1517 01:18:26,306 --> 01:18:27,556 Hi. 1518 01:18:27,556 --> 01:18:37,910 1519 01:18:37,910 --> 01:18:38,630 Test, test. 1520 01:18:38,630 --> 01:18:39,630 Dan, is that good? 1521 01:18:39,630 --> 01:18:40,880 Cool. 1522 01:18:40,880 --> 01:18:44,460 1523 01:18:44,460 --> 01:18:47,600 >> So the video will also be posted online for those that 1524 01:18:47,600 --> 01:18:48,850 have to leave now. 1525 01:18:48,850 --> 01:18:51,350 1526 01:18:51,350 --> 01:18:52,600 Awkward. 1527 01:18:52,600 --> 01:18:56,300 1528 01:18:56,300 --> 01:18:57,510 OK. 1529 01:18:57,510 --> 01:18:59,730 So quiz review. 1530 01:18:59,730 --> 01:19:00,980 This is a cat. 1531 01:19:00,980 --> 01:19:03,150 1532 01:19:03,150 --> 01:19:07,170 Now, JavaScript, which is maybe not as aww for some of you guys. 1533 01:19:07,170 --> 01:19:07,370 OK. 1534 01:19:07,370 --> 01:19:09,280 So that's first, recall from Zamyla. 1535 01:19:09,280 --> 01:19:11,270 Remember that PHP is run on the server. 1536 01:19:11,270 --> 01:19:15,230 And lots of times, you guys wrote loops in PHP to print out HTML, right? 1537 01:19:15,230 --> 01:19:19,170 So once that code executes, that HTML output that you print out 1538 01:19:19,170 --> 01:19:20,500 gets sent to the user. 1539 01:19:20,500 --> 01:19:25,960 And once that happens, no more PHP can be run, unless you reload the page, of 1540 01:19:25,960 --> 01:19:27,660 course, which reexecutes the PHP. 1541 01:19:27,660 --> 01:19:30,080 But once you print out that HTML, you can't go anywhere. 1542 01:19:30,080 --> 01:19:33,580 >> So that HTML is sent over to the user, which is the browser over here, where 1543 01:19:33,580 --> 01:19:35,470 Milo is using the computer. 1544 01:19:35,470 --> 01:19:40,340 And so well, there are several things once we send HTML to the user. 1545 01:19:40,340 --> 01:19:43,350 Sometimes we want to do something like when you click on something, we want 1546 01:19:43,350 --> 01:19:47,350 alert boxes to pop up, those sorts of interactions, like when you press the 1547 01:19:47,350 --> 01:19:50,740 key, when you click something on the page, I want something to happen. 1548 01:19:50,740 --> 01:19:54,550 Well, you can't reexecute PHP code once that HTML is set. 1549 01:19:54,550 --> 01:19:55,900 So how do you do this? 1550 01:19:55,900 --> 01:19:59,930 >> We introduce a new language called JavaScript, which runs in the browser 1551 01:19:59,930 --> 01:20:02,890 that allows you to do things to HTML after you receive 1552 01:20:02,890 --> 01:20:04,020 them from the server. 1553 01:20:04,020 --> 01:20:07,050 And this is why we call it a client-side programming language. 1554 01:20:07,050 --> 01:20:09,110 It works on your computer-- 1555 01:20:09,110 --> 01:20:11,400 the client. 1556 01:20:11,400 --> 01:20:13,830 Any questions about that so far? 1557 01:20:13,830 --> 01:20:15,650 That paradigm makes sense to people? 1558 01:20:15,650 --> 01:20:16,010 OK. 1559 01:20:16,010 --> 01:20:16,790 Good. 1560 01:20:16,790 --> 01:20:17,340 All right. 1561 01:20:17,340 --> 01:20:20,850 So the first thing to note is JavaScript is not PHP. 1562 01:20:20,850 --> 01:20:23,010 >> They have some different syntax, which we'll go into. 1563 01:20:23,010 --> 01:20:24,530 And they have very different uses. 1564 01:20:24,530 --> 01:20:26,640 JavaScript, again, for your browser, for the client. 1565 01:20:26,640 --> 01:20:30,640 Server runs somewhere on someone else's computer that sends information 1566 01:20:30,640 --> 01:20:31,720 to you, correct? 1567 01:20:31,720 --> 01:20:36,730 So if we ask you to write PHP code on an exam question, don't write 1568 01:20:36,730 --> 01:20:38,710 JavaScript and vice versa. 1569 01:20:38,710 --> 01:20:41,710 You'll just lose points, and it won't be right. 1570 01:20:41,710 --> 01:20:43,690 >> So let's get into some syntax differences-- 1571 01:20:43,690 --> 01:20:46,140 JavaScript on the left and PHP on the right. 1572 01:20:46,140 --> 01:20:48,670 The first thing you'll notice with JavaScript, we declare variables with 1573 01:20:48,670 --> 01:20:49,440 the var keyword-- 1574 01:20:49,440 --> 01:20:53,590 V-A-R. PHP used the dollar sign, as Zamyla discussed earlier. 1575 01:20:53,590 --> 01:20:57,570 If you want to declare an associative array, we see the familiar syntax on 1576 01:20:57,570 --> 01:20:59,030 the right side with PHP. 1577 01:20:59,030 --> 01:21:01,630 On the left side, instead you use curly braces. 1578 01:21:01,630 --> 01:21:03,280 And then your keys are on the left. 1579 01:21:03,280 --> 01:21:04,670 Then you have a colon. 1580 01:21:04,670 --> 01:21:06,560 And then you have the values that you want. 1581 01:21:06,560 --> 01:21:09,840 >> So this is how you would do it in PHP on the right side with that second 1582 01:21:09,840 --> 01:21:10,955 line that starts at Milo. 1583 01:21:10,955 --> 01:21:14,540 And that is how you would do it on the left side in JavaScript if you want 1584 01:21:14,540 --> 01:21:16,110 what we call an object. 1585 01:21:16,110 --> 01:21:19,340 And objects in JavaScript are just associative arrays. 1586 01:21:19,340 --> 01:21:23,710 So if you want to access fields, in PHP you use this bracket syntax. 1587 01:21:23,710 --> 01:21:27,170 And this way, you can reassign this owner field to Lauren. 1588 01:21:27,170 --> 01:21:30,150 >> Well, in JavaScript, if one want to access a field and change it, you can 1589 01:21:30,150 --> 01:21:31,370 use the dot syntax. 1590 01:21:31,370 --> 01:21:33,860 You can also use the bracket syntax. 1591 01:21:33,860 --> 01:21:35,550 But you can't use the dot syntax in PHP. 1592 01:21:35,550 --> 01:21:36,525 That won't work. 1593 01:21:36,525 --> 01:21:38,910 It only works in PHP. 1594 01:21:38,910 --> 01:21:42,060 And finally, to print things to the console, you use console.log, which 1595 01:21:42,060 --> 01:21:43,720 you guys use a lot in pset8. 1596 01:21:43,720 --> 01:21:45,260 You can console.log that. 1597 01:21:45,260 --> 01:21:50,030 >> If you want to print an array in PHP, you have to use print r. 1598 01:21:50,030 --> 01:21:53,240 And on the right side, you see i hash string concatenation over there. 1599 01:21:53,240 --> 01:21:54,070 Someone asked earlier. 1600 01:21:54,070 --> 01:21:55,850 I use a plus in JavaScript. 1601 01:21:55,850 --> 01:21:59,800 If I want to concatenate something in PHP, I use the dot. 1602 01:21:59,800 --> 01:22:00,800 These are different. 1603 01:22:00,800 --> 01:22:03,420 If you're writing PHP code, don't use a plus. 1604 01:22:03,420 --> 01:22:06,810 If you're writing JavaScript code, don't write a dot. 1605 01:22:06,810 --> 01:22:08,230 It will be wrong. 1606 01:22:08,230 --> 01:22:09,640 And you'll be sad. 1607 01:22:09,640 --> 01:22:11,000 >> So syntax differences. 1608 01:22:11,000 --> 01:22:14,710 Know your syntax, because if you have to write a question and you use syntax 1609 01:22:14,710 --> 01:22:16,890 from the wrong language, it will not work. 1610 01:22:16,890 --> 01:22:19,520 And it will be wrong. 1611 01:22:19,520 --> 01:22:22,420 So let's talk about some control flow differences, how you use 1612 01:22:22,420 --> 01:22:24,040 loops in each of them. 1613 01:22:24,040 --> 01:22:26,610 Zamyla went over the right side. 1614 01:22:26,610 --> 01:22:27,780 Stuff on the right side should be familiar. 1615 01:22:27,780 --> 01:22:29,670 Let's look at on the left side. 1616 01:22:29,670 --> 01:22:34,830 >> When you use for n loop in JavaScript, your loop variable, var i over there, 1617 01:22:34,830 --> 01:22:37,210 loop over the keys of the array. 1618 01:22:37,210 --> 01:22:39,150 So you see name, house, and role. 1619 01:22:39,150 --> 01:22:42,440 If I console.log i, I get name, house, and role. 1620 01:22:42,440 --> 01:22:44,720 Those are the keys. 1621 01:22:44,720 --> 01:22:50,080 In JavaScript, a foreach loop goes over the values of this array. 1622 01:22:50,080 --> 01:22:52,040 So you notice they're both i. 1623 01:22:52,040 --> 01:22:56,000 But in here on the PHP side, it prints out Milo, CS50, and Mascot. 1624 01:22:56,000 --> 01:22:57,390 Those are the values in PHP. 1625 01:22:57,390 --> 01:23:00,180 >> So these are how these two are different in the different languages. 1626 01:23:00,180 --> 01:23:02,970 So if you're using a foreach loop, don't assume that it 1627 01:23:02,970 --> 01:23:03,760 gives you the keys. 1628 01:23:03,760 --> 01:23:08,010 And if you're using a for n loop, don't assume it gives you the values. 1629 01:23:08,010 --> 01:23:10,420 Does this make sense so far? 1630 01:23:10,420 --> 01:23:13,630 The next slide is going to show you how you can access the opposite in 1631 01:23:13,630 --> 01:23:15,160 each of them. 1632 01:23:15,160 --> 01:23:18,550 >> Well, if you have the key in JavaScript and you want the value out, 1633 01:23:18,550 --> 01:23:20,440 you just index into the array with that. 1634 01:23:20,440 --> 01:23:23,640 So Milo of i will get you what you want-- the values. 1635 01:23:23,640 --> 01:23:25,260 There's this different syntax in PHP. 1636 01:23:25,260 --> 01:23:28,140 If you really want to know it, I don't think we've showed it to you yet. 1637 01:23:28,140 --> 01:23:31,740 But if you're interested, you can use this additional syntax on the right 1638 01:23:31,740 --> 01:23:35,130 side that will actually let you get the keys in PHP when you're using a 1639 01:23:35,130 --> 01:23:35,880 foreach loop. 1640 01:23:35,880 --> 01:23:39,640 So just a little bit of trivia if you're interested. 1641 01:23:39,640 --> 01:23:42,630 >> So that's just to demonstrate the differences between these two loops. 1642 01:23:42,630 --> 01:23:47,060 Don't mix them up when you're programming a question. 1643 01:23:47,060 --> 01:23:49,774 Any questions about that. 1644 01:23:49,774 --> 01:23:50,770 Cool. 1645 01:23:50,770 --> 01:23:51,310 All right. 1646 01:23:51,310 --> 01:23:52,250 JavaScript objects. 1647 01:23:52,250 --> 01:23:53,040 I talked about them. 1648 01:23:53,040 --> 01:23:54,650 They're like associative arrays. 1649 01:23:54,650 --> 01:23:57,730 The one thing I would like you to note here is that a value in an associative 1650 01:23:57,730 --> 01:23:59,540 array can be anything in JavaScript. 1651 01:23:59,540 --> 01:24:02,240 >> It can even be a function, like over there. 1652 01:24:02,240 --> 01:24:04,600 I have a function that is a value of a key. 1653 01:24:04,600 --> 01:24:07,030 And if I want to call that function, I just access bark. 1654 01:24:07,030 --> 01:24:08,490 And then I put the parentheses after that. 1655 01:24:08,490 --> 01:24:09,790 And that works. 1656 01:24:09,790 --> 01:24:15,570 So any questions? 1657 01:24:15,570 --> 01:24:16,370 No? 1658 01:24:16,370 --> 01:24:16,650 OK. 1659 01:24:16,650 --> 01:24:18,190 Good. 1660 01:24:18,190 --> 01:24:20,130 JavaScript, like PHP, is loosely typed. 1661 01:24:20,130 --> 01:24:20,970 What does that mean? 1662 01:24:20,970 --> 01:24:22,380 >> It does have types. 1663 01:24:22,380 --> 01:24:24,800 But when you declare a JavaScript variable, you say var i. 1664 01:24:24,800 --> 01:24:25,850 You don't say it. 1665 01:24:25,850 --> 01:24:26,460 That's not a thing. 1666 01:24:26,460 --> 01:24:28,000 You just say it's a variable. 1667 01:24:28,000 --> 01:24:31,590 And then JavaScript will handle the types under the hood for you. 1668 01:24:31,590 --> 01:24:34,390 We can freely convert between types because of this. 1669 01:24:34,390 --> 01:24:37,360 So i starts out as a number in this case. 1670 01:24:37,360 --> 01:24:38,790 And then I have a string. 1671 01:24:38,790 --> 01:24:39,710 And I add i to it. 1672 01:24:39,710 --> 01:24:41,810 And I reassign it back into i. 1673 01:24:41,810 --> 01:24:43,640 >> So on that first line, i is the number. 1674 01:24:43,640 --> 01:24:47,310 On the second line, i now becomes a string after I do the reassignment. 1675 01:24:47,310 --> 01:24:52,820 And here, I'm just concatenating that number onto the string. 1676 01:24:52,820 --> 01:24:56,210 So you see that even though i was an integer in the first part, it's sort 1677 01:24:56,210 --> 01:24:58,500 of like being converted to a string and then being added 1678 01:24:58,500 --> 01:25:00,510 onto that hello string. 1679 01:25:00,510 --> 01:25:02,340 And so that's what I mean by the loose typing. 1680 01:25:02,340 --> 01:25:04,760 That's that you convert between types very easily. 1681 01:25:04,760 --> 01:25:08,840 And it doesn't throw warnings at you like C does. 1682 01:25:08,840 --> 01:25:12,420 >> So i now contains hello 123 to the string. 1683 01:25:12,420 --> 01:25:13,270 Next. 1684 01:25:13,270 --> 01:25:15,610 We can also freely compare between types. 1685 01:25:15,610 --> 01:25:19,310 So if you just use ==, very much like in PHP, JavaScript 1686 01:25:19,310 --> 01:25:20,480 does a similar thing. 1687 01:25:20,480 --> 01:25:24,780 The string 123 is the same as the number 123 when you use double equals. 1688 01:25:24,780 --> 01:25:27,820 When used triple equals, it also wants to make sure that 1689 01:25:27,820 --> 01:25:29,240 the type is the same. 1690 01:25:29,240 --> 01:25:31,960 So because that is a string and that is a number, even though they're both 1691 01:25:31,960 --> 01:25:35,280 123, when you use triple equals, you get false. 1692 01:25:35,280 --> 01:25:39,330 In the double equals case, you get true, because double equals doesn't 1693 01:25:39,330 --> 01:25:40,020 care about type. 1694 01:25:40,020 --> 01:25:42,500 Triple equals does care about type. 1695 01:25:42,500 --> 01:25:43,750 Questions? 1696 01:25:43,750 --> 01:25:45,790 1697 01:25:45,790 --> 01:25:48,390 OK. 1698 01:25:48,390 --> 01:25:52,870 >> And another thing about JavaScript is scope is kind of global unless you're 1699 01:25:52,870 --> 01:25:53,460 in a function. 1700 01:25:53,460 --> 01:25:55,660 And it works the same way in PHP actually. 1701 01:25:55,660 --> 01:25:57,500 So let's go through this example. 1702 01:25:57,500 --> 01:25:59,110 I set i to 999. 1703 01:25:59,110 --> 01:26:00,900 And then I go into this for loop. 1704 01:26:00,900 --> 01:26:06,110 So if I'm printing i out in this for loop, I expect 0, 1, 2, 3, 4. 1705 01:26:06,110 --> 01:26:07,390 I get to i = 4. 1706 01:26:07,390 --> 01:26:10,770 It increments i now to 5 at the end of the for loop. 1707 01:26:10,770 --> 01:26:13,300 And then it breaks out of the loop, because it doesn't fulfill the 1708 01:26:13,300 --> 01:26:14,550 condition anymore. 1709 01:26:14,550 --> 01:26:16,650 >> What do you think that next console.log prints out? 1710 01:26:16,650 --> 01:26:20,570 1711 01:26:20,570 --> 01:26:24,790 So that's what it would do in C. In C, because if you have like var i outside 1712 01:26:24,790 --> 01:26:28,855 and you have var i inside a loop, like a for loop, then it makes it such that 1713 01:26:28,855 --> 01:26:30,690 it's scoped that the two i's are different. 1714 01:26:30,690 --> 01:26:34,630 In JavaScript, it will just treat it as the same i. 1715 01:26:34,630 --> 01:26:37,870 I get 5, because that was the value after it exited out of the loop. 1716 01:26:37,870 --> 01:26:39,120 So those i's are the same i. 1717 01:26:39,120 --> 01:26:42,620 1718 01:26:42,620 --> 01:26:44,130 Does that make sense? 1719 01:26:44,130 --> 01:26:45,720 >> Well, it makes sense from a JavaScript standpoint. 1720 01:26:45,720 --> 01:26:49,510 But the same paradigm doesn't carry over to C. They have 1721 01:26:49,510 --> 01:26:50,310 different scoping rules. 1722 01:26:50,310 --> 01:26:50,630 Yes. 1723 01:26:50,630 --> 01:26:52,566 >> AUDIENCE: [INAUDIBLE] 1724 01:26:52,566 --> 01:26:54,990 outside the function [INAUDIBLE]? 1725 01:26:54,990 --> 01:26:58,340 >> JOSEPH ONG: So outside which function? 1726 01:26:58,340 --> 01:27:00,810 So I'll get to that in just a second. 1727 01:27:00,810 --> 01:27:02,580 So we call foo(i). 1728 01:27:02,580 --> 01:27:06,920 This passes i into foo, increments it, and then logs it. 1729 01:27:06,920 --> 01:27:07,750 So it was 5. 1730 01:27:07,750 --> 01:27:09,210 So it becomes 6. 1731 01:27:09,210 --> 01:27:11,670 But what I'm talking about is that i in that function. 1732 01:27:11,670 --> 01:27:14,430 Because it's a parameter, it's scoped to that function. 1733 01:27:14,430 --> 01:27:17,800 So once I actually get out of that function, it's now going to go 1734 01:27:17,800 --> 01:27:19,860 back to the old i. 1735 01:27:19,860 --> 01:27:23,000 That i is only scoped because it's in a function. 1736 01:27:23,000 --> 01:27:24,200 And we have scope and functions. 1737 01:27:24,200 --> 01:27:29,080 But we don't have scope outside of functions in JavaScript. 1738 01:27:29,080 --> 01:27:31,170 Does that make sense? 1739 01:27:31,170 --> 01:27:31,320 Yes. 1740 01:27:31,320 --> 01:27:31,660 Question. 1741 01:27:31,660 --> 01:27:34,030 >> AUDIENCE: Same [INAUDIBLE]? 1742 01:27:34,030 --> 01:27:34,500 >> JOSEPH ONG: So yeah. 1743 01:27:34,500 --> 01:27:36,260 In PHP, it's the same type of thing. 1744 01:27:36,260 --> 01:27:37,680 There's a slight subtlety actually. 1745 01:27:37,680 --> 01:27:40,130 But you can ask me about that after the review. 1746 01:27:40,130 --> 01:27:43,930 You don't really need to know that subtlety for the quiz. 1747 01:27:43,930 --> 01:27:47,600 For all intents and purposes, like variables, global and PHP, unless 1748 01:27:47,600 --> 01:27:49,150 they're in a function, same thing in JavaScript. 1749 01:27:49,150 --> 01:27:49,480 Yes. 1750 01:27:49,480 --> 01:27:52,890 >> AUDIENCE: Why is this allowed in JavaScript and no where else? 1751 01:27:52,890 --> 01:27:55,010 >> JOSEPH ONG: So why is it allowed in JavaScript and not in C? 1752 01:27:55,010 --> 01:27:58,180 It's just whoever came up with JavaScript decided that this was OK in 1753 01:27:58,180 --> 01:27:59,510 JavaScript. 1754 01:27:59,510 --> 01:28:02,430 So it's just like a programming language convention as we would say. 1755 01:28:02,430 --> 01:28:02,580 Yes. 1756 01:28:02,580 --> 01:28:04,480 >> AUDIENCE: So why did it go from 6 to 5? 1757 01:28:04,480 --> 01:28:08,280 >> JOSEPH ONG: So it went from 6 to 5, because when I passed i into foo, that 1758 01:28:08,280 --> 01:28:13,420 i inside of foo is now scoped to foo, because scope exists in functions in 1759 01:28:13,420 --> 01:28:15,050 JavaScript. 1760 01:28:15,050 --> 01:28:18,720 But once I get out of here, because it was scoped to the function, I'm just 1761 01:28:18,720 --> 01:28:24,300 using the regular i that was inside the rest of the control flow. 1762 01:28:24,300 --> 01:28:25,210 Make sense? 1763 01:28:25,210 --> 01:28:26,910 Can I move on? 1764 01:28:26,910 --> 01:28:27,320 All right. 1765 01:28:27,320 --> 01:28:29,180 Cool. 1766 01:28:29,180 --> 01:28:31,890 >> The acceptance of this is objects are passed by reference. 1767 01:28:31,890 --> 01:28:33,990 You know how when you pass an array into C you could 1768 01:28:33,990 --> 01:28:35,810 actually modify the array? 1769 01:28:35,810 --> 01:28:37,350 It's the same thing in JavaScript. 1770 01:28:37,350 --> 01:28:42,960 If I pass an object, in this case, I passed Milo into this catify function. 1771 01:28:42,960 --> 01:28:44,490 Milo starts out. 1772 01:28:44,490 --> 01:28:46,310 His name is Milo Banana. 1773 01:28:46,310 --> 01:28:51,670 I pass that object into a function because it's an object, an associative 1774 01:28:51,670 --> 01:28:53,730 array in JavaScript. 1775 01:28:53,730 --> 01:28:57,110 >> When I perform an operation in that function, it will 1776 01:28:57,110 --> 01:28:58,560 actually change the object. 1777 01:28:58,560 --> 01:29:01,720 So this will only happen for objects in JavaScript, just like it happens 1778 01:29:01,720 --> 01:29:09,230 for arrays inside of C. So Milo's name will actually become cat now. 1779 01:29:09,230 --> 01:29:10,090 Does that make sense? 1780 01:29:10,090 --> 01:29:11,840 So this only works for objects. 1781 01:29:11,840 --> 01:29:13,090 Objects are passed by reference. 1782 01:29:13,090 --> 01:29:13,400 Yes. 1783 01:29:13,400 --> 01:29:16,850 >> AUDIENCE: So you're saying that in contrast to variable i. 1784 01:29:16,850 --> 01:29:17,240 >> JOSEPH ONG: Yeah. 1785 01:29:17,240 --> 01:29:20,330 Which variable i was just a number, right? 1786 01:29:20,330 --> 01:29:25,280 It's like in C when you pass an integer a, it makes a copy. 1787 01:29:25,280 --> 01:29:28,130 And when you pass an array, it actually changes the actual array in 1788 01:29:28,130 --> 01:29:32,870 C. The same thing happens with JavaScript in this case. 1789 01:29:32,870 --> 01:29:34,070 All right. 1790 01:29:34,070 --> 01:29:38,134 And next, Milo is sad because he's now a cat. 1791 01:29:38,134 --> 01:29:40,790 1792 01:29:40,790 --> 01:29:45,060 That was actually Milo after some trip to the vet. 1793 01:29:45,060 --> 01:29:46,860 >> So how do we use JavaScript in a web page? 1794 01:29:46,860 --> 01:29:47,790 We can include it. 1795 01:29:47,790 --> 01:29:50,090 This is HTML code with the strip tags. 1796 01:29:50,090 --> 01:29:51,300 So I have strip tags there. 1797 01:29:51,300 --> 01:29:54,820 And then I put some JavaScript code within the script tags. 1798 01:29:54,820 --> 01:29:56,390 And then it executes this. 1799 01:29:56,390 --> 01:29:58,830 When I just do it like this, it's called inline JavaScript. 1800 01:29:58,830 --> 01:30:02,400 It's kind of messy, because the JavaScript is actually in the HTML. 1801 01:30:02,400 --> 01:30:07,010 >> A better way to do this, much nicer, is to write your JavaScript in an 1802 01:30:07,010 --> 01:30:10,920 external file and then provide the script tag with a source. 1803 01:30:10,920 --> 01:30:14,920 And this will go to that JavaScript file and read the JavaScript code from 1804 01:30:14,920 --> 01:30:15,960 that file instead. 1805 01:30:15,960 --> 01:30:18,820 And this way, you don't have a lot of JavaScript at the beginning of your 1806 01:30:18,820 --> 01:30:20,760 HTML file, which makes it really messy. 1807 01:30:20,760 --> 01:30:21,860 You just put it somewhere else. 1808 01:30:21,860 --> 01:30:24,310 And then it will read it from there. 1809 01:30:24,310 --> 01:30:25,560 Did that makes sense? 1810 01:30:25,560 --> 01:30:27,990 1811 01:30:27,990 --> 01:30:29,640 >> Placement matters. 1812 01:30:29,640 --> 01:30:32,620 In this particular case, the script is before the body. 1813 01:30:32,620 --> 01:30:36,090 So when I execute that, there's nothing in the body yet. 1814 01:30:36,090 --> 01:30:39,920 Maybe this will make a little bit more sense when I show this next part. 1815 01:30:39,920 --> 01:30:43,210 In this case, the script comes after the div. 1816 01:30:43,210 --> 01:30:46,620 So the div actually appears on the page first. 1817 01:30:46,620 --> 01:30:49,470 Right here in this little red circle, you see the text appears. 1818 01:30:49,470 --> 01:30:51,810 And then the alert shows up. 1819 01:30:51,810 --> 01:30:54,890 >> In the first case, because the script was before the div, the 1820 01:30:54,890 --> 01:30:56,170 alert shows up first. 1821 01:30:56,170 --> 01:30:59,250 And then the div shows up after you dismiss the box. 1822 01:30:59,250 --> 01:31:01,330 So the execution matters. 1823 01:31:01,330 --> 01:31:02,290 So we'll keep this in mind. 1824 01:31:02,290 --> 01:31:03,640 This will be important in a little bit. 1825 01:31:03,640 --> 01:31:08,730 1826 01:31:08,730 --> 01:31:09,540 OK. 1827 01:31:09,540 --> 01:31:12,750 So well, how do you wait until the entire page is loaded then before you 1828 01:31:12,750 --> 01:31:13,580 execute some code? 1829 01:31:13,580 --> 01:31:15,540 We'll get into this a little bit later too. 1830 01:31:15,540 --> 01:31:19,016 But just keep this placement matters in mind for when we 1831 01:31:19,016 --> 01:31:20,570 come to another slide. 1832 01:31:20,570 --> 01:31:22,030 >> So we get to DOM now. 1833 01:31:22,030 --> 01:31:23,550 And what is DOM? 1834 01:31:23,550 --> 01:31:26,830 So if you look at HTML code, it's just a bunch of text on the screen. 1835 01:31:26,830 --> 01:31:30,560 So how does JavaScript know that this is an HTML element? 1836 01:31:30,560 --> 01:31:33,410 So we have to have some memory representation of this 1837 01:31:33,410 --> 01:31:34,930 structure that we have. 1838 01:31:34,930 --> 01:31:40,240 And whenever we have this in memory representation in JavaScript, we call 1839 01:31:40,240 --> 01:31:41,750 that the DOM. 1840 01:31:41,750 --> 01:31:45,130 And it's just a way that people decided that we should represent this 1841 01:31:45,130 --> 01:31:46,400 HTML structure as. 1842 01:31:46,400 --> 01:31:47,940 >> And what does this DOM look like? 1843 01:31:47,940 --> 01:31:50,460 Well, in memory representation, we take this text. 1844 01:31:50,460 --> 01:31:52,870 And we turn it into memory representation. 1845 01:31:52,870 --> 01:31:54,400 So this is the HTML. 1846 01:31:54,400 --> 01:31:58,090 So we first find out that every DOM tree has a document. 1847 01:31:58,090 --> 01:31:59,400 It looks like a tree. 1848 01:31:59,400 --> 01:32:03,550 And the document contains the HTML tag, actually everything 1849 01:32:03,550 --> 01:32:05,150 inside of this now. 1850 01:32:05,150 --> 01:32:06,970 The HTML tag has two children. 1851 01:32:06,970 --> 01:32:08,630 It has a head. 1852 01:32:08,630 --> 01:32:12,380 That head, if you look at indentation over there at how it's structured 1853 01:32:12,380 --> 01:32:14,960 between the close tags, head has a child. 1854 01:32:14,960 --> 01:32:17,130 The child is title. 1855 01:32:17,130 --> 01:32:18,370 Exactly. 1856 01:32:18,370 --> 01:32:21,000 >> Now, we have a body child. 1857 01:32:21,000 --> 01:32:24,870 And then that body has a child called family. 1858 01:32:24,870 --> 01:32:27,950 And that family has three children-- 1859 01:32:27,950 --> 01:32:29,550 oldest, middle, and youngest. 1860 01:32:29,550 --> 01:32:31,960 So you should know how to draw a diagram like this when we ask you how 1861 01:32:31,960 --> 01:32:34,270 to draw a diagram when we give you the HTML on the left. 1862 01:32:34,270 --> 01:32:36,350 Know how to produce the DOM tree. 1863 01:32:36,350 --> 01:32:38,930 And inside of these things, there's just some text, which I've represented 1864 01:32:38,930 --> 01:32:40,180 as little boxes. 1865 01:32:40,180 --> 01:32:42,750 1866 01:32:42,750 --> 01:32:47,980 >> Does this DOM tree structure make sense and what the DOM is? 1867 01:32:47,980 --> 01:32:49,300 So what does the p stand for? 1868 01:32:49,300 --> 01:32:51,850 Over here, the p over there in that tag represents a 1869 01:32:51,850 --> 01:32:54,510 paragraph tag in HTML. 1870 01:32:54,510 --> 01:32:57,080 So you can look it up. 1871 01:32:57,080 --> 01:32:59,290 But it just means it's some space for some text. 1872 01:32:59,290 --> 01:33:02,910 And it has some default CSS styling, because it's a paragraph tag. 1873 01:33:02,910 --> 01:33:05,130 But don't really worry about that part too much. 1874 01:33:05,130 --> 01:33:07,510 Just know it's a placeholder for some text. 1875 01:33:07,510 --> 01:33:08,480 Yes. 1876 01:33:08,480 --> 01:33:10,200 Question? 1877 01:33:10,200 --> 01:33:11,021 Yes. 1878 01:33:11,021 --> 01:33:12,374 >> AUDIENCE: You just mentioned CSS. 1879 01:33:12,374 --> 01:33:15,492 The hash family and the hash all that stuff is basically 1880 01:33:15,492 --> 01:33:17,400 representing IDs in CSS? 1881 01:33:17,400 --> 01:33:18,440 >> JOSEPH ONG: Yeah, exactly. 1882 01:33:18,440 --> 01:33:20,380 I'll get to what these hashes mean in a second. 1883 01:33:20,380 --> 01:33:23,480 When Angela went over CSS, she talked about CSS selectors. 1884 01:33:23,480 --> 01:33:26,770 These are the CSS selectors that she was talking about. 1885 01:33:26,770 --> 01:33:28,268 Yes, Rob? 1886 01:33:28,268 --> 01:33:32,060 >> ROB BOWDEN: I would also comment that DOM inside of title tag 1887 01:33:32,060 --> 01:33:35,385 is also a text node. 1888 01:33:35,385 --> 01:33:36,070 >> JOSEPH ONG: Right. 1889 01:33:36,070 --> 01:33:38,370 So inside the title tag, I have some text DOM. 1890 01:33:38,370 --> 01:33:42,730 So really, this title should have like a little box coming off of it as well. 1891 01:33:42,730 --> 01:33:45,740 But it doesn't really matter too much in this case. 1892 01:33:45,740 --> 01:33:49,620 We don't really care about text nodes, as we call them, too much. 1893 01:33:49,620 --> 01:33:50,270 OK, we do. 1894 01:33:50,270 --> 01:33:51,520 Apparently, we do. 1895 01:33:51,520 --> 01:33:54,260 1896 01:33:54,260 --> 01:33:57,100 And I will fix that when I upload it again. 1897 01:33:57,100 --> 01:33:59,830 Does that make sense? 1898 01:33:59,830 --> 01:34:01,160 >> So how do we work with the DOM? 1899 01:34:01,160 --> 01:34:03,790 Whenever you deal with the DOM in JavaScript, there are two steps. 1900 01:34:03,790 --> 01:34:05,030 You select a DOM element. 1901 01:34:05,030 --> 01:34:06,580 And then you do things to it. 1902 01:34:06,580 --> 01:34:11,480 So in this case, abstractly, I've selected the middle element. 1903 01:34:11,480 --> 01:34:14,530 And then an example of doing stuff to it would be changing the text. 1904 01:34:14,530 --> 01:34:16,020 That used to be Bob. 1905 01:34:16,020 --> 01:34:19,930 Now, what I did to it was I changed Bob to Milo in this case. 1906 01:34:19,930 --> 01:34:22,130 >> So how do we actually do this? 1907 01:34:22,130 --> 01:34:23,440 How do we do the selecting? 1908 01:34:23,440 --> 01:34:26,560 And how do we do the doing stuff to the thing once we've taken it? 1909 01:34:26,560 --> 01:34:30,470 Well, the way you guys have learned it in this class is by using something we 1910 01:34:30,470 --> 01:34:32,420 called jQuery. 1911 01:34:32,420 --> 01:34:33,910 So what is jQuery? 1912 01:34:33,910 --> 01:34:37,220 jQuery is a library that makes JavaScript easier to write. 1913 01:34:37,220 --> 01:34:39,500 So someone took the time and wrote jQuery. 1914 01:34:39,500 --> 01:34:41,530 >> jQuery is actually written in JavaScript. 1915 01:34:41,530 --> 01:34:44,550 And then because they did this, we now have a whole bunch of functions that 1916 01:34:44,550 --> 01:34:46,020 we can use that make our lives really easy. 1917 01:34:46,020 --> 01:34:48,580 1918 01:34:48,580 --> 01:34:50,030 So what are some of the things it does? 1919 01:34:50,030 --> 01:34:51,650 It makes selecting elements easier. 1920 01:34:51,650 --> 01:34:54,020 It makes changing HTML, adding classes easier. 1921 01:34:54,020 --> 01:34:55,360 It makes Ajax easier. 1922 01:34:55,360 --> 01:34:58,230 We'll get to that in a second. 1923 01:34:58,230 --> 01:35:00,630 >> And it's analogous to C libraries. 1924 01:35:00,630 --> 01:35:03,090 So you include string.h, you get strlen. 1925 01:35:03,090 --> 01:35:04,680 You get strcpy, all of these things. 1926 01:35:04,680 --> 01:35:09,650 When you include jQuery, you get nice ways to select elements to change 1927 01:35:09,650 --> 01:35:10,390 things, et cetera. 1928 01:35:10,390 --> 01:35:12,990 You get extra functionality that JavaScript doesn't give you. 1929 01:35:12,990 --> 01:35:15,310 So jQuery is not JavaScript. 1930 01:35:15,310 --> 01:35:18,660 jQuery is a library that's written in JavaScript that makes JavaScript 1931 01:35:18,660 --> 01:35:20,440 easier to write. 1932 01:35:20,440 --> 01:35:23,170 >> So jQuery is not a programming language. 1933 01:35:23,170 --> 01:35:24,540 But JavaScript is. 1934 01:35:24,540 --> 01:35:27,420 1935 01:35:27,420 --> 01:35:27,590 make. 1936 01:35:27,590 --> 01:35:30,420 Sure you get your terminology right. 1937 01:35:30,420 --> 01:35:32,490 Any questions? 1938 01:35:32,490 --> 01:35:33,882 Yes. 1939 01:35:33,882 --> 01:35:35,132 Is that a question? 1940 01:35:35,132 --> 01:35:37,910 1941 01:35:37,910 --> 01:35:38,350 All right. 1942 01:35:38,350 --> 01:35:40,080 So how do you use jQuery? 1943 01:35:40,080 --> 01:35:42,390 Well, when you're writing some JavaScript code and you include a 1944 01:35:42,390 --> 01:35:45,570 jQuery at the top of your file as a script file, you use the dollar sign 1945 01:35:45,570 --> 01:35:47,310 now to get access to jQuery. 1946 01:35:47,310 --> 01:35:49,860 And this is different from the dollar sign in PHP. 1947 01:35:49,860 --> 01:35:51,590 >> It's the same symbol you type on your keyboard. 1948 01:35:51,590 --> 01:35:52,780 But they mean very different things. 1949 01:35:52,780 --> 01:35:56,090 Dollar sign in PHP means this is how I declare a variable. 1950 01:35:56,090 --> 01:35:59,120 In JavaScript, when you've included jQuery, it stands for jQuery. 1951 01:35:59,120 --> 01:36:01,280 So keep that in mind. 1952 01:36:01,280 --> 01:36:03,420 So how might we select DOM elements? 1953 01:36:03,420 --> 01:36:06,500 Well, when you do it the ugly JavaScript way, you access the 1954 01:36:06,500 --> 01:36:08,240 document global variable. 1955 01:36:08,240 --> 01:36:11,170 And then you get element by ID family. 1956 01:36:11,170 --> 01:36:15,270 This is really long and wordy and not very nice. 1957 01:36:15,270 --> 01:36:18,190 >> Or you can get all elements that are a p tag. 1958 01:36:18,190 --> 01:36:20,080 That works too in JavaScript. 1959 01:36:20,080 --> 01:36:22,470 But we never really showed you the syntax too much. 1960 01:36:22,470 --> 01:36:24,620 What we showed you was jQuery. 1961 01:36:24,620 --> 01:36:28,720 So that entire selector up there that was expressed in JavaScript just gets 1962 01:36:28,720 --> 01:36:33,320 condensed to this very nice dollar sign hashtag family. 1963 01:36:33,320 --> 01:36:38,480 And $ p, just where it's like that. 1964 01:36:38,480 --> 01:36:41,690 If you want to select all p tags inside a family, we put a space 1965 01:36:41,690 --> 01:36:42,890 between the two. 1966 01:36:42,890 --> 01:36:44,815 And now, we get all the p tags inside a family. 1967 01:36:44,815 --> 01:36:48,740 1968 01:36:48,740 --> 01:36:50,380 >> And look familiar? 1969 01:36:50,380 --> 01:36:52,880 Well, Angela talked about CSS selectors. 1970 01:36:52,880 --> 01:36:54,200 Give me one second. 1971 01:36:54,200 --> 01:36:57,230 And so in order to select an element, you just use the same thing as you 1972 01:36:57,230 --> 01:36:58,530 would do with a CSS selector. 1973 01:36:58,530 --> 01:37:00,910 If you put a hash in front of it, it selects by ID. 1974 01:37:00,910 --> 01:37:02,220 A dot selects by classes. 1975 01:37:02,220 --> 01:37:06,230 If you just have the thing without hashes or dots, it selects those tags. 1976 01:37:06,230 --> 01:37:07,140 Questions. 1977 01:37:07,140 --> 01:37:07,470 Yes? 1978 01:37:07,470 --> 01:37:11,510 >> AUDIENCE: When we use dot in our HTML, is that not jQuery? 1979 01:37:11,510 --> 01:37:13,520 >> JOSEPH ONG: Dot in our HTML is a JavaScript thing. 1980 01:37:13,520 --> 01:37:14,750 It's not a jQuery thing. 1981 01:37:14,750 --> 01:37:17,620 The way you guys learned it with jQuery is to use .html. 1982 01:37:17,620 --> 01:37:21,510 And then you passed it whatever the HTML is going to be. 1983 01:37:21,510 --> 01:37:23,480 So I'll get to that in just a second actually. 1984 01:37:23,480 --> 01:37:27,800 So how do we do stuff to element once we've selected it? 1985 01:37:27,800 --> 01:37:30,130 So that's an example of selecting an element. 1986 01:37:30,130 --> 01:37:32,280 So now, we want to do stuff to it. 1987 01:37:32,280 --> 01:37:35,730 >> So in this case, let me go back to the previous slide. 1988 01:37:35,730 --> 01:37:37,360 It was Bob before. 1989 01:37:37,360 --> 01:37:40,660 And I want to change that inside HTML to Milo. 1990 01:37:40,660 --> 01:37:43,240 So I call the HTML function from the element. 1991 01:37:43,240 --> 01:37:45,580 That HTML function is a method of element. 1992 01:37:45,580 --> 01:37:47,430 And then I give it what I want the HTML to be. 1993 01:37:47,430 --> 01:37:50,560 And it just replaces what's inside of that tag with whatever I give it. 1994 01:37:50,560 --> 01:37:50,830 Yes. 1995 01:37:50,830 --> 01:37:51,170 Question? 1996 01:37:51,170 --> 01:37:57,397 >> AUDIENCE: The hashtag is used for only the jQuery. 1997 01:37:57,397 --> 01:37:59,313 [INAUDIBLE] 1998 01:37:59,313 --> 01:38:01,230 we wouldn't use that. 1999 01:38:01,230 --> 01:38:01,960 >> JOSEPH ONG: Yeah, exactly. 2000 01:38:01,960 --> 01:38:03,750 But don't worry too much about pure JavaScript. 2001 01:38:03,750 --> 01:38:06,670 I just want you guys to focus on how you would do it with jQuery, because 2002 01:38:06,670 --> 01:38:10,020 that's going to be the important part on the quiz. 2003 01:38:10,020 --> 01:38:10,400 Right. 2004 01:38:10,400 --> 01:38:10,880 Exactly. 2005 01:38:10,880 --> 01:38:16,025 So you see that hashtag, so that corresponds to select the element with 2006 01:38:16,025 --> 01:38:18,310 the ID middle because of that hashtag. 2007 01:38:18,310 --> 01:38:19,670 Hashtag means ID. 2008 01:38:19,670 --> 01:38:22,870 And this element has an ID of middle. 2009 01:38:22,870 --> 01:38:24,366 So that's the element we select. 2010 01:38:24,366 --> 01:38:27,160 >> AUDIENCE: [INAUDIBLE]. 2011 01:38:27,160 --> 01:38:31,090 dollar sign hashtag [INAUDIBLE]? 2012 01:38:31,090 --> 01:38:31,710 >> JOSEPH ONG: So no. 2013 01:38:31,710 --> 01:38:33,280 The question is can you use .value. 2014 01:38:33,280 --> 01:38:36,445 And .value only works on elements that are inputs. 2015 01:38:36,445 --> 01:38:40,950 2016 01:38:40,950 --> 01:38:45,495 In jQuery, it would be .val, not .value. 2017 01:38:45,495 --> 01:38:49,080 So I'll get to a small example that demonstrates all this in combination 2018 01:38:49,080 --> 01:38:49,850 in a second. 2019 01:38:49,850 --> 01:38:53,130 But I think this serves a little snippet makes sense to people so far. 2020 01:38:53,130 --> 01:38:55,450 Want to change the HTML, call the HTML method. 2021 01:38:55,450 --> 01:38:56,432 Yes. 2022 01:38:56,432 --> 01:38:58,200 >> AUDIENCE: Can you explain the method again? 2023 01:38:58,200 --> 01:39:01,900 >> JOSEPH ONG: So a method is just a function that belongs to one, in this 2024 01:39:01,900 --> 01:39:04,590 case, one of these DOM elements, because you see I 2025 01:39:04,590 --> 01:39:05,940 selected the element first. 2026 01:39:05,940 --> 01:39:07,320 Actually, let me use the mouse. 2027 01:39:07,320 --> 01:39:09,330 I selected the element first. 2028 01:39:09,330 --> 01:39:12,310 And then I called this HTML function that it had. 2029 01:39:12,310 --> 01:39:15,710 And because this function belongs to this thing, we call it a method. 2030 01:39:15,710 --> 01:39:19,480 That's just a fancy name for it. 2031 01:39:19,480 --> 01:39:20,730 Say that again. 2032 01:39:20,730 --> 01:39:22,880 2033 01:39:22,880 --> 01:39:25,170 So remember, we selected the element now. 2034 01:39:25,170 --> 01:39:27,810 And we've put it inside of the element variable. 2035 01:39:27,810 --> 01:39:28,600 Correct? 2036 01:39:28,600 --> 01:39:34,380 >> So when we want to change the HTML on inside, because it was Bob before, you 2037 01:39:34,380 --> 01:39:36,420 want to change that text to Milo. 2038 01:39:36,420 --> 01:39:37,920 So we call HTML. 2039 01:39:37,920 --> 01:39:41,610 And we tell it what the HTML inside that element should be now. 2040 01:39:41,610 --> 01:39:44,107 And so it changes it to Milo, because I gave it Milo. 2041 01:39:44,107 --> 01:39:46,542 >> AUDIENCE: So they're working together. 2042 01:39:46,542 --> 01:39:47,030 [INAUDIBLE] 2043 01:39:47,030 --> 01:39:47,390 >> JOSEPH ONG: Yeah, yeah. 2044 01:39:47,390 --> 01:39:48,180 They're working together. 2045 01:39:48,180 --> 01:39:50,210 So one of them selects the element first. 2046 01:39:50,210 --> 01:39:52,863 And the second one does something to it. 2047 01:39:52,863 --> 01:39:53,790 Yes. 2048 01:39:53,790 --> 01:39:56,168 >> AUDIENCE: [INAUDIBLE]. 2049 01:39:56,168 --> 01:40:01,280 If this method is different from in HTML you have the method equal actual. 2050 01:40:01,280 --> 01:40:01,560 >> JOSEPH ONG: Yeah. 2051 01:40:01,560 --> 01:40:03,370 That is a different method. 2052 01:40:03,370 --> 01:40:04,670 That is a different method. 2053 01:40:04,670 --> 01:40:07,860 And we can cover that in just a second when we get to an example. 2054 01:40:07,860 --> 01:40:12,000 I want to make sure that we speed up because we're running out of time. 2055 01:40:12,000 --> 01:40:15,360 But we've run way over time now. 2056 01:40:15,360 --> 01:40:15,490 OK. 2057 01:40:15,490 --> 01:40:16,430 Cool. 2058 01:40:16,430 --> 01:40:20,130 So if you want to add a class, there's also an add class method. 2059 01:40:20,130 --> 01:40:24,300 This is just an example of what you can do with jQuery. 2060 01:40:24,300 --> 01:40:25,950 That just adds a class. 2061 01:40:25,950 --> 01:40:28,660 >> If you want to remove it, you can call remove. 2062 01:40:28,660 --> 01:40:32,280 That's just another thing you can do. 2063 01:40:32,280 --> 01:40:35,680 So more examples of things you can do. 2064 01:40:35,680 --> 01:40:37,510 So can I just put it at the top like this? 2065 01:40:37,510 --> 01:40:38,760 Youngest remove. 2066 01:40:38,760 --> 01:40:42,470 2067 01:40:42,470 --> 01:40:45,930 If I just execute that JavaScript at the top of my file, will that work? 2068 01:40:45,930 --> 01:40:48,540 2069 01:40:48,540 --> 01:40:48,920 Right. 2070 01:40:48,920 --> 01:40:50,530 Because middle doesn't exist yet. 2071 01:40:50,530 --> 01:40:51,840 So this is not going to work. 2072 01:40:51,840 --> 01:40:52,800 Execution order. 2073 01:40:52,800 --> 01:40:55,040 It goes to the top first. 2074 01:40:55,040 --> 01:40:55,540 What? 2075 01:40:55,540 --> 01:40:57,450 >> AUDIENCE: Youngest doesn't exist yet? 2076 01:40:57,450 --> 01:40:57,810 >> JOSEPH ONG: Yeah. 2077 01:40:57,810 --> 01:40:58,710 Youngest doesn't exist yet. 2078 01:40:58,710 --> 01:40:59,600 Exactly. 2079 01:40:59,600 --> 01:41:01,320 >> AUDIENCE: You said middle. 2080 01:41:01,320 --> 01:41:01,510 >> JOSEPH ONG: Sorry. 2081 01:41:01,510 --> 01:41:02,720 Youngest doesn't exist yet. 2082 01:41:02,720 --> 01:41:04,510 And the other thing is I haven't included the jQuery 2083 01:41:04,510 --> 01:41:06,580 file ask script src. 2084 01:41:06,580 --> 01:41:07,960 So that's not going to work. 2085 01:41:07,960 --> 01:41:09,580 Actually, I didn't do that in the next slide, which is 2086 01:41:09,580 --> 01:41:10,700 supposed to fix that either. 2087 01:41:10,700 --> 01:41:14,120 But the way we do this is JavaScript is event driven. 2088 01:41:14,120 --> 01:41:17,410 So what we do is we use an event handler to make this happen. 2089 01:41:17,410 --> 01:41:19,510 And so I select the document set first. 2090 01:41:19,510 --> 01:41:23,810 I say, OK, when the document is ready, let me run a function. 2091 01:41:23,810 --> 01:41:25,470 So that's all that syntax means. 2092 01:41:25,470 --> 01:41:27,100 >> I selected the document. 2093 01:41:27,100 --> 01:41:29,530 Now, when the document is ready, run the function. 2094 01:41:29,530 --> 01:41:32,970 And so over here when the document is ready, which means all the HTML has 2095 01:41:32,970 --> 01:41:36,140 loaded, then I run the function that removes that element. 2096 01:41:36,140 --> 01:41:40,270 And so now, when I run this function that I passed into ready, I'm 2097 01:41:40,270 --> 01:41:43,780 guaranteed that all the HTML on the page is going to exist first. 2098 01:41:43,780 --> 01:41:44,100 Yes. 2099 01:41:44,100 --> 01:41:44,425 Question? 2100 01:41:44,425 --> 01:41:48,200 >> AUDIENCE: What is the event keyword within the function? 2101 01:41:48,200 --> 01:41:51,750 >> JOSEPH ONG: So that event keyword in the function is just a parameter that 2102 01:41:51,750 --> 01:41:53,490 gets passed to the function for any event. 2103 01:41:53,490 --> 01:41:55,470 It's just something that you get for free. 2104 01:41:55,470 --> 01:41:59,320 When you are using key handlers in pset8, that event could tell you, for 2105 01:41:59,320 --> 01:42:01,350 example, which key you pressed on. 2106 01:42:01,350 --> 01:42:05,540 In this case, for a ready event, it's actually not super useful. 2107 01:42:05,540 --> 01:42:08,640 But for a key down event, it's more useful, because you get to know which 2108 01:42:08,640 --> 01:42:12,330 key you pressed by accessing key code off that event object. 2109 01:42:12,330 --> 01:42:13,530 Correct? 2110 01:42:13,530 --> 01:42:15,160 Does that make sense? 2111 01:42:15,160 --> 01:42:16,280 OK. 2112 01:42:16,280 --> 01:42:16,580 Yes. 2113 01:42:16,580 --> 01:42:17,150 Question? 2114 01:42:17,150 --> 01:42:19,290 >> AUDIENCE: So can you put the script tag lower down? 2115 01:42:19,290 --> 01:42:19,940 >> JOSEPH ONG: So yeah. 2116 01:42:19,940 --> 01:42:21,500 You could put the script tag lower down. 2117 01:42:21,500 --> 01:42:23,090 But then it just becomes really messy. 2118 01:42:23,090 --> 01:42:26,590 And we like to centralize all of our code in one place. 2119 01:42:26,590 --> 01:42:28,290 And this will allow us to do it. 2120 01:42:28,290 --> 01:42:32,010 Remember earlier I said there's a nicer way to ensure that elements are 2121 01:42:32,010 --> 01:42:33,880 on the page before you execute code? 2122 01:42:33,880 --> 01:42:36,079 And this is just a nice way you would accomplish that. 2123 01:42:36,079 --> 01:42:37,329 >> AUDIENCE: [INAUDIBLE]. 2124 01:42:37,329 --> 01:42:41,710 2125 01:42:41,710 --> 01:42:42,230 >> JOSEPH ONG: Yeah. 2126 01:42:42,230 --> 01:42:43,460 You would still have to, right? 2127 01:42:43,460 --> 01:42:46,930 Because remember, you included the file at the top of the page. 2128 01:42:46,930 --> 01:42:49,890 So it's going to execute first before you get to the bottom of the page. 2129 01:42:49,890 --> 01:42:54,600 2130 01:42:54,600 --> 01:42:55,180 OK. 2131 01:42:55,180 --> 01:42:59,210 So you can also add a different type of event handler. 2132 01:42:59,210 --> 01:43:00,640 This one just processes clicks. 2133 01:43:00,640 --> 01:43:03,910 When I click on youngest, then it will pop up with an alert. 2134 01:43:03,910 --> 01:43:05,440 This is just a different type of event. 2135 01:43:05,440 --> 01:43:08,840 As opposed to the ready event, you now use the click event when you receive 2136 01:43:08,840 --> 01:43:10,190 clicks on an element. 2137 01:43:10,190 --> 01:43:13,860 >> And so in this case, remember, the click handler is attached to youngest. 2138 01:43:13,860 --> 01:43:16,080 So it only happens when I click on youngest. 2139 01:43:16,080 --> 01:43:19,510 And in the other one, the ready event was attached to the document. 2140 01:43:19,510 --> 01:43:23,750 So it waits for the document to be ready. 2141 01:43:23,750 --> 01:43:25,120 Make sense? 2142 01:43:25,120 --> 01:43:26,190 I think I can move on. 2143 01:43:26,190 --> 01:43:26,610 Yes. 2144 01:43:26,610 --> 01:43:26,980 Question? 2145 01:43:26,980 --> 01:43:28,230 >> AUDIENCE: [INAUDIBLE]. 2146 01:43:28,230 --> 01:43:31,676 2147 01:43:31,676 --> 01:43:33,620 in this case you use [INAUDIBLE]. 2148 01:43:33,620 --> 01:43:36,650 >> JOSEPH ONG: Oh, yeah, because in this case, I have to wait for the youngest 2149 01:43:36,650 --> 01:43:40,740 element to appear on the screen first before I can attach a click handler to 2150 01:43:40,740 --> 01:43:43,062 it, which is why I put it inside of a document ready. 2151 01:43:43,062 --> 01:43:45,780 2152 01:43:45,780 --> 01:43:46,840 OK. 2153 01:43:46,840 --> 01:43:52,390 And next, so this is a big example of how you would combine everything. 2154 01:43:52,390 --> 01:43:55,930 This is just a form validation example you've seen in lecture. 2155 01:43:55,930 --> 01:43:58,410 So take it step by step as you go through this. 2156 01:43:58,410 --> 01:43:59,590 And it will be totally OK. 2157 01:43:59,590 --> 01:44:01,400 Just read it from top to bottom. 2158 01:44:01,400 --> 01:44:03,030 >> I have a form at the bottom. 2159 01:44:03,030 --> 01:44:07,590 When the document is ready, I attach a submit handler to the form, such that 2160 01:44:07,590 --> 01:44:12,910 when I submit the form, I get the values inside each of those inputs. 2161 01:44:12,910 --> 01:44:14,560 And I check if it's blank. 2162 01:44:14,560 --> 01:44:17,090 If it's blank, I return false, because I don't want to submit the form, 2163 01:44:17,090 --> 01:44:18,950 because the form is wrong. 2164 01:44:18,950 --> 01:44:22,040 If the password is blank or it's less than eight characters, I don't submit 2165 01:44:22,040 --> 01:44:24,470 the form, because that's also wrong. 2166 01:44:24,470 --> 01:44:28,150 And the return false just prevents the form from submitting and 2167 01:44:28,150 --> 01:44:30,150 going to a new page. 2168 01:44:30,150 --> 01:44:31,310 >> And hopefully, this makes sense. 2169 01:44:31,310 --> 01:44:34,650 I think you guys should walk through this code step by step on your own. 2170 01:44:34,650 --> 01:44:38,350 And once you understand what the select elements and do stuff to it 2171 01:44:38,350 --> 01:44:40,520 actually entails, this will make a lot of sense to you. 2172 01:44:40,520 --> 01:44:41,295 Yes? 2173 01:44:41,295 --> 01:44:44,150 >> AUDIENCE: What does the name = username mean? 2174 01:44:44,150 --> 01:44:48,530 >> JOSEPH ONG: So the name = username and name = password just means look at the 2175 01:44:48,530 --> 01:44:50,730 attribute of whatever you're selecting. 2176 01:44:50,730 --> 01:44:51,790 And then that has to match. 2177 01:44:51,790 --> 01:44:53,870 So we go into registration. 2178 01:44:53,870 --> 01:44:56,240 And then we look at all inputs and registration. 2179 01:44:56,240 --> 01:44:59,990 And then we pick the one where the name attribute is equal to username. 2180 01:44:59,990 --> 01:45:04,040 So that first selector only selects the username input. 2181 01:45:04,040 --> 01:45:08,220 And that second selector only selects the password one, because those have 2182 01:45:08,220 --> 01:45:12,240 their name attributes set as what they're supposed to be. 2183 01:45:12,240 --> 01:45:12,575 Question? 2184 01:45:12,575 --> 01:45:17,030 >> AUDIENCE: On submission, how does the bottom part resolve the top part? 2185 01:45:17,030 --> 01:45:19,350 >> JOSEPH ONG: So that's because of the event handler. 2186 01:45:19,350 --> 01:45:23,000 So we're waiting for a submit event that gets fired from the form. 2187 01:45:23,000 --> 01:45:24,730 And that's all that is submit. 2188 01:45:24,730 --> 01:45:26,080 Why do I call submit up there? 2189 01:45:26,080 --> 01:45:28,870 It says, when the form is submitted, I get a submit event. 2190 01:45:28,870 --> 01:45:33,480 So let me just intercept that and then run this code instead. 2191 01:45:33,480 --> 01:45:33,823 Yes? 2192 01:45:33,823 --> 01:45:35,866 >> AUDIENCE: Why do you have to have function event? 2193 01:45:35,866 --> 01:45:38,580 Why can't you just [INAUDIBLE]? 2194 01:45:38,580 --> 01:45:41,140 >> JOSEPH ONG: Because in JavaScript, you have to declare the functions. 2195 01:45:41,140 --> 01:45:42,910 That's just how it works in JavaScript. 2196 01:45:42,910 --> 01:45:44,800 You have to say it's going to run a function. 2197 01:45:44,800 --> 01:45:47,290 So you're telling it that you're expecting a function here instead of 2198 01:45:47,290 --> 01:45:48,260 just curly braces. 2199 01:45:48,260 --> 01:45:50,460 >> AUDIENCE: And the function is whatever follows? 2200 01:45:50,460 --> 01:45:50,650 >> JOSEPH ONG: Yeah. 2201 01:45:50,650 --> 01:45:52,790 The function is whatever is inside the curly braces after 2202 01:45:52,790 --> 01:45:53,630 that function keyword. 2203 01:45:53,630 --> 01:45:54,045 Yes? 2204 01:45:54,045 --> 01:45:55,295 >> AUDIENCE: [INAUDIBLE]. 2205 01:45:55,295 --> 01:46:00,180 2206 01:46:00,180 --> 01:46:00,660 >> JOSEPH ONG: For submit? 2207 01:46:00,660 --> 01:46:03,520 >> AUDIENCE: No, for function without the event. 2208 01:46:03,520 --> 01:46:03,770 >> JOSEPH ONG: Yeah. 2209 01:46:03,770 --> 01:46:05,610 So without the event, you can have that. 2210 01:46:05,610 --> 01:46:08,480 If you don't need the event, then you can just omit it. 2211 01:46:08,480 --> 01:46:12,220 But if you do, then you just put it there. 2212 01:46:12,220 --> 01:46:12,520 Yes. 2213 01:46:12,520 --> 01:46:13,190 Quick question? 2214 01:46:13,190 --> 01:46:14,440 >> AUDIENCE: [INAUDIBLE]. 2215 01:46:14,440 --> 01:46:21,170 2216 01:46:21,170 --> 01:46:21,440 >> JOSEPH ONG: Yeah. 2217 01:46:21,440 --> 01:46:24,550 Because what you need to do, the document.ready just says wait for all 2218 01:46:24,550 --> 01:46:26,540 the HTML on the page to load first. 2219 01:46:26,540 --> 01:46:30,510 And usually, you want your elements in place before you run any code. 2220 01:46:30,510 --> 01:46:31,030 All right. 2221 01:46:31,030 --> 01:46:32,180 We have to get to Ajax. 2222 01:46:32,180 --> 01:46:33,110 We don't have much time. 2223 01:46:33,110 --> 01:46:35,170 So pros and cons. 2224 01:46:35,170 --> 01:46:37,450 JavaScript is easier try write with jQuery. 2225 01:46:37,450 --> 01:46:38,930 But jQuery is kind of slow. 2226 01:46:38,930 --> 01:46:42,290 >> It's like PHP is slower than C, because it's interpreted. 2227 01:46:42,290 --> 01:46:45,690 And jQuery is a bit slower than JavaScript, because it does a lot of 2228 01:46:45,690 --> 01:46:46,630 things under the hood. 2229 01:46:46,630 --> 01:46:48,660 And so if you're using jQuery, it's just a little bit slower than 2230 01:46:48,660 --> 01:46:51,630 JavaScript, even though it gives you nice elegance. 2231 01:46:51,630 --> 01:46:53,970 And finally, Ajax. 2232 01:46:53,970 --> 01:46:59,170 So far with Ajax, you haven't seen Ajax in terms of pset7 yet, because 2233 01:46:59,170 --> 01:47:01,150 when you do, you submit a form to quote. 2234 01:47:01,150 --> 01:47:02,350 It loads a new page. 2235 01:47:02,350 --> 01:47:04,440 So you get this big white flash on the page while that 2236 01:47:04,440 --> 01:47:06,820 second page loads, correct? 2237 01:47:06,820 --> 01:47:08,780 >> It would be really nice if you didn't have this flash. 2238 01:47:08,780 --> 01:47:11,600 Like Facebook, if you just scroll to the bottom, it adds new content 2239 01:47:11,600 --> 01:47:13,490 without refreshing the entire page. 2240 01:47:13,490 --> 01:47:15,420 So something like this would be nice. 2241 01:47:15,420 --> 01:47:17,370 This is JavaScript code on the left side. 2242 01:47:17,370 --> 01:47:19,390 You get what is inside of that input. 2243 01:47:19,390 --> 01:47:21,340 You get the stock info from Yahoo! 2244 01:47:21,340 --> 01:47:27,440 And then you make a big string that says, OK, this is the message I want 2245 01:47:27,440 --> 01:47:28,400 to show on the screen. 2246 01:47:28,400 --> 01:47:32,280 And then you put that message inside of some HTML element that gets 2247 01:47:32,280 --> 01:47:33,970 displayed on the screen. 2248 01:47:33,970 --> 01:47:35,540 >> So that's all that's happening here. 2249 01:47:35,540 --> 01:47:39,410 So basically, because this is all JavaScript and you don't need to run 2250 01:47:39,410 --> 01:47:42,980 anymore PHP, this will make sure that the page doesn't refresh. 2251 01:47:42,980 --> 01:47:47,470 So this is just an abstract idea that I'm saying here for now. 2252 01:47:47,470 --> 01:47:50,630 The abstract idea is that if you do it all in JavaScript, you don't have a 2253 01:47:50,630 --> 01:47:52,330 page refresh. 2254 01:47:52,330 --> 01:47:53,800 But how do you actually do this? 2255 01:47:53,800 --> 01:47:56,230 >> Well, actually, let's talk about a problem with this first. 2256 01:47:56,230 --> 01:47:59,340 A problem is in JavaScript, execution is synchronous. 2257 01:47:59,340 --> 01:48:02,000 So you have to wait for one line to finish before you 2258 01:48:02,000 --> 01:48:03,370 execute the next line. 2259 01:48:03,370 --> 01:48:06,130 And what if I'm going over to Yahoo!, and their servers are really slow, and 2260 01:48:06,130 --> 01:48:08,790 it takes them three seconds to give me back that stock info? 2261 01:48:08,790 --> 01:48:12,620 When I hit that price line, if the execution is synchronous, as it is by 2262 01:48:12,620 --> 01:48:15,390 default, what it's just going to do is your browser is going to stall for 2263 01:48:15,390 --> 01:48:15,930 three seconds. 2264 01:48:15,930 --> 01:48:18,900 And you're not going to be able to do anything while it gets that data. 2265 01:48:18,900 --> 01:48:20,010 It's going to be frozen. 2266 01:48:20,010 --> 01:48:20,800 >> And that's bad. 2267 01:48:20,800 --> 01:48:23,390 You don't want a user to have a frozen web page. 2268 01:48:23,390 --> 01:48:24,170 Correct? 2269 01:48:24,170 --> 01:48:25,480 That's just bad. 2270 01:48:25,480 --> 01:48:26,770 Everyone agrees? 2271 01:48:26,770 --> 01:48:29,270 If you're browsing Facebook and it freezes and you can't do anything, you 2272 01:48:29,270 --> 01:48:31,920 get really frustrated. 2273 01:48:31,920 --> 01:48:34,960 So the solution is we make something asynchronous instead. 2274 01:48:34,960 --> 01:48:38,910 So all this asynchronous thing says is, I'm going to ask this 2275 01:48:38,910 --> 01:48:40,280 URL for some data. 2276 01:48:40,280 --> 01:48:41,610 And then I'm going to keep going. 2277 01:48:41,610 --> 01:48:45,330 I'm just going to keep executing whatever code that was after that. 2278 01:48:45,330 --> 01:48:49,290 >> And then whenever that data is ready, then I will process it. 2279 01:48:49,290 --> 01:48:50,540 That's all it is saying. 2280 01:48:50,540 --> 01:48:52,795 2281 01:48:52,795 --> 01:48:56,710 >> AUDIENCE: Ajax just makes code asynchronous? 2282 01:48:56,710 --> 01:48:58,560 >> JOSEPH ONG: It's an asynchronous way of fetching data. 2283 01:48:58,560 --> 01:49:01,230 So the first thing about Ajax is it lets me get data 2284 01:49:01,230 --> 01:49:03,170 from an external website. 2285 01:49:03,170 --> 01:49:07,045 And the second thing is it makes sure that my page doesn't stall while I'm 2286 01:49:07,045 --> 01:49:07,970 fetching that data. 2287 01:49:07,970 --> 01:49:09,600 That's the asynchronous part of it. 2288 01:49:09,600 --> 01:49:13,040 Because it goes off somewhere else, because I say I keep going on while 2289 01:49:13,040 --> 01:49:14,900 it's fetching that data, that makes it asynchronous. 2290 01:49:14,900 --> 01:49:17,170 I keep executing. 2291 01:49:17,170 --> 01:49:18,960 >> So keep that asynchronous idea in mind. 2292 01:49:18,960 --> 01:49:22,010 And I'll show you what the difference is. 2293 01:49:22,010 --> 01:49:23,920 The synchronous version is on the left side. 2294 01:49:23,920 --> 01:49:26,240 The asynchronous version is on the right side. 2295 01:49:26,240 --> 01:49:29,170 Look at the numbers to see which steps correspond to what 2296 01:49:29,170 --> 01:49:30,610 executes at each line. 2297 01:49:30,610 --> 01:49:32,730 Over there, the alert shows up first. 2298 01:49:32,730 --> 01:49:34,590 Because getting stock info from Yahoo! 2299 01:49:34,590 --> 01:49:37,250 takes three seconds, it stalls for three seconds. 2300 01:49:37,250 --> 01:49:39,880 And then it alerts the price after those three seconds. 2301 01:49:39,880 --> 01:49:43,690 >> So now, that alert shows up at that time-- 2302 01:49:43,690 --> 01:49:44,610 three seconds in. 2303 01:49:44,610 --> 01:49:47,670 And then it alerts by after that. 2304 01:49:47,670 --> 01:49:48,930 So it just goes step by step. 2305 01:49:48,930 --> 01:49:51,200 It's like what you guys would accept, correct? 2306 01:49:51,200 --> 01:49:54,170 With asynchronous execution, you alert first. 2307 01:49:54,170 --> 01:49:57,410 Then you go off to this URL. 2308 01:49:57,410 --> 01:49:59,530 And you say, I'm going to just ask for the data. 2309 01:49:59,530 --> 01:50:01,170 And then I'm going to process it later. 2310 01:50:01,170 --> 01:50:04,230 So it immediately executes the next line after I make that 2311 01:50:04,230 --> 01:50:05,710 asynchronous request. 2312 01:50:05,710 --> 01:50:08,920 >> So a 0.001 seconds, you see alert hi. 2313 01:50:08,920 --> 01:50:10,960 Execute that function, alert bye. 2314 01:50:10,960 --> 01:50:14,240 And because I made a promise that I would process the data later, what 2315 01:50:14,240 --> 01:50:17,920 happens is when that data comes back three seconds later, then I run that 2316 01:50:17,920 --> 01:50:21,380 function that I have over there. 2317 01:50:21,380 --> 01:50:21,870 Yes? 2318 01:50:21,870 --> 01:50:25,750 >> AUDIENCE: Could you specify or clarify what Ajax means? 2319 01:50:25,750 --> 01:50:30,460 >> JOSEPH ONG: So Ajax is a way that if I need data when I'm on a website and I 2320 01:50:30,460 --> 01:50:34,690 don't want to refresh the page, then I use this technology called Ajax. 2321 01:50:34,690 --> 01:50:40,630 That essentially just means, go fetch data from another website. 2322 01:50:40,630 --> 01:50:43,724 And do it in a way that just doesn't stall my web page. 2323 01:50:43,724 --> 01:50:46,650 >> AUDIENCE: So is that an inherent part of JavaScript or jQuery? 2324 01:50:46,650 --> 01:50:50,590 >> JOSEPH ONG: So someone wrote a way to do this in JavaScript a long time ago. 2325 01:50:50,590 --> 01:50:52,050 At one point, it didn't exist. 2326 01:50:52,050 --> 01:50:56,630 And so someone invented this technique to allow people to request this data 2327 01:50:56,630 --> 01:50:57,680 in this fashion. 2328 01:50:57,680 --> 01:50:59,550 And they wrote some stuff to do it for you. 2329 01:50:59,550 --> 01:51:01,605 And jQuery just gives you this very nice way to do it 2330 01:51:01,605 --> 01:51:03,112 with this $.get function. 2331 01:51:03,112 --> 01:51:07,200 2332 01:51:07,200 --> 01:51:09,480 questions? 2333 01:51:09,480 --> 01:51:11,560 I can answer questions about Ajax afterward too. 2334 01:51:11,560 --> 01:51:13,870 I'll be here. 2335 01:51:13,870 --> 01:51:16,390 >> So it let's us fetch data without refreshing the page. 2336 01:51:16,390 --> 01:51:18,200 And it let's us do this in an asynchronous way that 2337 01:51:18,200 --> 01:51:19,450 doesn't freeze the page. 2338 01:51:19,450 --> 01:51:22,680 2339 01:51:22,680 --> 01:51:27,310 Too long, didn't read if that explanation was too long for you. 2340 01:51:27,310 --> 01:51:29,430 So finally, cross-site scripting attacks. 2341 01:51:29,430 --> 01:51:30,910 We saw this with Zamyla. 2342 01:51:30,910 --> 01:51:34,285 If in my database someone has this name, which is this script tag, and I 2343 01:51:34,285 --> 01:51:38,280 have some code on my page that prints out people's names in a row, or I have 2344 01:51:38,280 --> 01:51:43,310 some JavaScript code that inserts this name into the page, 2345 01:51:43,310 --> 01:51:45,680 what HTML gets produced? 2346 01:51:45,680 --> 01:51:47,290 Well, I print out the HTML tag. 2347 01:51:47,290 --> 01:51:48,390 I print out all these tags. 2348 01:51:48,390 --> 01:51:50,740 >> I get to the part where I'm printing out with my friends. 2349 01:51:50,740 --> 01:51:52,980 I print Lauren out. 2350 01:51:52,980 --> 01:51:54,200 It print Milo out. 2351 01:51:54,200 --> 01:51:56,810 And then my name in the database is script post 2352 01:51:56,810 --> 01:51:58,060 unflattering Facebook status. 2353 01:51:58,060 --> 01:52:00,740 2354 01:52:00,740 --> 01:52:04,330 Because I inserted this into the page because it looks like JavaScript, when 2355 01:52:04,330 --> 01:52:07,930 this page gets sent to the user, it gets executed as JavaScript. 2356 01:52:07,930 --> 01:52:10,800 And so this is what we call a cross-site scripting attack. 2357 01:52:10,800 --> 01:52:14,570 Someone puts malicious information in your database that could correspond to 2358 01:52:14,570 --> 01:52:17,080 some additional string or some JavaScript string. 2359 01:52:17,080 --> 01:52:20,130 >> And when it gets printed out to the page in this fashion, then what 2360 01:52:20,130 --> 01:52:22,580 happens is that bad code gets executed that I didn't intend 2361 01:52:22,580 --> 01:52:25,110 for it to get executed. 2362 01:52:25,110 --> 01:52:28,230 And that's all a cross-site scripting attack is. 2363 01:52:28,230 --> 01:52:31,790 And the way you get around this is like Zamyla said. 2364 01:52:31,790 --> 01:52:34,340 You just wrap things in HTML special chars. 2365 01:52:34,340 --> 01:52:39,460 And this HTML special chars is a PHP function that will prevent this sort 2366 01:52:39,460 --> 01:52:42,000 of thing from happening to you if you have a malicious 2367 01:52:42,000 --> 01:52:43,830 string in your database. 2368 01:52:43,830 --> 01:52:47,650 It just escapes it, so that it doesn't get interpreted as HTML. 2369 01:52:47,650 --> 01:52:50,960 It replaces the little brackets with what we call entities. 2370 01:52:50,960 --> 01:52:52,250 And we went over this in lecture too. 2371 01:52:52,250 --> 01:52:55,800 So I think you guys should have a good grasp on that. 2372 01:52:55,800 --> 01:52:57,420 Questions? 2373 01:52:57,420 --> 01:52:57,820 Yes. 2374 01:52:57,820 --> 01:53:00,860 >> AUDIENCE: So how would the [INAUDIBLE]? 2375 01:53:00,860 --> 01:53:01,555 >> JOSEPH ONG: Say that again. 2376 01:53:01,555 --> 01:53:02,500 >> AUDIENCE: How would the monitor-- 2377 01:53:02,500 --> 01:53:02,860 >> JOSEPH ONG: Right. 2378 01:53:02,860 --> 01:53:06,080 So you have something that says, when I register, type in my name. 2379 01:53:06,080 --> 01:53:09,390 I just type in that field, my name is stript post unflattering Facebook 2380 01:53:09,390 --> 01:53:11,570 status close script tag. 2381 01:53:11,570 --> 01:53:15,690 And that just gets put into the database, because I can't say someone 2382 01:53:15,690 --> 01:53:18,260 in the world doesn't have a name with a left arrow in it or the 2383 01:53:18,260 --> 01:53:19,036 word script in it. 2384 01:53:19,036 --> 01:53:21,330 That doesn't really make sense. 2385 01:53:21,330 --> 01:53:24,560 So I just have to make sure that I sanitize the stuff before I print it 2386 01:53:24,560 --> 01:53:25,420 out to the page. 2387 01:53:25,420 --> 01:53:27,140 >> AUDIENCE: So the HTML special cards prevents the script tags? 2388 01:53:27,140 --> 01:53:28,710 >> JOSEPH ONG: Yeah. 2389 01:53:28,710 --> 01:53:29,960 So it doesn't prevent the script tags. 2390 01:53:29,960 --> 01:53:32,320 It just makes sure that the script tags don't get 2391 01:53:32,320 --> 01:53:35,120 interpreted as HTML or-- 2392 01:53:35,120 --> 01:53:35,400 yeah. 2393 01:53:35,400 --> 01:53:38,470 It just comes up as what it actually is. 2394 01:53:38,470 --> 01:53:39,220 All right. 2395 01:53:39,220 --> 01:53:40,930 So that was the quiz review. 2396 01:53:40,930 --> 01:53:41,830 Cool. 2397 01:53:41,830 --> 01:53:45,088 >> [APPLAUSE]