Re: How do I…

Igor de Oliveira Couto igor at superstudent.net
Tue May 15 18:11:16 EDT 2012


Dear Dan,

I'm a newbie to LiveCode, but your question is more of an algorithm question than a language-specific one, so I'll give it a shot:

On 16/05/2012, at 1:50 AM, Dan Friedman wrote:

> I could use a little help...  Say you have a search field.  In the script of that field you use the rawKeyUp message to search for whatever it is they typed (search your database, compile a list and display it neatly in a list).  How would you structure this so that when they typed a second character in the search field, the executing rawKeyUp will halt and start over using the updated search string?

You will need to have a command that does the search in the database, and then displays the results in a list. That command will have to go through several steps, such as:

* get the text currently in the search field
* build an SQL query
* connect to the database
* perform the query
* check the results
* build a list with the results
* display the list to the user

In order to make your programme more responsive to the user, you can check at *every* step of the way whether the current text in the field has changed - ie., whether the user has continued typing, and has not waited for a response. So, the algorithm becomes:

* get the text currently in the search field
 - check that the text in the search field has not changed
* build an sql query
 - check that the text in the search field has not changed
* connect to the database
 - check that the text in the search field has not changed
... and so on

As you can see, the trick is to give a chance for the user to update the field, before you complete the task - and check along the way. Now, if you just put all the steps into a single handler, then it becomes more difficult both to 'pace' the handler - and give the user a chance to update the field - as well as to stop it halfway along, if you need to. 

Other more experienced users may have a smarter approach, but the way that I'd go about it would be to break the task into several small 'chained' handlers, that call each other in sequence, checking whether the current text in the field has changed - like this:

1) declare some local script variables outside the handler, to keep track of text changes and database results:

local sOriginalText, sQuery, sDBID, sDBResult

2) split each step in our algorithm into a distinct handler, that checks the text in the field and then calls the next one in the sequence, if the current text in the field has not changed - for instance:

on quickSearchStart
  put field "quickSearchField" into sOriginalText
  send "quickSearchBuildSQL" to me in 1 tick
end quickSearchStart

on quickSearchBuildSQL
  -- we only proceed if the text has not changed:
  if the text of field "quickSearchField" is not sOriginalText then exit quickSearchBuildSQL
  -- build the query:
  put "SELECT * FROM clients WHERE name LIKE " & "%" & sOriginalText & "%" into sQuery
  -- go to the next step:
  send "quickSearchDBConnect" to me in 1 tick
end quickSearchBuildSQL

on quickSearchDBConnect
  -- we only proceed if the text has not changed:
  if the text of field "quickSearchField" is not sOriginalText then exit quickSearchDBConnect
  -- connect to the database:
  ... 
end quickSearchDBConnect

...and so on.

Last of all, you need to call the very first handler - our 'quickSearchStart' - whenever the text in the quickSearchField changes. Right now you seem to be doing that on a 'rawKeyUp' message, but LiveCode 5.5 has a new field message, specially for this situation: the 'textChanged' message. So, in the field script you can start the quick search with:

on textChanged
 if me is not empty then quickSearchStart
end textChanged

As I said, other more experienced users may have different, smarter solutions, but from an algorithm perspective, this should work.

Let us know how you go! :-)

--
Igor Couto
Sydney, Australia





More information about the use-livecode mailing list