CiTi · Computer Coding · Year One · Unit 4

My First Admin

Student Handout

The Admin Pattern

An admin is a set of private pages for changing a site’s data without opening the database file. Every table gets one folder with three pages:

index.htmlLists every record.
add.htmlAdds a record with [append].
edit.htmlChanges a record with [replace].

Three separate pages. The list has no form: it links to add.html, where the form lives. Each form uses action="[thisurl]" to send back to its own page. Every page is built in this order:

  1. The vars.inc include
  2. The processing block (add and edit pages only)
  3. titlevar, header.inc, nav.inc
  4. The page content
  5. footer.inc

Code below is shown as a picture. Type it; pink ___ and <!-- your turn --> lines are yours to finish.

Day 1: Index and Add

Part A: The List

  1. Make the folder. In amp, make 27admin27, and inside it state. Your admin lives at /citi/coding/2027/1/YOURINTS/amp/27admin27/state/.

  2. Make index.html. Build it in the page order above. Inside the content div, add the link to add.html and this search. No form on this page. Fill in the blanks with the other fields.

  • [lookup] turns the country SKU into its name.
  • lookinfield is the field to match in country.db, value is what to match it to, and returnfield is the field to print.
  1. Check it. Load the page. All 101 records should list, sorted by name, with country names, not numbers. An admin shows every record, active or not.

Part B: The Add Form

  1. Make add.html. Same page order. Put this form in the content div and finish it.

  • f_ names keep form values apart from field tags like [TITLE].
  • The hidden action tells the page the form was sent.
  • [thisurl] is this page’s address, so the form sends back to itself.
  • The country option’s value is what R_COUNTRY stores. Check state.db to see what that is.
  • The status values are what STATUS stores: 1 is active, 0 is not.
  1. Try it. Fill it in and submit. Nothing saves yet. That is Part C.

Part C: The Append

  1. Add the processing block. Put this right after the vars.inc include, at the very top of add.html. Fill in the blanks.

PieceJob
[showif [action]=add]Only runs when the form was sent. A plain visit skips it and shows the form.
[append db=...]Adds one new line (record) to the end of the database.
autonumber=SKUWebDNA fills in the SKU: the highest one so far, plus 1.
FIELD=valueOne pair per field. The name must match the .hdr exactly.
&Goes between pairs. This is how WebDNA knows where one value ends.
[url]...[/url]Wraps anything a person typed, so an & in it cannot cut the value off.
[!] [/!]Hides the line returns, so each pair can sit on its own line.
STATUS=[f_status]From the status menu. Menu values are never typed, so no [url].
[closedatabase]Saves to the file now. The footer never runs, because...
[redirect index.html]...the browser is sent back to the list, which shows the new record.
  1. Add a state. Add Trinidad & Tobago, code TT, Active. It should appear on the list.

  2. Check the file. Open state.db in CyberDuck. Find the new line and its SKU.

  3. Check the contact form. Open your contact page. Your new state is in the menu, under its country.

Why does the [append] need its own [closedatabase]?

Break It: Day 1

Predict, change one thing, submit, check the list and state.db, then put it back. Delete test records in CyberDuck when you finish.

1. Remove [url] and [/url] around [f_title]. Add Trinidad & Tobago again.

I predict
What happened

2. Remove the & after the TITLE pair.

I predict
What happened

3. Change TITLE= to TITEL=.

I predict
What happened

4. Remove &autonumber=SKU. Look at the new line in state.db.

I predict
What happened

5. Delete the hidden action input from the form.

I predict
What happened
Day 2: Edit and Replace

Part D: Edit Links

  1. Add an Edit column. In index.html, add this cell as the last one in the row.

Hover each link: every URL carries that record’s SKU. The name is skuvar, never sku, so it cannot get mixed up with the [SKU] field.

Part E: The Edit Form

  1. Make edit.html. Same page order. Put this in the content div and finish it.

Find itThe search finds the one record whose SKU came in the URL.
Fill it[input] puts each value in its box safely, even with quotes.
Select itselected goes on the option that matches the saved value.

Why [statecountry]? Inside the country search, [SKU] means the country. Save the state’s country first, just like [countrysku] in the contact menu.

  1. Check it. Click Edit on New York. Every box, the country, and the status should match the record.

Part F: The Replace

  1. Add the processing block. At the top of edit.html, after vars.inc. Fill in the blanks.

[append] (add.html)[replace] (edit.html)
Does whatAdds a new recordChanges an existing record
Which recordA new one at the endEvery record that matches its search
SKUautonumber=SKU makes oneeqSKUdatarq=[skuvar] finds one
STATUSFrom the menu, Active firstFrom the menu, prefilled with selected
  1. Edit a state. Change Trinidad & Tobago’s status to Inactive and save.

  2. Check it. The list shows 0, state.db shows 0, and it is gone from the contact menu.

Break It: Day 2

Same routine: predict, change, test, check the file, put it back.

6. In edit.html, remove [input] and [/input] around [TITLE]. Edit a state named Trinidad & "Tobago".

I predict
What happened

7. Remove the [showif] around selected on the country option. Edit New York.

I predict
What happened

8. Delete the hidden skuvar input, change a name, and save.

I predict
What happened

Admin Rules to Remember

  • Three pages per table: index, add, edit.
  • The processing block goes at the top, inside a [showif [action]=...].
  • Field names in the append and replace match the .hdr. A typo is dropped with no error.
  • Wrap typed text in [url]. Put & between pairs.
  • Close the database, then redirect.
  • [replace] changes every record its search matches. Search by SKU.
  • Check state.db, not just the page.

Success Criteria Check-In

Exit Ticket

What does [url] protect, and what happened without it? How does [replace] know which record to change? What would happen if its search matched more than one?