Creating a YAML data file
Video: Creating a YAML data fileIn 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.
- Next steps
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.
- 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
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.