← All Lessons Student Walkthrough →

CiTi · Computer Coding · Year One · Unit 4

My First Search

From a flat file to a database-driven select menu on the contact form

Program Computer Coding: Year 1
Unit 4: Building Data-Driven Websites & Client Management
Duration Two class periods (~55 min each)
Grouping Individual build, pair testing
Project AMP Search Lab + contact form

Essential Question

From the Year 1 curriculum map
What purposes do databases serve? What distinguishes relational databases?

Curriculum Map Alignment

From Yr 1 curriculum

Concept / Knowledge / Content

  • Functional database concepts
  • Relational database structure: primary and foreign keys
  • Tables and forms in web design
  • Server-side technology for form processing

Skills / Tasks

  • Create accessible forms
  • Connect HTML forms to server-side scripts

Program Connections

Skills & Portrait alignment

Program Skills: What I’m Building Toward

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

NYS Portrait of a Graduate

Academically Prepared Critical Thinker

Guaranteed & Viable Curriculum Framework

3 Dimensions for this lesson

Integrated Academic Knowledge

Reading a coded instruction piece by piece, using comparison logic, and seeing how one table points at another through a key.

Employability Skills

Following exact file rules, predicting before testing, and reading a silent failure as information.

Technical Skills

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.

Learning Intention

“Unwrapped” from the standard

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.

Success Criteria

  • I can create a .db file and a matching .hdr file with sample data.
  • I can name the five pieces of a search criterion: comparison, field, syntax, required, value.
  • I can write a [search] tag pair and sort its results.
  • I can explain what [numfound] reports and why [founditems] is a loop.
  • I can show field values inside [founditems].
  • I can nest two searches to build a select with optgroups that sends the abbreviation and shows the full name.

Authenticity Continuum

Where this lesson sits
Instruction & Skill Practice
Teacher-Designed Simulation
Realistic Industry Task
Authentic Audience / Client
Product, Service, Impact, Solution

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.

Materials & Prep

  • Student walkthrough (y1-u4-l5-so). Students need their amp folder with its includes and a contact page in contactus.
  • state_country.zip: state.db, state.hdr, country.db, country.hdr. Students upload it in Part F to replace their sample data.
  • Working Search Lab on the teacher AMP site.
  • Finished state menu: AMP Contact Us.

Part 1: Make a Database

Period 1

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.

databases/country.hdr (tabs, shown here as gaps)
SKU TITLE CODE STATUS
databases/country.db
1000 United States of America USA 1 1001 Canada CAN 1 1002 Mexico MEX 1
databases/state.hdr
SKU CODE TITLE R_COUNTRY STATUS
databases/state.db (sample: full data comes in Part 5)
1001 AK Alaska 1000 1 1002 AL Alabama 1000 1 1012 GU Guam 1000 0 1038 NY New York 1000 1 1065 ON Ontario 1001 1 1067 QC Quebec 1001 1 1087 JAL Jalisco 1002 1

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.

Part 2: The First Search

Period 1
The tag pair
[search db=[dbvar]state.db&neSKUdatarq=fa] <p>I found [numfound] states.</p> [/search]

Everything between [search] and [/search] can use the results. Read the criterion one piece at a time:

PieceIn neSKUdatarq=faJob
ComparisonneHow to compare. ne means not equal.
FieldSKUWhich column to look in. Must match the .hdr file.
SyntaxdataThe word that marks this as a search. Always there, never changes.
RequiredrqThis test must pass. With two rq tests, a record has to pass both.
Value=faWhat 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.

CodeMeansExampleFinds in your state.db
eqequalseqR_COUNTRYdatarq=10004 records: the United States
nenot equalneR_COUNTRYdatarq=10003 records: everywhere else
grgreater thangrR_COUNTRYdatarq=1000&R_COUNTRYtype=num3 records
gegreater or equalgeR_COUNTRYdatarq=1001&R_COUNTRYtype=num3 records
lsless thanlsR_COUNTRYdatarq=1001&R_COUNTRYtype=num4 records
leless or equalleR_COUNTRYdatarq=1001&R_COUNTRYtype=num6 records
bwbegins withbwTITLEdatarq=NewNew York
ewends withewTITLEdatarq=skaAlaska

