Using the Find Functions for HTML Database Forms - The ordinarySearch() function
(Page 2 of 4 )
When the recordset is not sorted, this function does the searching. It takes as arguments the cell value whose row is searched, and the index of the column having the cell. This cell value is called the key. If it sees the value, it returns the row index. If it doesn't see the value, it returns the total number of rows in the recordset; note that this is one unit higher than the maximum row index. It is up to the calling function to interpret the result. This is the function:
function ordinarySearch(key, titleInx)
{
for (i=0; i<numberOfRows; ++i)
{
if (document.getElementById('Recordset').rows[i].cells[titleInx].innerHTML == key)
return i;
}
return numberOfRows;
}
The binarySearch() function
This function is called when the recordset is sorted. When the recordset is sorted, the search from this function is very fast, much faster than that of the ordinary search. The function takes as arguments the same parameters that the ordinary search takes. It returns the same values under the same conditions. If the user wants to be sure to have a fast search, he can sort the recordset based on the column, which has the cell value he will find. After that he finds the cell value (row) by clicking the Find button. This is the binarySearch() function:
function binarySearch(key, titleInx)
{
var left = 0;
var right = numberOfRows - 1;
while (left <= right)
{
var mid = parseInt((left + right)/2);
if (document.getElementById('Recordset').rows[mid].cells[titleInx].innerHTML == key)
return mid;
else if (document.getElementById('Recordset').rows[mid].cells[titleInx].innerHTML < key)
left = mid + 1;
else
right = mid - 1;
}
//when element is not found
return numberOfRows;
}
If you want to know how the function operates, consult the article I wrote. The recordset is like a two dimensional array. The binarySearch() function here is slightly different from the one I wrote in the article. In that article, searching is done only for a one-dimensional array. Here the function is different, because the two dimensional array aspects have to be taken into consideration.
Next: The getTwoWords() Function >>
More HTML Articles
More By Chrysanthus Forcha