Articles Archive
Articles Search
Director Wiki
 

Using Director with Databases Part 4

March 8, 2001
by Gary Rosenzweig

This week we'll add one last feature to the database program that we have been building for the last three columns. I also want to discuss alternative ways to store data in Director. While the previous versions have had the ability to list all of the entries in the database, we will need the ability to only list selected entries. This will become important as the database grows. Adding a search function is not too difficult if we just alter the existing code a bit. The listData handler previously created an HTML table containing all of the records in the database. But what if we passed a list into this handler that told it which entries it should list. For instance, if we passed into it the value [1, 3], it would list only record 1 and 3 of the database. At the same time, let's make the listData handler robust enough that we don't have to alter any of the existing calls to it. If no list is passed in to the handler, it will assume that we want all records listed.

Below is the new handler. Besides the parameter list after the declaration name; the only new lines come after the -- see if there is a list to follow comment. This will first check to see if the value of list is VOID. If it is not, then it will check to see if this record number is found in the list. If it is not in the list, then it will use next repeat to skip the record.

-- show entire database in HTML list

on listData list

  if gDatabase.count < 1 then
    -- if no data yet, then show message instead
    member ("list").text = "No data."
    
  else
    -- start html code
    html = "<HTML><BODY><TABLE BORDER=1><TR>"
    
    -- create headings row
    repeat with i = 1 to gDatabase[1].count
      put
"<TD>" & getPropAt (gDatabase[1], i) & "</TD>" after html
    end
repeat
    put
"</TR>" after html
    
    -- put each entry in a row
    repeat with i = 1 to gDatabase.count
      
      -- see if there is a list to follow
      if not voidP (list) then
        
        -- skip if not in list
        if not getOne (list, i) then next
repeat
        
      end
if
      
      put
"<TR>" after html
      
      -- link to edit record
      put
"<TD><A HREF='" & i & "'>EDIT</A></TD>" after html
      
      -- put each item in a column
      repeat with j = 1 to gDatabase[i].count
        put
"<TD>" & gDatabase[i][j] & "</TD>" after html
      end
repeat
      put
"</TR>" after html
    end
repeat
    
    -- end html code
    put
"</TABLE></BODY></HTML>" after html
    
    -- put html into member
    member ("list").html = html
  end
if

  go to frame "list data"

end

In order to call the new listData handler, we need to first build the list of records somehow. I've added a new button labeled Search to the main menu of the program. When the user presses this button, they get taken to a "Search" frame that will have a text field that they can use to enter the search term. They can then press the Go button on that frame which will run the search handler.

The search handler gets the text they entered and loops through all of the fields of all of the records looking for it. It will use the contains function to determine if there is a match. So a search for "gar" will match with "gar" or "gary".

on search

  -- get text to search for
  searchText = member ("search text").text

  -- init list of found records
  list = []

  -- loop through all records
  repeat with i = 1 to gDatabase.count
    
    -- loop through all fields
    repeat with j = 1 to gDatabase[i].count
      
      -- see if the search text was found in the field
      if gDatabase[i][j] contains searchText then
        
        -- add record number to list
        add
list, i
        next
repeat
        
      end
if
      
    end
repeat
    
  end
repeat

  -- only list found records
  listData (list)

end

This method of searching is pretty general. It will find the substring in any field of each record. You may want to only search a specific string, or possibly use the = comparison rather than contains for an exact match.

The result of a search will look just like the full list of all of the records, except that not all of the records will necessarily be listed. The program is now a pretty full-featured database. You can use this code as a base to build your own specific application. However, if you plan on having a lot of data, I have some suggestions on how you can better store it all.

One method that I have used successfully in the past is to store each record as its own text cast member. I can then use an external cast library as the database, and each member is a record. You can either store a string-converted list in each member, or use each line of each member as a field of that record. If you are comfortable with parsing text, you can create your own markup language to store the data in each text member. Or, you could even use the XML Xtra to store XML data as text in each member, but I've never tried that.

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

Gary Rosenzweig is the author of six books on Macromedia Director and Flash. He also publishes a weekly email newsletter for Flash and Director developers called the Developer Dispatch. You can subscribe to this newsletter at http://developerdispatch.com.

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-2017, Director Online. Article content copyright by respective authors.