Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts

    strict search an array.

    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.


    Code:
    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!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This what you want?

    Code:
    <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>

  • #3
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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...

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <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>

  • #5
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    yep. there are.

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

  • #8
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    hmm. the link doesnt work. copy/paste the url into your address bar

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Posts
    624
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Have you seen this thread?

    Finding duplicate numbers in an array?

  • #10
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    this could make for a good Code Challenge!

  • #11
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok. i looked at that and took a try at butchering that too! heres what i came up with:
    Code:
    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

  • #12
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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++)

  • #13
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thats what i thought too. but a msc told me otherwise! http://www.webxpertz.net/forums/show...threadid=22508

  • #14
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    i uploaded the page to what i have from the previous post.

  • #15
    Regular Coder
    Join Date
    Dec 2002
    Location
    a bit west of pittsburgh
    Posts
    236
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •