Articles Archive
Articles Search
Director Wiki
 

Using Director with Databases: Part 3

February 23, 2001
by Gary Rosenzweig

This week, I will complete the database program we have been building for the last two columns. The key features that are missing are the ability to modify and delete records.

To do either of these functions, you must give the user the ability to select a record. We already have the ability to list the record in a scrolling HTML table. To allow the user to select one, we can easily add a small piece of hypertext to each line so that they can click and select it for modification. This requires only one line of code to be added to the listData handler. This will add a column with the word EDIT in it. This word will be hyperlinked with the number of the record as the hyperlink data.

Here is the additional line of code needed in the listData handler. Check out the example movie to see exactly where it goes.

-- link to edit record
put " " after html

Now, when the user selects List Data from the main menu, they get a list with a hyperlinked EDIT before every row. Clicking on this will send a message to the sprite that we can intercept with hyperlinkClicked. This will pass the hyperlink data in as a string, which we can then convert to a number and send to a new handler: editRecord.

on hyperlinkClicked me, href

  recordNum = value (href)
  editRecord (recordNum)

end

The editRecord handler will get the fields from the specified record and place them in the same members as used by the New Entry frame. However, we'll jump to the new frame Edit Record instead. This will include the buttons Record Changes and Delete Record instead of the buttons Clear and Add This. The handler will also use a new global variable, gEditRecord, to store the number of the record being edited. This will come in handy when it is time to replace the old data with the new data.

on editRecord n

  -- remember which is being edited
  gEditRecord = n

  -- get record from database
  record = gDatabase[gEditRecord]

  -- put fields from record onto screen
  member ("entry-name").text = record["Name"]
  member ("entry-street").text = record["Street"]
  member ("entry-city").text = record["City"]
  member ("entry-state").text = record["State"]
  member ("entry-zip").text = record["Zip"]
  member ("entry-phone").text = record["Phone"]
  member ("entry-birthday").text = record["Birthday"]

  go to frame "Edit Record"

end

The user can select and edit any text in any of the fields. When they are done, they would hit the Record Changes button which will call the following handler. It will look a lot like the old addNewEntry handler, but instead of adding the data to gDatabase, it will replace the item in gDatabase indicated by gEditRecord.

on recordChanges

  -- get all fields and make a new record
  newEntry = [:]
  addProp newEntry, "Name", member ("entry-name").text
  addProp newEntry, "Street", member ("entry-street").text
  addProp newEntry, "City", member ("entry-city").text
  addProp newEntry, "State", member ("entry-state").text
  addProp newEntry, "Zip", member ("entry-zip").text
  addProp newEntry, "Phone", member ("entry-phone").text
  addProp newEntry, "Birthday", member ("entry-birthday").text

  -- replace old record with new one
  gDatabase[gEditRecord] = newEntry

  -- return to list view
  listData

end

The deleteRecord handler also uses the gEditRecord global so that it can identify and delete the proper item from the gDatabase list.

on deleteRecord

  -- remove record from database
  deleteAt gDatabase, gEditRecord

  -- return to list view
  listData

end

Both deleteRecord and recordChanges will call listData when they are done to return to the list display of the database. This will allow the user to quickly modify or delete several records without having to return to the main menu each time.

While this database program now has a lot of functionality in it, it is nowhere near complete. The next step I would take would be to add a search function. This would be a modification of the listData handler that would only display records meeting certain criteria. While this isn't needed with a small database, it is almost necessary with a larger one. I'll explain more next week.

A sample Director 8 movie is available for download in Mac or Windows format.

Gary Rosenzweig's two most recent books are:

Special Edition Using Director 8 -- The most comprehensive guide to Director ever, including tons of examples and demo movies. It's suitable for novices and experts alike.

Advanced Lingo for Games -- How to make games with Director 7 and 8. This book comes complete with full source code for more than 20 complete games.

More information about these books can be found at http://clevermedia.com/resources/bookstore/. They can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.