CiTi · Computer Coding · Year One · Unit 4
Student Walkthrough
Every page you have built so far holds its content in the code. A database moves the content into its own file, so a page can ask for what it needs. Change the data and the page changes, with no HTML edits.
.db file.[search] asks for the records you want.[founditems] repeats once per record.By the end, one short block of code will write a menu of about 100 states and provinces, grouped by country, on your contact form.
A database here is just a text file. Each line is one record. A tab separates the fields. The field names go in a partner file with the same name, ending in .hdr. You will build two tables: country and state.
Make the folder. In CyberDuck, inside amp, make a folder named databases if you do not have one.
Make the country header file. Make a new file named country.hdr in that folder. Type this one line, pressing the Tab key between names, and press Return at the end.
Make the country data file. Make country.db in the same folder. Type these records with a Tab between fields. No field names here: those live in the .hdr file. Press Return after the last line.
Make the state header file. Make state.hdr the same way.
Make the state data file. Make state.db with this sample data. You will swap in the full list in Part F. Return after the last line.
| state.db field | Holds | Example |
|---|---|---|
| SKU | The primary key: every record gets its own number | 1038 |
| CODE | Abbreviation | NY |
| TITLE | Full name | New York |
| R_COUNTRY | The SKU of its country: a foreign key | 1000 |
| STATUS | 1 for active, 0 for not | 1 |
Guam has status 0 on purpose. You will see why in Part D.
Add a path variable. Open includes/vars.inc and add this at the bottom, unless you already have dbvar.
Close the databases. WebDNA keeps a copy of each database in memory, so an uploaded change will not show unless that copy is closed. Add these lines to the very bottom of includes/footer.inc.
.hdr file only.[search] is a tag pair. The opening tag says which database to look in and what to look for. Everything up to [/search] can use the results.
Make the lab page. In your amp/contactus folder, make a new file named search.html. This is your lab page for the lesson. Paste this starter.
Open it. Load amp/contactus/search.html in your browser. You should see I found 7 states.
neSKUdatarq=fa looks strange, but it is five pieces stuck together:
| 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. |
Read aloud: find every record whose SKU is not equal to fa. No SKU is ever fa, so every record passes. That is how we find everything. The letters stand for find all, but they are not a keyword: try banana and you get the same seven.
| 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 |
[numfound] is how many records matched. You already used it. It only works between [search] and [/search].
[founditems] is a loop. Whatever you put inside it repeats once for every record found, top to bottom. Write one line of HTML and get seven.
Replace your first search with this. Save and refresh. You get a numbered list of seven states, but look at the order.
Only the <li> is inside the loop. The <ol> and the count are outside, so they print once.
Now that you can see the list, you can see its problem. With no sort, results come back in no particular order, and not the order of your file.
Add a sort. Add &TITLEsort=1 to the end of your opening [search] tag. The & joins it to the rest of the tag. Save and refresh: the list now runs A to Z, Alabama first.
| Add this | Result |
|---|---|
| &TITLEsort=1 | A to Z by full name |
| &deTITLEsort=1 | Z to A (de = descending) |
| &SKUsort=1&SKUtype=num | Low to high. Numbers need type=num, or 10 sorts before 6. |
Inside [founditems], each field name from your .hdr file becomes a tag that holds that field for the current record. That is what [TITLE] and [CODE] did above. Capitals do not matter.
Try another field. Add [R_COUNTRY] to the list item. Then add [index], which gives the pass number of the loop.
Add three more searches. Paste these under your first search, above the closing </div> tag.
Search 2 has two rq tests, so a record must pass both: Guam is in the United States but its status is 0, so it is left out. Search 3 shows the same tags work on country.db. Search 4 finds nothing, so [founditems] never runs, and the message comes from [showif].
Predict first. Change one thing, save, refresh, record it, and put it back. Learn what each mistake looks like now, so you can recognize it later.
[TITLE] to [TITEL].eqR_COUNTRYdatarq to eqR_CUONTRYdatarq.data: eqR_COUNTRYrq=1000.[search db=[dbvar]state.db].TITLEsort=1 to CODEsort=1. Watch Alaska and Alabama.[TITLE] outside [founditems].state.db to states.db in the search.state.db and upload it.Time for the real thing. Your sample has 7 states. The full list has about 100, so download it and replace your sample.
Download state_country.zipUpload the data. Unzip it and upload all four files to amp/databases/, over the ones you made. Your Search Lab now shows the full list.
Start with a flat list. In contactus/index.html, add this where the state field belongs, after the address. Refresh and open the menu: every state, in one long list.
Look at the option: the value is what gets sent and the text between the tags is what people see. They come from different fields, so the visitor reads Alabama and your form sends AL. The sort is on TITLE because that is the name people read.
Group it by country. Replace the whole select with this version. A search inside a search: for each country, write an optgroup, then run a new search for that country’s states.
Why save [countrysku]? Both tables have SKU, TITLE, and CODE. Inside the inner [founditems], those names mean the state, not the country. Saving the country’s SKU in a variable first keeps it safe.
Check the page. Open the menu: three groups, in alphabetical order inside each. Then View Source: all of that was written by the server.
Send it. Add this line to the email in contactus/thankyou.html, then send yourself one test. The email should say the abbreviation.
data, rq, =value. No spaces.neSKUdatarq=fa..hdr file.[founditems]. [numfound] only works inside [search].type=num..db with a Return, or the last record vanishes.[closedatabase] line in the footer.