Week 3

Getting information from the user

Homework

Reading

Production

Using PHP, esophagitis add the interactive elements to the front-end you build last week. Implement the $_GET and/or $_POST arrays available in PHP.

Due next class (2/18). Blog write-up due 8pm Feb 17.

Tech prep for next week
(see video links in the sections below)

Accessing the file system with PHP

Along with the videos, more about refer to the PHP.net documentation for:
file_get_contents 
file_put_contents

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens

/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you to set a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just  add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”.  This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

 

Watch:
Reading files with file_get_contents: http://www.youtube.com/watch?v=Hr_HcMEBPdo
Writing to files with file_put_contents: http://www.youtube.com/watch?v=blSAiNRmauc

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data are file, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

Watch: Basics of csv : http://www.youtube.com/watch?v=n7LiO6F7O8I&list=PL65AA826CE34FD00F

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript  code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

[
{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
},
{
"name":"Penn Station",
"location": "1 Penn Plaza"
}
]

Watch: What is JSON? http://www.youtube.com/watch?v=40aKlrL-2V8

Parsing JSON with PHP

Refer to the PHP.net documentation for json_encode and json_decode functions in conjunction with the videos

Watch: Very quick demos:
json_decode http://www.youtube.com/watch?v=-iMR0uac2Kg
json_encode http://www.youtube.com/watch?v=LdxW9OSPK-o

Saving JSON data to a file with PHP
Watch: Lynda.com, Working with Data on the Web, Ch. 4, section on “Updating your JSON data” http://www.lynda.com/CSS-tutorials/Updating-your-JSON-data/133326/145995-4.html

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>