www.webdeveloper.com
Results 1 to 12 of 12

Thread: searching arrays for mutiple conditions?

  1. #1
    Join Date
    Apr 2009
    Posts
    44

    searching arrays for mutiple conditions?

    Hiya. I'm got an application that uses (will use) 6 drop down boxes to filter through items stored in an array, if I can get a search system sorted out.

    At the minute i've tried a range of ways and I can't suss this out. At the minute its counting the number of entries that match the filter criteria but its not even getting that 100% right.

    I've tried using one big if statment 'if condition 1 && condition 2 && condition3 = true then' etc. I've tried nested ifs.

    At the moment I thought I'd try to dump the values of servos into a new array, that meet the criteia of the first if, then using another if, create another array and repeat this until only the servos that meet all 6 if conditions are stored inside one array that i can then set to display on screen. if there are no matches then i can display a 'none found' message.

    Heres my code, hopefully its commented enough to follow whats happening, please help if you can, I'm new to the language so if its something daft then please forgive me. My end goal would be something similar to this
    Attached Files Attached Files
    Last edited by lil_bugga; 10-06-2011 at 08:09 PM. Reason: attaching file as code is too long to post

  2. #2
    Join Date
    Apr 2009
    Posts
    44
    ok with further testing and a multitude of alret boxes I've come to the conclusion that my code is correct, pretty much.

    I'm still not 100% why but its finding the right servo's and creating an array with them. If I select air it finds 30 matches, if i select heli it finds 7 matches and if i select surface it finds 12 matches which is just what I want , however when I run a count on the number of entries inside the array, it gives me a count of 30 for air, 37 for heli and then 49 for surface.

    Why is it that my new array is still adding the results of non matches rather than skipping over them and only holding, say the 7 servos that would come up if searching for heli.

    PHP Code:
    var matches = Array();
            for(var 
    i=0i<servos.lengthi++){
                if (
    servos[i].application === document.searchFilters.applicationMenu.value) {                
                    
    matches[i] = " modelno " servos[i].modelno " name " servos[i].name " speed " servos[i].speed " v3_3 " servos[i].v3_3 " v4_8 " servos[i].v4_8 " v6_0 " servos[i].v6_0 " v7_2 " servos[i].v7_2 " weight " servos[i].weight " dimensions " servos[i].dimensions " opvoltage " servos[i].opvoltage " image " servos[i].image " description " servos[i].description "</br></br>"
                    
    alert(matches);
                } else {
                    
    i++;
                } 

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    You have to count only the matches servos
    Code:
    var matches = Array();
            for(var i=0; i<servos.length; i++)
                if (servos[i].application === document.searchFilters.applicationMenu.value) {                
                    matches[matches.length] = " modelno " + servos[i].modelno + " name " + servos[i].name + " speed " + servos[i].speed + " v3_3 " + servos[i].v3_3 + " v4_8 " + servos[i].v4_8 + " v6_0 " + servos[i].v6_0 + " v7_2 " + servos[i].v7_2 + " weight " + servos[i].weight + " dimensions " + servos[i].dimensions + " opvoltage " + servos[i].opvoltage + " image " + servos[i].image + " description " + servos[i].description + "</br></br>";
                }
            alert(matches);

  4. #4
    Join Date
    Apr 2009
    Posts
    44
    Cheers 007Julien, I realised after a while that when I was incrimenting 'i' I was also incrimenting matches as I had matches[i].

    Having got that working I wanted to adapt the array to allow searchability like the orignal array had but I cant get it right. As I said above my aim is to create multiple arrays contained within if statments to allow me to filter my searchs.

    I'd like to be able to use search[i].type as a secondary filter, but to do that I need to be able to access the information within the newly created array in the search[i].* format, but everything I've tried tells me the serch[i].* is undefined

    Again I'll attach my code as the forum says its too long to post, maybe you can see where I'm getting it wrong
    Attached Files Attached Files

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by lil_bugga View Post
    I'd like to be able to use search[i].type
    You have no array/object called search... So?

  6. #6
    Join Date
    Apr 2009
    Posts
    44
    nope, your correct kor, where I said search[i] i meant to put matches[i]

    PHP Code:
    function dataPull(){
    //create servoStore to build page for display
    //var servoStore="";
                
    // search and get servos meeting application filter settings
            
    var matches = Array();
            for(var 
    i=0i<servos.lengthi++){
                if (
    servos[i].application == document.searchFilters.applicationMenu.value) {                
                    
    matches[i] = " modelno " servos[i].modelno " name " servos[i].name " speed " servos[i].speed " v3_3 " servos[i].v3_3 " v4_8 " servos[i].v4_8 " v6_0 " servos[i].v6_0 " v7_2 " servos[i].v7_2 " weight " servos[i].weight " dimensions " servos[i].dimensions " opvoltage " servos[i].opvoltage " image " servos[i].image " description " servos[i].description "</br></br>"
                }
    // the following line are the conditions that need to be used to filter the servos against the values of the dropdowns.        
    //if servos[i].application / servos[i].sclass / servos[i].type / servos[i].motor / servos[i].bearings / servos[i].gears 
    // if someone were to set the dropdown boxes to air/sub micro/digital/corelss/bushed/nylon or 1/1/2/1/2/1 it should return 3 items SPMDSP60 / SPMDSP75 / SPMDSP60J

            //document.getElementById('servoDisplay').innerHTML = servoStore;
            
    document.getElementById('servoDisplay').innerHTML matches.length;

            }
    }; 

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    As 007Julien stated: use matches[matches.length] instead of matches[i]

    I don't understand which is your aim, really...

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    I have made a minor change with sclass instead of class (always and in sclassMenu)... There is too problems with "-" values ?
    Code:
    function setup () {
    //...
    // loop through and display all servos on start up, ready for filtering. 
    for(var i=0; i<servos.length; i++) servoStore += " modelno " + servos[i].modelno + " name " + servos[i].name + " speed " + servos[i].speed + " v3_3 " + servos[i].v3_3 + " v4_8 " + servos[i].v4_8 + " v6_0 " + servos[i].v6_0 + " v7_2 " + servos[i].v7_2 + " weight " + servos[i].weight + " dimensions " + servos[i].dimensions + " opvoltage " + servos[i].opvoltage + " image " + servos[i].image + " description " + servos[i].description + "</br></br>";
    	document.getElementById('servoDisplay').innerHTML = servoStore;
    };
    
    function dataPull(){var i,j;
    	var servoStore="";
    	var criteres=new Array("application", "sclass", "type", "motor", "bearings", "gears");
    	var conditions=[];
    	var frm=document.searchFilters;
    	for (i=0;i<criteres.length;i++) {conditions[i]=frm[criteres[i]+"Menu"].value;}
    	for(i=0; i<servos.length; i++){selectedItem=true;
    		for (j=0;j<conditions.length;j++) {// alert(i+" "+conditions[j]+" "+servos[i][criteres[j]])
    			selectedItem = selectedItem && (!conditions[j]) || (servos[i][criteres[j]]==conditions[j]); 
    			if (selectedItem) 				
    				servoStore += " modelno " + servos[i].modelno + " name " + servos[i].name + " speed " + servos[i].speed + " v3_3 " + servos[i].v3_3 + " v4_8 " + servos[i].v4_8 + " v6_0 " + servos[i].v6_0 + " v7_2 " + servos[i].v7_2 + " weight " + servos[i].weight + " dimensions " + servos[i].dimensions + " opvoltage " + servos[i].opvoltage + " image " + servos[i].image + " description " + servos[i].description + "</br></br>"; 
    		}
    	}	
    	document.getElementById('servoDisplay').innerHTML = servoStore;
    }

  9. #9
    Join Date
    Apr 2009
    Posts
    44
    007Julien I'm looking at your code and I'm a little lost with it. Could you explain it to me? Once I get my head around whats happening I'll be able to not only make sence of it but use it too.

  10. #10
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    You have only to take all values of selects in the array conditions (the first for loop), to test if each servos satisfy this six conditions.

    A servo (the second i loop) is to select for a criterion (the j loop with a boolean value selectedItem) if there is no conditions for this criterion (conditions[j] is zero and !conditions[j] is true) or if the conditions[j] is equal to is property (servos[i][criteres[j]] is servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor, servos[i].bearings or servos[i].gears for j=0, 1, 2.. or 5).

    This must be completed with the signification of "-"...

    EDIT : I have make an error sorry. The condition is :
    Code:
    selectedItem = selectedItem && ((!conditions[j]) || (servos[i][criteres[j]]==conditions[j]));
    I don't saw the this (onclick="datapull(this)". You wanted probably establish a list of selected servos (matches = Array()) and restrict this list with each select. Then you have to write

    Code:
    function datapull(currentSelect){
    // work with currentSelect.value;
    }
    That is not a good idea. You have to consider all select if the user enlarge the choice...
    With all this Elucubrations you will probably find the best solution. Sorry.
    Last edited by 007Julien; 10-07-2011 at 11:24 AM.

  11. #11
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    It's work with
    Code:
    selectedItem = selectedItem && (conditions[j]==0 || (servos[i][criteres[j]]==conditions[j]));

  12. #12
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    It is too important to select all default option with this line
    Code:
    <option value="0" selected="selected">Show All</option>
    Since, contrary to appearances, no option are selected !

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles