CiTi · Computer Coding · Year One · Unit 4

My First Search

Student Walkthrough

The Big Idea

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.

1. StorePut records in a .db file.
2. Ask[search] asks for the records you want.
3. Loop[founditems] repeats once per record.
4. ShowField names print each value.

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.

Part A: Make a Database

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.

  1. Make the folder. In CyberDuck, inside amp, make a folder named databases if you do not have one.

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

SKU TITLE CODE STATUS
  1. 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.

1000 United States of America USA 1 1001 Canada CAN 1 1002 Mexico MEX 1
  1. Make the state header file. Make state.hdr the same way.

SKU CODE TITLE R_COUNTRY STATUS
  1. 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.

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
state.db fieldHoldsExample
SKUThe primary key: every record gets its own number1038
CODEAbbreviationNY
TITLEFull nameNew York
R_COUNTRYThe SKU of its country: a foreign key1000
STATUS1 for active, 0 for not1

Guam has status 0 on purpose. You will see why in Part D.

  1. Add a path variable. Open includes/vars.inc and add this at the bottom, unless you already have dbvar.

[!] database folder [/!] [text]dbvar=[pathvar]databases/[/text]
  1. 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.

[closedatabase db=[dbvar]country.db] [closedatabase db=[dbvar]state.db]
  • Tabs, not spaces. And field names go in the .hdr file only.
  • Never name a field after a tag, such as date, time, text, or url.

Part B: Write Your First Search

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

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

[include file=/citi/coding/2027/1/YOURINTS/amp/includes/vars.inc] [text]titlevar=Search Lab[/text] [include file=[incvar]header.inc] [include file=[incvar]nav.inc] <div class="content"> <h2>Search Lab</h2> [search db=[dbvar]state.db&neSKUdatarq=fa] <p>I found [numfound] states.</p> [/search] </div> [include file=[incvar]footer.inc]
  1. Open it. Load amp/contactus/search.html in your browser. You should see I found 7 states.

Reading the search

neSKUdatarq=fa looks strange, but it is five pieces stuck together:

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.

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.

Write eqR_COUNTRYdatarq=1001 as a plain English sentence

Comparisons

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

Part C: numfound, founditems, and sorting

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

Pass 1Current record: the first state found. Write one list item.
Pass 2Current record: the next state. Write another.
Passes 3 to 7Same line, next record, until none are left.
  1. Replace your first search with this. Save and refresh. You get a numbered list of seven states, but look at the order.

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

Only the <li> is inside the loop. The <ol> and the count are outside, so they print once.

Move the <p>I found</p> line inside founditems. What happens, and why?

Sorting

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.

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

[search db=[dbvar]state.db&neSKUdatarq=fa&TITLEsort=1] <p>I found [numfound] states.</p> <ol> [founditems] <li>[TITLE] ([CODE])</li> [/founditems] </ol> [/search]
Add thisResult
&TITLEsort=1A to Z by full name
&deTITLEsort=1Z to A (de = descending)
&SKUsort=1&SKUtype=numLow to high. Numbers need type=num, or 10 sorts before 6.

Part D: Field Names

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.

  1. Try another field. Add [R_COUNTRY] to the list item. Then add [index], which gives the pass number of the loop.

  2. Add three more searches. Paste these under your first search, above the closing </div> tag.

[!] 2. Two required answers: United States AND active [/!] [search db=[dbvar]state.db&eqR_COUNTRYdatarq=1000&eqSTATUSdatarq=1&TITLEsort=1] <h3>Active United States</h3> <p>I found [numfound].</p> <ul> [founditems] <li>[TITLE]</li> [/founditems] </ul> [/search] [!] 3. The other table. Numbers need type=num. High to low with de [/!] [search db=[dbvar]country.db&eqSTATUSdatarq=1&deSKUsort=1&SKUtype=num] <h3>Countries, highest SKU first</h3> <ol> [founditems] <li>[SKU]: [TITLE]</li> [/founditems] </ol> [/search] [!] 4. When nothing matches, founditems never runs [/!] [search db=[dbvar]state.db&eqR_COUNTRYdatarq=9999] <h3>Country 9999</h3> [showif [numfound]=0]<p>No states found.</p>[/showif] [founditems] <p>[TITLE]</p> [/founditems] [/search]

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

Part E: Break It

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.

1. Change [TITLE] to [TITEL].

I predict
What happened

2. Change eqR_COUNTRYdatarq to eqR_CUONTRYdatarq.

I predict
What happened

3. Remove data: eqR_COUNTRYrq=1000.

I predict
What happened

4. Remove the whole criterion: [search db=[dbvar]state.db].

I predict
What happened

5. Change TITLEsort=1 to CODEsort=1. Watch Alaska and Alabama.

I predict
What happened

6. Move [TITLE] outside [founditems].

I predict
What happened

7. Change state.db to states.db in the search.

I predict
What happened

8. Delete the Return after the last line of state.db and upload it.

I predict
What happened

Part F: The State Menu

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.zip
  1. Upload 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.

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

<label for="state">State / Province *</label> <select id="state" name="state" required> <option value="">-- Choose one --</option> [search db=[dbvar]state.db&eqSTATUSdatarq=1&TITLEsort=1] [founditems] <option value="[CODE]">[TITLE]</option> [/founditems] [/search] </select>

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.

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

<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>
Outer loopFinds a country, saves its SKU, opens an optgroup.
Inner searchFinds states whose R_COUNTRY equals that SKU, and writes their options.
Back outCloses the optgroup and moves on to the next country.

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.

  1. Check the page. Open the menu: three groups, in alphabetical order inside each. Then View Source: all of that was written by the server.

  2. Send it. Add this line to the email in contactus/thankyou.html, then send yourself one test. The email should say the abbreviation.

State: [state]

Search Rules to Remember

  • Five pieces: comparison, field, data, rq, =value. No spaces.
  • Every search needs a criterion. To get everything, use neSKUdatarq=fa.
  • A misspelled field in a search finds 0 with no error. Check the .hdr file.
  • Field tags only work inside [founditems]. [numfound] only works inside [search].
  • Sort numbers with type=num.
  • End every .db with a Return, or the last record vanishes.
  • Every database gets a [closedatabase] line in the footer.

Success Criteria Check-In

Exit Ticket

Take apart neSKUdatarq=fa. What does each piece do? Why does the state search use [countrysku] and not [SKU] in its own results?