Ok, let’s talk about saving pets. No, not like rescuing them, though that’s
really cool too. I’m talking about being able to submit our new pet form
and saving that pet information somewhere so that it shows up on our site.
I want to make our pet list truly dynamic!
We haven’t talked about databases yet, and we’re not using one. But actually,
the pet data our site needs is being stored in a simple pets.json file.
And this file is something we can read from and even update. And hey, that’s
basically all a database really does. So if we can figure out how to update
the pets.json file each time we submit this form, we’re in business!
First, we can re-use our get_pets function to get an array of all of
the existing pets from the file. Let’s add this right at the bottom of
our form processing code:
Next, create an associative array that represents the new pet that’s being
added. Make sure the keys you’re using match the existing pets from the file.
We don’t have age or image fields yet, so just set those to be blank:
Ok, refresh and re-post the form. Hmm, it looks like it did nothing. But
that’s not true! We submitted the form, our code detected this was a POST
request, we saved the new stuff to pets.json, and then the page continued
rendering the blank form. There weren’t any fireworks, but I think this worked!
Go to the homepage to find out for sure! We didn’t give it an image, but
there’s our pet. We don’t even have a database, and we already have a dynamic
If you look at pets.json, it got flattened onto one line. That’s ok!
Spaces and new lines aren’t important in JSON, and PHP saved without any
extra whitespace. Again, that’s fine really.
But since I did like my file better when it was readable, give json_encode
a second argument of JSON_PRETTY_PRINT:
Fill out our form again. Hey, now pets.json looks awesome again. We are
really good at training this digital pet :) JSON_PRETTY_PRINT
is called a constant, which is kind of like a variable, exept that it’s
magically available everywhere, doesn’t have a $, and its value can’t
change. You won’t use them often, so don’t worry about them too much.