intermediate web-app

Listing Records from a Database

Most pages show a list — articles, products, users, messages. The pattern is always the same: get the count, fetch the rows, iterate from 0 to count minus one, render each one. Learn this pattern once and you can build any list.

Part of: Frame framework

Step 1: get the count. Step 2: fetch all rows.

	GET "/posts" :
		string count_res = db.query("SELECT CAST(COUNT(*) AS CHAR) as cnt FROM posts WHERE published = 1", "[]")
		integer post_count = json.get(count_res, "data.rows.0.cnt").toInteger()

		string posts_res = db.query("SELECT title, slug, excerpt FROM posts WHERE published = 1 ORDER BY created_at DESC", "[]")
		string cards = ""

Always get the count first — you need it to know the iterate range. CAST(COUNT(*) AS CHAR) returns the count as a string, then .toInteger() converts it. Two queries run sequentially: count first, then the full data fetch.

Step 3: iterate through the rows and build HTML.

		iterate i in 0 to post_count - 1
			string idx = i.toString()
			string post_title = json.get(posts_res, "data.rows." + idx + ".title")
			string post_slug = json.get(posts_res, "data.rows." + idx + ".slug")
			string post_excerpt = json.get(posts_res, "data.rows." + idx + ".excerpt")
			cards = cards + post_card(post_title, post_slug, post_excerpt)

		return http.respond(200, "text/html", cards)
GET /posts → HTML list of all published posts

Build the json.get path dynamically: "data.rows." + idx + ".column". This works for any number of rows. The cards string grows with each iteration — concatenation is how you assemble a list of components.

  • Count rows first with COUNT(*), then fetch the full result set
  • CAST(COUNT(*) AS CHAR) + .toInteger() converts the count to an integer
  • iterate i in 0 to count - 1 loops through every row
  • Access each row with json.get(result, "data.rows." + i.toString() + ".column")
📚 → Book 1, Chapter 7 : Query Parameters and Search →
Copied!