Working with Data on the Web
Illustration by John Hersey

Creating a YAML data file


From:

Working with Data on the Web

with Joseph Lowery

Video: Creating a YAML data file

In earlier chapters, I showed you how to take form values entered online and save them in a specific format like CSV, XML, and JSON. want to see how to do it in (UNKNOWN) and PHP? Who wouldn't? Lets keep going. Well, I've got index.php from the chapter 5, 05_02 folder now open in my code editor. Again, there's a form on the page and it's down around line 40 there. That asks for name, email, date of birth and some other data.

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course Working with Data on the Web
2h 21m Intermediate Aug 21, 2013

Viewers: in countries Watching now:

Often, designers need to convey simple data without the overhead of a relational database such as MySQL. This course introduces web technologies that make it easy for a designer or developer to store, retrieve, and display data online.

First, discover how data is collected via standard and advanced HTML5 forms. Then look at a client-friendly approach to storing that data using simple CSV files. Next, Joseph shows how to store, manage, and style data with the three big players: the granddaddy of static data, XML; the popular JavaScript-based JSON; and the recursively named YAML (YAML Ain't Markup Language), frequently used in Ruby-based projects. Plus, take a close look at basic HTML5 data options, including local storage and the flexible data attribute.

Topics include:
  • Interacting with data through HTML forms
  • Storing data as comma-separated values
  • Saving, retrieving, and displaying data as XML
  • Setting up and updating JSON data
  • Creating a YAML data file
  • Using HTML5 data storage solutions
Subjects:
Developer Web
Software:
HTML XML CSS JSON YAML
Author:
Joseph Lowery

Creating a YAML data file

In earlier chapters, I showed you how to take form values entered online and save them in a specific format like CSV, XML, and JSON. want to see how to do it in (UNKNOWN) and PHP? Who wouldn't? Lets keep going. Well, I've got index.php from the chapter 5, 05_02 folder now open in my code editor. Again, there's a form on the page and it's down around line 40 there. That asks for name, email, date of birth and some other data.

However, the code we'll be using is not form specific, so it can be used with most, if not all forms. Let's head back up to the top of the page and bring in our PHP. So, I'll start with a PHP code block. Now, the very first line of code that I'm going to bring in is an include. And it's an include that I've found is the easiest way to output YAML code. It's done with a special PHP class called, Spice.

I'm assuming that's how you pronounce it anyway. What makes it simple is that all you need to do is to include the spice.php file, and the code library is available. I've downloaded a copy, so let's incorporate it. We'll use the include function, set a parentheses and then in single quotes Spyc.php. I have mine in upper case, so that's why we have the upper case S there. Follow that with a semicolon.

Now, as we've done in the other chapters, the form is set up with the Post method and an empty action. So, when it's submitted, the information will be posted to the same page. To run the code only when that happens, we need to add an IF statement that checks to see if submit is set in the posted data. And if it is, that means that the Submit button was clicked. So let's create our if statement. If parentheses is set, the keyword there, another set of parentheses to find out what set.

We're going to check the POST array, and specifically within the array, we'll look for submit. So you use square brackets after $_POST, and within those square brackets, single quotes. And what it is you're looking for, the name submit. Okay. So at the end of that line, if that's true, let's put in our curly braces. And we'll add our functionality there. First, we'll define a variable to hold our file name.

And I'll call this $file and set that equal to in quotes data.yaml. Now, just to show you, I have data.yaml already created. It's a empty file, and we'll be filling that in with our data. Okay. Back to index.php. Next, we'll remove the submit element from the array via the unset function. So, unset Parenthesis, $_POST, square brackets, submit. Now we're ready to add a little spice. Oh, come on, who could resist? I'll define a new variable called YAML, and then set that equal to Spycyamldumppost.

There's a particular syntax here, so watch carefully. So my variable is $yamel, and we'll set that equal to Spyc::. And the function name within Spyc::YAMLDump, and then what it is we're gong to dump out. It's the post array. Now once we have that in a variable, all we need to do is to write our data using the file_put_contents function.

So file_put_contents parenthesis and then there are three arguments here. The file we're going to put the contents into, which we've already declared as a variable file, what are the contents that we're putting into the file and that's $YAML, and then finally we'll use the FILE_APPEND parameter. So that we'll be recording one record after another.

Alright? I'll finish this off with a semicolon, and our last line is a redirect using the header function. Make sure you turn your caps lock off, if you haven't already. Header, parentheses, single quotes, location, colon thanks.php. Alright. There's our final semicolon, and this code is baked. Alright. Let's save the page, and we're ready to try it out in our local web server. So back over to the browser, here's my page that I preloaded.

I'll click Refresh, there's our form. Enter a good friend of mine, Mr D Votion. I'm not kidding, it's actually his name, but not his email address. Give him an own special number, and oh, he spent many years studying art. Alright. Let's submit it.

Good, we've got our thank you. That means we didn't encounter any errors. So, let's head back and check our data.yaml file. I'll click to that. I get a message from Aptana saying my file's changed. Do I want to see them? You bet I do. And I click yes. And there's my YAML output. Not bad. There's all the data in a standard YAML format. Now, Spice isn't quite sophisticated enough to add any indentation, but that's okay. The three dashes, that separate each record give us a valid YAML document.

Thanks to the File Append argument, the next site visitor who fills out the form, will be added to the YAML file. We're good to go.

There are currently no FAQs about Working with Data on the Web.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Working with Data on the Web.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.