PDA

View Full Version : Search Function Code in HTML


Atlas Shrugged
10th Apr 2007, 05:55
I am in the process of designing an application which will be run in IE or Firefox. It will be entirely offline and not on the web.

Part of the application needs to include a page with a search function to search a database of about 6000 keywords. I have the input box and the database completed but I cant seem to find the code to tie it all together.

I've searched various online manuals & tutorials but the closest I can find is the Google Search function which doesn't really suit my needs.

All I'm after is the code for a simple search function which will look up the word in list and drop the results on the same page

I want to try and make it work something like this:

Enter Keyword: xxxxx

Press "Search"

Search Results:
1
2
3

The user then clicks on the selection it loads the page with the rest of the details on it.

Can anyone point me in the right direction to find such code?

AS

robdesbois
10th Apr 2007, 07:59
Clues on database type and server-side language?

Atlas Shrugged
10th Apr 2007, 11:12
Sorry, I dont know what "server side language" is. :ugh: Its written in javascript (very basic) and the database is more of a list of about 6000 entries on a single page.

What I'm trying to make it do is essentially find the word and then create a link to another page where the rest of the information is, eg:

Database:

Coffee
Milk
Sugar
Tea
Water
Coffee with sugar
etc

Search for, say Sugar and the results would be
1. Sugar
2. Coffee with sugar

which I can then link to the appropriate page.

Hope that makes sense, I'm fairly new to this sort of thing

AS

robdesbois
10th Apr 2007, 11:14
Ok when you say 'database' what are you referring to? How is this database stored?

Atlas Shrugged
10th Apr 2007, 11:34
Its just a list stored in the body of a webpage or html document. An example from the page is:

<FORM NAME="database">
<INPUT TYPE="hidden" NAME="list" VALUE='xxx

<title> Plant Nurseries <keys> Bulb propagating, Fruit tree nursery operation, Ornamental plant growing, Plant stock growing n.e.c., Seedling growing, Turf growing, Vine stock nursery operation. </p> <content></p><table noborder cellspacing=4 cellpadding=0 width=600><tr><td width="15%" valign="top"><b>Division:</b></td><td width="87%" valign="top"> Agriculture, Forestry & Fishing ( A ) </td></tr><td width="15%" valign="top" height=10><b>Subdivision:</b></td><td width="87%" valign="top" height=10> Agriculture ( 01 ) </td></tr><td width="15%" valign="top"><b>Group:</b></td><td width="87%" valign="top"> Horticulture and Fruit Growing ( 011 ) </td></tr><td width="15%" valign="top"><b>Class:</b></td><td width="87%" valign="top"> <b> Plant Nurseries ( 0111 ) </tr></table> </p></li><li>Description: This class consists of units mainly engaged in propagating and growing ornamental plants, plants for transplanting into gardens (such as seedlings or young fruit trees) or bulbs. This class also includes units mainly engaged in growing turf. </p></li><li>Exclusions and References: Units mainly engaged in (a) growing flowers for seed collection are included in Class 0112 Cut Flower and Flower Seed Growing; and (b) growing forest nursery stock are included in Class 0303 Services to Forestry. </p> <url>APC
<title> Cut Flower and Flower Seed Growing <keys> Display foliage growing, Flower growing, Seeds, flower, growing </p> <content></p><table noborder cellspacing=4 cellpadding=0 width=600><tr><td width="15%" valign="top"><b>Division:</b></td><td width="87%" valign="top"> Agriculture, Forestry & Fishing ( A ) </td></tr><td width="15%" valign="top" height=10><b>Subdivision:</b></td><td width="87%" valign="top" height=10> Agriculture ( 01 ) </td></tr><td width="15%" valign="top"><b>Group:</b></td><td width="87%" valign="top"> Horticulture and Fruit Growing ( 011 ) </td></tr><td width="15%" valign="top"><b>Class:</b></td><td width="87%" valign="top"> <b> Cut Flower and Flower Seed Growing ( 0112 ) </tr></table> </p></li><li>Description: This class consists of units mainly engaged in growing flowers or foliage for cutting for display or growing flowers for seed collection. </p></li><li>Exclusions and References: Units mainly engaged in growing seedlings of ornamental plants (including flowers) for sale are included in Class 0111 Plant Nurseries. </p> <url>APC
<title> Vegetable Growing <keys> Beans growing (except dry field beans or soybeans), Garlic growing, Herbs growing n.e.c., Kumara growing, Melons growing, Onions growing, Potato growing, Seed potato growing, Sugar beet growing, Sweet potato (kumara) growing, Tomato growing, Vegetables growing (except dry field peas or beans or soybeans), Vegetable seed growing </p> <content></p><table noborder cellspacing=4 cellpadding=0 width=600><tr><td width="15%" valign="top"><b>Division:</b></td><td width="87%" valign="top"> Agriculture, Forestry & Fishing ( A ) </td></tr><td width="15%" valign="top" height=10><b>Subdivision:</b></td><td width="87%" valign="top" height=10> Agriculture ( 01 ) </td></tr><td width="15%" valign="top"><b>Group:</b></td><td width="87%" valign="top"> Horticulture and Fruit Growing ( 011 ) </td></tr><td width="15%" valign="top"><b>Class:</b></td><td width="87%" valign="top">

