CiTi · Computer Coding · Year One · Unit 4
From a flat file to a database-driven select menu on the contact form
What purposes do databases serve? What distinguishes relational databases?
Hands-On:
#4 Design a relational database
#5 Write and execute database queries
#7 Validate and debug my own work
Professional:
#7 Treat a mistake as data, not failure
#9 Document as you go
Reading a coded instruction piece by piece, using comparison logic, and seeing how one table points at another through a key.
Following exact file rules, predicting before testing, and reading a silent failure as information.
Building .db and .hdr files, writing a search with criteria and sorting, looping over results, and nesting two searches to generate a grouped select menu.
Students will create a database, write searches against it, and display the results in a loop, then use two related tables to generate an organized state and province menu on their contact form.
[search] tag pair and sort its results.[numfound] reports and why [founditems] is a loop.[founditems].Skill practice aimed at a real page: the select they finish with is a real piece of the Aimee Mohr project, on its contact page.
amp folder with its includes and a contact page in contactus.state.db, state.hdr, country.db, country.hdr. Students upload it in Part F to replace their sample data.A WebDNA database is a plain text file. Each line is one record, and a tab separates the fields. The .hdr file holds one line of field names. The .db file holds only the data, and the two share a name in the same folder. Students type both tables themselves.
Students add dbvar to vars.inc and [closedatabase] lines to footer.inc. WebDNA keeps each database in memory, so without those lines an uploaded edit does not appear. Guam has status 0 on purpose: a later search in the lab leaves it out. Alaska and Alabama are there for the sorting lesson.
Everything between [search] and [/search] can use the results. Read the criterion one piece at a time:
| Piece | In neSKUdatarq=fa | Job |
|---|---|---|
| Comparison | ne | How to compare. ne means not equal. |
| Field | SKU | Which column to look in. Must match the .hdr file. |
| Syntax | data | The word that marks this as a search. Always there, never changes. |
| Required | rq | This test must pass. With two rq tests, a record has to pass both. |
| Value | =fa | What to compare against. |
Say it aloud: “Find every record whose SKU is not equal to fa.” No SKU is ever fa, so every record passes. That is why this is our find-all. The letters are shorthand for find all and are not a keyword: neSKUdatarq=banana finds the same seven records. A search with no criterion finds nothing, so a find-all has to ask something.
| Code | Means | Example | Finds in your state.db |
|---|---|---|---|
| eq | equals | eqR_COUNTRYdatarq=1000 | 4 records: the United States |
| ne | not equal | neR_COUNTRYdatarq=1000 | 3 records: everywhere else |
| gr | greater than | grR_COUNTRYdatarq=1000&R_COUNTRYtype=num | 3 records |
| ge | greater or equal | geR_COUNTRYdatarq=1001&R_COUNTRYtype=num | 3 records |
| ls | less than | lsR_COUNTRYdatarq=1001&R_COUNTRYtype=num | 4 records |
| le | less or equal | leR_COUNTRYdatarq=1001&R_COUNTRYtype=num | 6 records |
| bw | begins with | bwTITLEdatarq=New | New York |
| ew | ends with | ewTITLEdatarq=ska | Alaska |
Two criteria: with rq on both, a record must pass both (United States AND active: 3 states). Without rq, passing either one is enough.
[numfound] is the count. It works anywhere inside the search, before or after the loop, and prints as its own name outside it. [founditems] is the loop: its contents repeat once per found record, top to bottom, so one <li> in the code becomes seven on the page. [index] gives the current row number. When [numfound] is 0 the loop never runs, which is why the “nothing found” message belongs in a [showif [numfound]=0].
Sorting waits until here on purpose: with only [numfound] on the page there is no order to see. Once the list prints it comes out scrambled (New York, Ontario, Alabama), which gives students the reason to sort.
Sorting: TITLEsort=1 sorts A to Z, deTITLEsort=1 reverses it. Numbers also need SKUtype=num, or 10 lands before 6. With no sort at all the order is whatever WebDNA returns, not the order in the file.
Inside [founditems], every field name in the .hdr file becomes a tag holding that value for the current record: [CODE], [TITLE], [R_COUNTRY]. Capitals do not matter. Outside the loop, or spelled wrong, the tag prints as literal text, which makes these easy typos to spot.
Everything comes together. state.db has a foreign key, R_COUNTRY, that holds the SKU of a country.db record: the same idea as the game-store data map. The outer search makes one <optgroup> per country, and the inner search fills it with that country’s states.
| state.db field | Holds | Example | Used for |
|---|---|---|---|
| CODE | Abbreviation | NY | The value: what the form sends |
| TITLE | Full name | New York | What the visitor reads, and the sort |
| R_COUNTRY | Country SKU | 1000 | Which optgroup it belongs in |
The line [text]countrysku=[SKU][/text] matters because both tables have SKU, TITLE, and CODE. Inside the inner [founditems] those names mean the state record, so the country’s SKU is saved first. Countries sort by SKU so the United States comes first.
eqR_CUONTRYdatarq=1000 and eqR_COUNTRYrq=1000 (no data) both return 0 with no error. A wrong field name in the output at least shows as [TITEL]. A wrong database name does print an error: Database file could not be found or opened.
A .db file must end with a line return. Without one, the last record disappears and [numfound] says 6.
With spaces, the whole line lands in the first field: [numfound] is still right but [TITLE] is blank.
With a .hdr file, a header line in the .db becomes a record: one extra found, and one row reads TITLE.
Without [closedatabase], reloading kept showing old data after an upload. One run of it brought the new data in.
The state abbreviation is in CODE, so CODEsort=1 gives Alaska (AK) before Alabama (AL). Sorting on TITLE, the full name, puts them in alphabetical order for the reader.
[closedatabase].[search] needs [/search], and every [founditems] needs [/founditems]. In the nested version, indent and count them.& and no spaces.thankyou.html needs the new State: line, and the email should show the abbreviation, not the full name.| Time | Phase | What happens |
|---|---|---|
| Period 1: make it, find it, loop it | ||
| 5 min | Hook | Show the state menu on the AMP contact page, then view its source: dozens of options nobody typed. “One short block of code wrote all of this.” |
| 15 min | Part A | Build the .hdr and .db files for both tables, add dbvar and [closedatabase]. |
| 15 min | Part B | Build the Search Lab with the first search. Take neSKUdatarq=fa apart on the board, then run the banana test. |
| 20 min | Parts C–D | Add [founditems], trace the loop on the board record by record, point out the scrambled order and add the sort, then add fields. Paste in the three more searches if time allows. |
| Period 2: the state menu | ||
| 15 min | Part E: Break It | Pairs run experiments 1, 2, 5, and 8, predicting first. The rest are for anyone who finishes early, or at home. |
| 15 min | Flat List | Upload the full data, then one search with all states and no groups. Point out that the value and the visible text are different fields. |
| 20 min | Nest It | Add the country search and optgroups. Trace it: one country, then all its states, then the next country. |
| 5 min | Exit Ticket | Update thankyou.html, do one real send, and finish the exit ticket. |
Formative: Live check of contactus/search.html: all four searches work, and the count, sorting, and no-results message are correct. Then contactus/index.html: three optgroups, states in alphabetical order, and a test email showing the abbreviation.
Exit ticket (completion-graded): explain neSKUdatarq=fa piece by piece, and explain why the state search needs [countrysku].
Extension: Add a Canada-only list to the Search Lab, then show each state’s country name by searching country.db inside the loop.