english

learn php

learn css

learn html

learn javascript

learn php

12. text files & json

assignments
5 minutes / 889 words

twelve / assignments

assignment 12.1

write a program that takes an airport code and returns current weather conditions.

you can use this html form as a starting point...

<form method="get" action="forecast.php">
    <input type="text" name="airport" placeholder="jfk" />
    <select name="forecast">
        <option value="daily">daily</option>
        <option value="hourly">hourly</option>
    </select>
    <input type="submit" value="get conditions" />
</form>

forecasts are available as json for any location in the united states by accessing the free government api. you're welcome to find a free weather service in another country if you like but this one is easy and convenient to practice with.

to read the daily forecast...

https://api.weather.gov/gridpoints/{office}/{gridX},{gridY}/forecast

to read the hourly forecast...

https://api.weather.gov/gridpoints/{office}/{gridX},{gridY}/forecast/hourly

you can get the specific addresses from latitude and longitude...

https://api.weather.gov/points/{latitude},{longitude}

and you can find the latitude and longitude for any airport from its code...

https://airportsapi.com/api/airports/{airport}

note that airport codes are not case-sensitive but this api is so you need to send the code as all-caps to get a successful result.

these all return json in predictable formats for free with no signups or logins required. try them from your browser and look at their api specifications documentation.

if no forecast is found or no airport code matches, don't forget to include appropriate error messages.

keep track of which airports have been searched and include links to the last ten searches as well as the form at the top of every page. you'll need to store the list as a delimited string instead of an array then separate it into an array because cookies are always strings.

assignment 12.2

write a program that contains a basic blog executed using json files with authentication for a single user.

user information should be stored in a single json file and allow a change in password. don't forget to use hashing to secure the password with one-way encryption.

each blog entry should be stored as two files, one json with the date/time and title, the other plain text with the body of the post.

you should be able to add, edit and delete posts including changing their date/time as well as see a list of entries and display their contents.

don't forget to wrap each line of the post between <p> and </p> when you display them.

you can use this html as a starting point for the login screen...

<form method="post" action="blog.php?action=login">
    <input type="text" name="username" placeholder="username" />
    <input type="password" name="password" placeholder="password" />
    <input type="submit" value="login" />
</form>

you can use this html as a starting point for the posts...

<form method="post" action="blog.php?action=edit">
    <input type="text" name="title" placeholder="post title" value="" /><br />
    <input type="datetime-local" name="datetime" value="" /><br />
    <textarea name="body" placeholder="blog post"></textarea><br />
    <input type="submit" value="save" />
</form>

content goes in the value properties for input and between the textarea and /textarea tags for textarea. datetime-local uses yyyy-mm-ddThh:mm for date and time so you'll need to convert between that and unix time using the methods we've already seen.

don't forget about special characters like " and <> that might be in these fields.

you can use this html as a starting point for the lists of posts...

<ul>
    <li><a href="blog.php?action=show&post=12345">post title</a> (2190.5.27 at 4.20)</li>
    <li><a href="blog.php?action=show&post=12346">post title</a> (2190.5.27 at 4.21)</li>
    <li><a href="blog.php?action=show&post=12347">post title</a> (2190.5.27 at 4.22)</li>
</ul>

you should be able to write this as a single php file with various actions depending on what the user is trying to do. it's ok to display the login form at the bottom of every page if that makes it easier for your process. don't worry about writing complete html pages. these html segments are enough to make the program work and we'll deal with the html side of things later.

assignment 12.3

find a wordlist to use. there are many floating around but you can grab a free 4mb list of only alphabetic words in english from the "english-words" project on github. any wordlist is fine, though.

get your program to download the wordlist if it's not already loaded

now select a random word at least five letters long and discover all the anagrams that can be made from it. don't forget that simply searching for letters won't tell you how many of each there are so you'll have to count. ("otter" has the specific letters from "other" but "other" doesn't have enough ts to make "otter".)

display the word and all possible words made from its letters in length then alphabetical order.

learn php

learn css

learn html

learn javascript

powered by akelei. an avi sato project.