I want it to search the keys and return the "title" ie search for "bulb" and it will display "Plant Nurseries"

robdesbois
10th Apr 2007, 11:46
Ok, to achieve this as you've started it I'd recommend some sort of javascript library (first guess http://www.jquery.com).

I would recommend that you don't do it this way though - 6000 entries is a lot. Is this running on a web server or just a file you open off your computer?

Atlas Shrugged
10th Apr 2007, 11:56
Just from a file opend off the computer.

Thanks for the link - I'll have look in there

:ok:

Atlas Shrugged
12th Apr 2007, 03:31
Me again! Hope you dont mind one more question.

I managed to piece together something that works the way I want it to but now I can't get the input box to appear on the page. I can put one in there but I can seem to get it to work or start the search.

Any thoughts???

Here's a copy of the code for the page. In the example the word to search for is "plant"

<HTML><HEAD><TITLE>Search Page</TITLE></HEAD><STYLE TYPE=text/css></STYLE></HEAD><FORM NAME="database"><INPUT TYPE="hidden" NAME="list" VALUE='ABC

<title><A href="PAGE LINK GOES HERE.htm">Plant Nurseries</A><keys>Bulb propagating, Fruit tree nursery operation, Ornamental plant growing, Plant stock growing n.e.c., Seedling growing, Turf growing, Vine stock nursery operation.</p><content><url>ABC

<title>Result 2<keys> Plant growing, Plant stock growing <p><content><url>ABC

<title>Result 3<keys>Plant growing</p><content><url>ABC

'>
</FORM>

<script language="JavaScript">
<!--
function Page(url,title,keywords,description) {
while ((url.length > 0) && (url.charAt(0) == " ")) {
url = url.substring(1,url.length);
}
this.url = url;
while ((title.length > 0) && (title.charAt(0) == " ")) {
title = title.substring(1,title.length);
}
this.title = title;
this.keywords = keywords;
this.description = description;
return this;
}
function Database() {
var pos = 0;
//*htm link~Page Title|Title^description*
//<url>htm link<title>Page Title<keys>Title<content>description
while ((pos1 = amorphous.indexOf("<title>",pos)) != -1) {
pos2 = amorphous.indexOf("<keys>",pos1+1);
pos3 = amorphous.indexOf("<content>",pos1+1);
pos4 = amorphous.indexOf("<url>",pos3+1);

if ((pos2 != -1)
&& (pos2 < pos3) && (pos3 < pos4)
&& (pos4 <= amorphous.indexOf("<url>",pos))) {
this[database_length++] = new Page(amorphous.substring(pos,pos1),
amorphous.substring(pos1+7,pos2),
amorphous.substring(pos2+6,pos3),
amorphous.substring(pos3+9,pos4));
pos = pos4+1;
} else { // error reading amorphous database
if (pos+30 <= amorphous.length)
alert('Error reading in amorphous database around "'
+ amorphous.substring(pos,pos+30) + '"');
pos = amorphous.indexOf("<url>",pos) + 5;
}
}
return this;
}
function search(str) {
menu_length = 0;
temp = new Object();
temp_length = 0;
words_length = 0;
words = new Object();
pos = 0;
while ((pos = str.indexOf(" ")) != -1
&& and_search != "exact") {
words[words_length] = str.substring(0,pos);
if (words[words_length].length > 0)
words_length++;
if (str.length == 1)
str="";
else
str = str.substring(pos+1,str.length);
}
if (str.length > 0)
words[words_length++] = str;
for (q=0;q<words_length;q++) {
temp_length = 0;
str = words[q].toLowerCase();
len = (and_search=="and"&&q>0?menu_length:database_length);
for (n=0; n<len; n++) {
if (and_search=="and"&&q>0) {
combo = (menu[n].title + " " + menu[n].description
+ " " + menu[n].keywords).toLowerCase();
} else {
combo = (database[n].title + " " + database[n].description
+ " " + database[n].keywords).toLowerCase();
}
if (combo.indexOf(str) != -1) // found
temp[temp_length++] = (and_search=="and"&&q>0?menu[n]:database[n]);
}
if (and_search!="and" && q>0) {
added = 0;
for (i=0;i<temp_length;i++) {
duplicate = false;
for (j=0;j<menu_length&&!duplicate;j++) {
if (menu[j] == temp[i]) {
duplicate = true;
}
}
if (!duplicate)
menu[menu_length+(added++)] = temp[i];
}
menu_length += added;
} else {
for(h=0;h<temp_length;h++)
menu[h] = temp[h];
menu_length = temp_length;
}
}
}
function entry() {
if ((document.entryform.keyword.value.length == 0)
|| (document.entryform.keyword.value == " ")) {
alert("First you must enter a keyword to search for.");
return false;
}
and_search = (document.entryform.and_or.selectedIndex == 0?"and":"or");
if (document.entryform.and_or.selectedIndex == 2)
and_search = "exact";
location.href = location.pathname + "?"
+ escape(document.entryform.keyword.value)
+ (and_search != "or"?"&"+and_search:"");
return false;
}
function redWord(str) {
for(r=0; r<words_length; r++) {
pos = -3;
word = words[r].toLowerCase();
while ((pos = str.toLowerCase().indexOf(word,pos+3)) != -1) {
val = pos+word.length;
str = str.substring(0,pos) + "*"
+ str.substring(pos,val) + "|"
+ str.substring(val,str.length);
}
}
pos = -16;
while ((pos = str.toLowerCase().indexOf("*",pos+16)) != -1)
str = str.substring(0,pos) + "<font color=red>"
+ str.substring(pos+1,str.length);
pos = -7;
while ((pos = str.toLowerCase().indexOf("|",pos+7)) != -1)
str = str.substring(0,pos) + "</font>"
+ str.substring(pos+1,str.length);
return str;
}

var amorphous = document.database.list.value;
temp_str = amorphous.substring(amorphous.length-2,amorphous.length);
if (temp_str.indexOf("<url>") == -1)
amorphous += "<url>";
else
amorphous += " "; // amorphous database must have characters after last asterisk

database_length = 0; // Netscape 2 fix
var database = new Database(); // read in from amorphous database

menu_length = 0; // Netscape 2 fix
var menu = new Object();

string = "";
and_search = "or";
if (location.search.length > 1) {
string = unescape(location.search.substring(1,location.search.length) );
pos = 0;
while ((pos = string.indexOf('"',pos)) != -1) {
string = string.substring(0,pos) + '\\"' + string.substring(pos+1,string.length);
pos += 2;
}
if (string.substring(string.length-4,string.length) == "&and") {
string = string.substring(0,string.length-4);
and_search = "and";
} else if (string.substring(string.length-6,string.length) == "&exact") {
string = string.substring(0,string.length-6);
and_search = "exact";
} else if (string.substring(string.length-3,string.length) == "&or") {
string = string.substring(0,string.length-3);
and_search = "or";
}
search(string);
}

if (location.search.length > 1)
document.write();

// eliminate the keywords portion of the statement below to eliminate them from your display

for (n=0; n<menu_length; n++)
document.write('<b>&nbsp&nbsp'+menu[n].title
+' </b><ul>'+redWord(menu[n].description)+'</li></ul><br>\n');
if ((menu_length == 0) && (location.search.length > 1))
document.write('Primary Activity "'+string+'" not found!\n');
// -->
</script>
<hr noshade>
<p>

<!--Content to appear after the search results goes here-->

</p>
</body>
</html>

robdesbois
12th Apr 2007, 08:39
AS,

Do you mean the input called 'list'?

<INPUT TYPE="hidden" NAME="list" VALUE='ABC

It's not visible because the type is 'hidden' instead of 'text'.

I don't know whether this is a concern but your HTML is extremely non-standard. You end the head twice (</HEAD>), your form tag doesn't have an action or method (forms are used for user input, there's no need to have it just for the information content). Important here is your input and its value attribute are never closed! You need to add a quote after VALUE='ABC to close the attribute, then /> after that to close the input field.

Also, there are plenty of non-existent (or mis-used) tags in your information section: it appears as though you're using XML-type syntax for storing a database within an HTML page....

Anyway, try making the input field text not hidden, and close it properly and see how that works :)