www.webdeveloper.com
Results 1 to 5 of 5

Thread: Limit to number of Else Ifs? Too Much Recursion

  1. #1
    Join Date
    Jul 2009
    Posts
    2

    Limit to number of Else Ifs? Too Much Recursion

    Hey, I am really inexperienced with javascript and am coming up with this error that I do not know how to fix.

    Here is what I'm trying to do. I have a database table full of a couple thousand people with unique ids and on a web page form people have the ability to edit one person at a time. To do so, I want them to enter in the persons id# to identify which person it is since this avoids the problems of incorrect spelling of the persons name, people that have the same name, ect.

    What I wanted to do with javascript is check to see what number they have entered in the input text area then check to see if it matches a record in the database, if so output the name so they know they have the correct id or tell them the id is not valid.

    Since I'm inexperienced with javascript and know of no way to have the javascript query my database the only way I knew how to do this is to dynamically create a javascript function with the server side language I'm using (PHP) that contains a long if-else if-else if...and so on for every unique id. However, when I try to run this it doesn't work and firebug gives the error that there is too much recursion and the function is no longer recognized. I had the script only return the first few results and it worked so I was puzzled. I kept upping the number of unique ids it would show and therefore the number of else if statements and I got it to work for up to 1,535 of them and it runs fast even when entering id 1535. But, for 1536 it doesn't work.

    Looking at the else if statement generated by id 1536 there is nothing special about it. It is valid like all the rest, and its body doesn't call another function, it just returns the name.

    I know there is probably a better way to do this, but I find this to be a peculiar error and was hoping someone could give me some insights.

    Thanks!

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    What if you just used ajax to pass the ID to your php script that looks up the user. Then php can call the SQL and return the name if a name is returned or return false if they don't exist. You wouldn't need a bunch of if-then-else statements at the point and you could let the SQL do the work like its built to do.

  3. #3
    Join Date
    Jul 2009
    Posts
    2
    Yes, I figured ajax would be able to do it by querying the database but I have never used ajax and don't know how.

    This is probably a good time to learn it I guess, but I still am curious about the error.

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    I can't help you with the error without seeing your code. I'm having trouble understanding why you have so many if-then-else statements in the first place. If you only wanted to load the db into javascript when the page loads initially you should use an array and just check if the index exists and thus you would again only have a singe if-then-else block to work with.

  5. #5
    Join Date
    Jul 2009
    Posts
    62
    i am a newbie when it comes to JavaScript, but if you have more then three conditions switch is a better option... i dont know about its limits thou but you could test it out... here is how it would look:
    Code:
    	
    
                 switch  (mode)  {                              //mode been the variable
    				
    			case "height":                       //if the variable mode == height
    
    				document.write.('height');  //code to execute if height
    	
    					 break;            //break required at end of each case
    			 
    			case "width":
    
    	                         document.write.('width');   
      
    					 break;
    			 
    			case "both":
    
    			          document.write.('both');
    
    			 		break;
    
                             default:                                     //if the variable is not equal to any of the options
                                      document.write.('no match');
                                       
    	}

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