Two criteria: with rq on both, a record must pass both (United States AND active: 3 states). Without rq, passing either one is enough.

Part 3: numfound, founditems, and sorting

Period 1
The loop
[search db=[dbvar]state.db&neSKUdatarq=fa] <p>I found [numfound] states.</p> <ol> [founditems] <li>[TITLE] ([CODE])</li> [/founditems] </ol> [/search]

[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.

The same loop, sorted
[search db=[dbvar]state.db&neSKUdatarq=fa&TITLEsort=1] <p>I found [numfound] states.</p> <ol> [founditems] <li>[TITLE] ([CODE])</li> [/founditems] </ol> [/search]

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.

Part 4: Field Names

Period 1

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.

Part 5: The State Menu

Period 2

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 fieldHoldsExampleUsed for
CODEAbbreviationNYThe value: what the form sends
TITLEFull nameNew YorkWhat the visitor reads, and the sort
R_COUNTRYCountry SKU1000Which optgroup it belongs in
contactus/index.html
<label for="state">State / Province *</label> <select id="state" name="state" required> <option value="">-- Choose one --</option> [search db=[dbvar]country.db&eqSTATUSdatarq=1&SKUsort=1&SKUtype=num] [founditems] [text]countrysku=[SKU][/text] <optgroup label="[TITLE]"> [search db=[dbvar]state.db&eqSTATUSdatarq=1&eqR_COUNTRYdatarq=[countrysku]&TITLEsort=1] [founditems] <option value="[CODE]">[TITLE]</option> [/founditems] [/search] </optgroup> [/founditems] [/search] </select>
What the browser receives
<optgroup label="United States of America"> <option value="AL">Alabama</option> <option value="AK">Alaska</option> ... </optgroup> <optgroup label="Canada"> <option value="AB">Alberta</option> ...

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.

The Tricky Parts

All tested on our server

1A typo in the search fails silently

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.

2No final line return, no last record

A .db file must end with a line return. Without one, the last record disappears and [numfound] says 6.

3Spaces are not tabs

With spaces, the whole line lands in the first field: [numfound] is still right but [TITLE] is blank.

4Field names in both places

With a .hdr file, a header line in the .db becomes a record: one extra found, and one row reads TITLE.

5Uploaded edits do not appear

Without [closedatabase], reloading kept showing old data after an upload. One run of it brought the new data in.

6Sort by the field people read

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.

Teacher Watch List

  • Do not call fa a keyword. Students who think it is magic will not be able to write any other search. The banana test settles it.
  • Check the footer first. “My change isn’t showing” is usually a missing [closedatabase].
  • Every [search] needs [/search], and every [founditems] needs [/founditems]. In the nested version, indent and count them.
  • Keep the search on one line. The criteria are joined with & and no spaces.
  • Test one real send: thankyou.html needs the new State: line, and the email should show the abbreviation, not the full name.

Lesson Flow

Two periods, ~55 min each
TimePhaseWhat happens
Period 1: make it, find it, loop it
5 minHookShow 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 minPart ABuild the .hdr and .db files for both tables, add dbvar and [closedatabase].
15 minPart BBuild the Search Lab with the first search. Take neSKUdatarq=fa apart on the board, then run the banana test.
20 minParts C–DAdd [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 minPart E: Break ItPairs run experiments 1, 2, 5, and 8, predicting first. The rest are for anyone who finishes early, or at home.
15 minFlat ListUpload 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 minNest ItAdd the country search and optgroups. Trace it: one country, then all its states, then the next country.
5 minExit TicketUpdate thankyou.html, do one real send, and finish the exit ticket.

Assessment

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.