This weekend I gave a talk at Interesting New York titled, “How I Learned to Make Stuff on the Internet (and You Can Too).” Even though I was a bit rushed (it was supposed to end at 6pm and I went on at 6:10 with two more to go after me), it was super fun. Everyone was really great and it totally lived up to billing. Anyway, I figured I’d put my presentation and a rough version of my speaking points up online for people to enjoy.
Real quick, before I get into all that stuff. The basic idea here is something I’ve talked about before around here. I really believe that learning to build awesome stuff on the internet isn’t that hard and that a big part of the problem is just the mental blocks (“it must have been so hard to learn” people say). The truth of the matter is, after I understood how to pass data between pages, everything else pretty much fell into place. Sure I knew a bit of HTML/CSS, but essentially once I understood a few basics I was able to search for whatever additional information I needed (and if there’s one thing that exists in excess on the web, it’s information about how the web was made).
So, here’s the presentation, followed by some speaking notes. Hope you enjoy. Also, I am seriously thinking about getting some people together in NYC and trying to teach a little class on this. I’m not expert on PHP or programming, but I think that’s part of the fun. Let me know if that sounds interesting and maybe we’ll make it happen.
- A few months ago I taught myself PHP (yes, that’s a recursive initialism).
- In the process, I think I discovered two big mental barriers that I needed to get over: How to pass information from one page to another and what an online database was … So, I want to spend some time going through those two things with the hope of inspiring more people to make stuff on the internet.
- So, let’s get started.
- Pretty much every application on the internet relies on forms to get data from users and input it into the system. Gmail, for instance, has a send button …
- Facebook has “post” for changing your status.
- And Google has “Google Search” to find anything you’re looking for. Essentially when the user hits that button, the information they’ve inputted is sent to a server and handled. The thing is, I didn’t really understand how this happened. Sure, I understood the basic idea, but my question was how do I get it back? Where does the information go?
- The answer, it turns out, depends on what method you use for handling your form. The two most popular are GET (for retrieving information, a la searching) and POST (for sending information to a database, a la an information form). By using one of these two methods in the method=”XX” portion of your form HTML, you chose how you’re going to handle the data that’s inputted.
- How about an example? Google uses GET. The html for the form goes something like method=”GET” action=”/search” (actually they leave out the GET, since it’s the default method for handling form data and they’re all about keeping the file size down on the home page … or at least that’s why I assume they do it)
- Anyhow, GET works something like this. Ever notice all the crazy stuff up in the URL bar when you search on Google? You know, all the stuff after the question mark …
- Well, that’s your query (and a bunch of other stuff). Just look before each equal sign and you’ll start to be able to decipher what it all means.
- But the most important piece of it is the “q=”, since q stands for “query” and that’s where they store whatever you are searching for (try changing that in the URL bar next time you search to see how it works). Essentially Google pulls that information down off the URL bar and builds the page off it. Not too shabby, huh?
- POST, on the other hand, passes data without sending it to the URL bar. I’m sure someone with more knowledge of how this stuff works could explain it better, but unfortunately I’m not that man.
- So finally, just in case, here’s a sample of some PHP that shows how you would grab that query from your GET or POST stored values. Note that the language already has GET and POST built right in and all you need to do is grab those values and then do whatever you want with them. Pretty simple, actually.
- Okay, now for part 2: Databases.
- They’re not really as scary as people think they are.
- Basically they’re just like an excel spreadsheet (which I’m assuming you’ve all played with)
- A database is made up of columns.
- And rows.
- And tables (which Excel calls sheets).
- The big difference lies in how you actually pull information out of the database. Unlike Excel, you can’t just highlight a row and grab that data. Instead you’ve got to use some SQL code to grab your information. This is the basic structure of a SQL call to read data from a database. You select the column you want from the table you want where the value for column x is equal to (or less than, or greater than, etc.) whatever value you’re looking for.
- Let’s say we just wanted to grab all the information from rows where the person lives in California …
- Well, the SQL would look something like this. Select * (which means all columns from that row) from sheet1 (remember, that’s the name of our table, which is basically just a different spreadsheet) where the value in the column “state” equals California. Make sense?
- Thanks to the magic of transparent overlays, we can see that the row that gets returned is just the one we wanted (Fake Fakerton is the only person in our database from California).
- So what about writing data to the database? Well, that’s pretty easy to. You just insert into the table of your choosing the values (x, y, z, zz, etc.) … The only important point here is that this assumes you are inserting data into every column, if you want to skip any you need to specify that. But let’s not worry about it for now …
- Here’s a more specific example. Let’s say you want to add Noah Brier’s (that’s me) info to the DB. You would just use the code above (remember, we’ve got to go in order of the columns we have). So that insert into sheet1 (the table of our choosing) the following values (which are all in parentheses).
- And voila, it’s just added to the next row (assuming your DB is set up that way). Simple as pie.
- So that’s it. I know I haven’t gone that deep, but hopefully this will encourage some people to get started.
That’s it. If you like this maybe I’ll do a few more on the topic.