...

View Full Version : strict search an array.



joeframbach
02-22-2003, 08:06 PM
here is my code. but i guess i'll have to explain a few things.


pics is an array of URL strings. "http://blah.org/folder/image-happystuff.gif" for example.
<input type=text id='query'> contains the search string, seperated by spaces.
i want it to only return strings that match all query strings.




function doSearch()
{
var result = pics;
var query = document.getElementById("query").value.split(" ");
for(i in result) for(j in query)
if(result[i].split("-")[1].indexOf(query[j])==-1)
result = result.splice(i,1);
document.getElementById("res").innerHTML=result.length + " results were returned.<br>" + result.join("<br>");
}


but it wont return anything!

x_goose_x
02-23-2003, 04:34 AM
This what you want?



<html>
<head>
<script>
pics = new Array("http://blah.org/one.gif","http://blah.org/two.gif","http://blah.org/three.gif","http://blah.org/four.gif","http://blah.org/five.gif");
function doSearch() {
var result = pics;
var query = document.getElementById("query").value.split(" ");
var matches = new Array();
for (i in result) {
for (j in query) {
if (result[i]==query[j]) {
matches[matches.length] = result[i];
}
}
}
document.getElementById("res").innerHTML = matches.length + " results were returned.<br>" + matches.join("<br>");
}
</script>
</head>
<body>
<form>
<input type="text" id="query" style="width: 400px;" value="http://blah.org/two.gif http://blah.org/two.jpg http://blah.org/four.gif"><br>
<input type="button" value="GO" onclick="doSearch();">
</form>
<div id="res"></div>
</body>
</html>

joeframbach
02-23-2003, 05:31 AM
if i type "a a" in the textbox, then it would match all of them twice. i do not want taht, so follow my logic. i want to exclude the ones that dont match, thus removing doubles...

x_goose_x
02-23-2003, 05:47 AM
<html>
<head>
<script>
pics = new Array("http://blah.org/one.gif","http://blah.org/two.gif","http://blah.org/three.gif","http://blah.org/four.gif","http://blah.org/five.gif");
function doSearch() {
var result = pics;
var query = document.getElementById("query").value;
var matches = new Array();
for (i in result) {
if (query.indexOf(result[i])!=-1) {
matches[matches.length] = result[i];
}
}
document.getElementById("res").innerHTML = matches.length + " results were returned.<br>" + matches.join("<br>");
}
</script>
</head>
<body>
<form>
<input type="text" id="query" style="width: 400px;" value="http://blah.org/two.gif http://blah.org/two.jpg http://blah.org/four.gif"><br>
<input type="button" value="GO" onclick="doSearch();">
</form>
<div id="res"></div>
</body>
</html>

joeframbach
02-23-2003, 05:54 AM
if they put "joe f" it'll return something like "joe frambach", but not "joeframbach" or "frambach, joe" , which , if i could get what i originall had to work, would be correct

x_goose_x
02-23-2003, 06:03 AM
On the line:


if(result[i].split("-")[1].indexOf(query[j])==-1)

why are you splitting it with the hyphen? There are none in your array.

joeframbach
02-23-2003, 06:08 AM
yep. there are.

heres the array. http://www.geocities.com/bobiscrazy05/buddyURLs.js

joeframbach
02-23-2003, 06:11 AM
hmm. the link doesnt work. copy/paste the url into your address bar

Graeme Hackston
02-23-2003, 06:12 AM
Have you seen this thread?

http://www.codingforums.com/showthread.php?s=&threadid=4599

joeframbach
02-23-2003, 06:20 AM
this could make for a good Code Challenge! :rolleyes:

joeframbach
02-23-2003, 06:33 AM
ok. i looked at that and took a try at butchering that too! heres what i came up with:


function doSearch()
{
var result = new Array()
var query = document.getElementById("query").value.split(" ");
for(i=0;i<pics.length;i++)
for(j=0;j<query.length;j++)
if(pics[i].split("-")[1].indexOf(query[j])>=0)
result[result.length] = pics[i];
for(i=0;i<result.length;i++)
for (var key in result)
if (result[key] == result[i])
{
result.splice(/\D/.test(key) ? key : Number(key));
break;
}
document.getElementById("res").innerHTML=result.length + " results were returned.<br>" + result.join("<br>");
}


it works! w00h00! but it searches for any words in the query rather than all words in the query

x_goose_x
02-23-2003, 06:41 AM
To help clean up your code a bit, it's good practise to use:

for(i in results)

instead of:

for(i=0;i<result.length;i++)

joeframbach
02-23-2003, 06:58 AM
thats what i thought too. but a msc told me otherwise! http://www.webxpertz.net/forums/showthread.php3?s=&threadid=22508

joeframbach
02-23-2003, 07:25 AM
i uploaded the page to what i have from the previous post.

joeframbach
02-23-2003, 08:21 AM
joh6nn helped me realize that what i was doing was completely wrong (thank you) and i redid how the pictures are shown, to avoid legal issues

joeframbach
02-23-2003, 08:48 AM
i'm just poking around with different things... here's something better.

i also realized that i never posted a link to the site...
www.geocities.com/bobiscrazy05/buddyicons.html

x_goose_x
02-23-2003, 04:50 PM
it jumps around like crazy with the onmouseovers. You may want to set the height of the div that hold the image.

joeframbach
02-23-2003, 08:28 PM
another rewrite:rolleyes: .... i have no life

x_goose_x
02-23-2003, 08:37 PM
youm might also want to preload your images. Make it look a little smoother. And when you make a new search the last image that was onmouseover'd is still there. You might want to have it load the first image on the list or have no image at all. Just my input tho.

joeframbach
02-23-2003, 09:02 PM
i asked bob(the person i'm writing this for) for his input. he asked if i could preload the images from anywhere in the site. i was thinking a frame on the index page with a height of 0 that contains all the pics in a 1x1 image tag. that would preload it into the temp folder, right?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum