SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 39 of 39
  1. #26
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry felgall I can't see which { matches which } even though I wondered why people staggerd their script I don't see how it points out which bracket matches another. I got to call it a day. Hopefully I'll get it tomorrow. Thanks again for everyones help. On the bounce.

  2. #27
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @ScallioXTX - thanks for the indexOf trick. Totally forgot about that and went a longer PHP route.

  3. #28
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Use Id as Table name in query

    Quote Originally Posted by ScallioXTX View Post
    Should work fine. Maybe IE8 wants a space between else and the curly brace?

    Also, please beware that your code will be executed as

    Code:
    if () { 
    
    }
    if () {
    
    }
    else {
    
    }

    i.e., the red if is completely separate from the blue if and else (so if the red if matches, the blue if will not match, but the blue else will!).

    If you want either A, or B, or otherwise ... you should use

    Code:
    if () {
      // do something
    } else if {
      // do something else
    } else {
      // none of the above matched
    }
    ScallioXTX
    Thanks for that and the indexof syntax. The function in the second code div syntax is what I'm looking for. IF, ELSE IF, ELSE. felgall said that there is a mismatch of the { } brackets in the way I wrote the function and reminded me why the script Gods stagger their Functions so that they can figure out which match each other. I myself never use that method and align all my text to the left so as to avoid scrolling to see the whole function.
    centered affect actually got my original question answered. How would I pass the Id of an element clicked as the Table name in a sql query. Turns out "SELECT/*FROM_".$q) was correct and I swear I searched the web for two weeks to find that tastey tid bit and never found it. Thank you Swami.

    Proposed Function
    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++){   
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
    sidebarli[i].onclick=function(){   
    alert(this.id+': Is sending your request.');
    var p=this.id; // centered effect @sitepoint forums javascript
    getproduct(p);
    }
    else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
    sidebarli[i].onclick=function(){   
    var p=this.id;
    f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
    }
    else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='search'){
    alert(this.id+': Requiers 5 digit input.');   
    }
    So there is a mismatch of the curly brackets { } that I can't see. IE8 doesn't requier a space between the if_{ or other statements as far as I know. I try to write all of my functions without additional white space. As far as I can see my function copies the syntax of ScallioXTX previous post. It maybe a Jscript syntax thing?

  4. #29
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Use Id as Table name in query

    ScallioXTX
    I guess I'm asking for the full syntax of functions within IF, ELSE IF and ELSE
    SOMETHING LIKE THIS
    Code:
    if () {
      // do something
    } else if (//brackets not excepted?) { //throws syntax error
      // do something else
    } else (//brackets not excepted?) {
      // none of the above matched
    }
    Function
    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++){   
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
    sidebarli[i].onclick=function(){   
    alert(this.id+': Is sending your request.');
    var p=this.id; // centered effect @sitepoint forums javascript
    getproduct(p);
    }
    else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){//syntax error
    sidebarli[i].onclick=function(){   
    var p=this.id;
    f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
    }
    else(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='search'){
    alert(this.id+': Requiers 5 digit input.');   
    }

  5. #30
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,036
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Eep, I forgot the brackets in the else if.

    Suppose we want to do something based on the value of a variable a, the full syntax is
    Code:
    if (a==2) {
       // do something with a
    } else if (a==3) {
      // do something else with a
    } else {
      // a is not 2 and it's also not 3
      // do yet something else with a here
    }
    But as I pointed out in my earlier post you only need one if/else pair, not if/else if/else; either an item has the class "disabled" (in which case you don't perform the AJAX call) or it does (in which case you do perform the AJAX call).

    In your if/else if/else you're checking for the classname of the parent, but that's completely irrelevant for your problem (think about it).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #31
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Use Id as Table name in query

    ScallioXTX
    As al the li elements in all the ul elements in #sidebar div may be asigned the classes 'item', 'item disabled' or 'item visited' the parentNode is important because there ar three types of httprequests being made and I am trying to accomodate all three request in one function
    1. uls 0 to 3 in sidebar div httprequest product table
    2. ul 4 in sidebar div httprequest writes object to f1 div
    3. ul 5 in sidebar div httprequest searches the whole database by id =code and returns the table that code number is found in


    It is not the indexof class that I am trying to apply. I need to assign any of the 3 class to all of the li elements in sidebar div and therefor can only exclude the anything but 'item' and the parentNode ul to apply a seperate httprequest. Thanks for your patience again.

  7. #32
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not do a switch statement based on post #29:

    Untested...
    HTML Code:
    var sidebarli = document.getElementById('sidebar').getElementsByTagName('li'),
    	i;   
    for(i = 0; i < sidebarli.length; i++) {   
    	if(sidebarli[i].className == 'item') {
    		switch( sidebarli[i].parentNode.className ) {
    			case 'prod' : 
    				sidebarli[i].onclick = function() {};
    			break;
    			case 'site' :
    				sidebarli[i].onclick = function() {};					
    			break;
    			case 'search' :
    				sidebarli[i].onclick = function() {};
    			break;
    		}		
    	}
    }

  8. #33
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use Id as Table name in query

    MSDN LIBRARYvar z = 3;
    Both ambigious and cryptic and though it claims to be EMCA complient Windows Script 3.* (Jscript) is still slightly different and has no 'else if' statement but this is the syntax they recomend for 'if..else'
    Code:
     
    if (x == 5) {
        if (y == 6)
            z = 17;
    }
    else
        z = 20;
    MY FUNCTION ENDEAVOUR JSCRIPT 3.*
    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++){   
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
    sidebarli[i].onclick=function(){   
    alert(this.id+': Is sending your request.');
    var p=this.id; // centered effect @sitepoint forums javascript
    getproduct(p);
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
    sidebarli[i].onclick=function(){   
    var p=this.id;
    f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
    }
    else{ // Throws syntax error here
    alert(this.id+': Requiers 5 digit input.');   
    }

  9. #34
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,036
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Let me try and break this down for you. This is what's happening now --

    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++) {   
      if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod') {   
        sidebarli[i].onclick=function() {    
          alert(this.id+': Is sending your request.');
          var p=this.id; // centered effect @sitepoint forums javascript
          getproduct(p);
          if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site') {
            sidebarli[i].onclick=function() {   
              var p=this.id; 
              f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
            }
            else{ // Throws syntax error here
              alert(this.id+': Requiers 5 digit input.');   
            }
    This is what should happen --

    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++) {   
      if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod') {   
        sidebarli[i].onclick=function() {    
          alert(this.id+': Is sending your request.');
          var p=this.id; // centered effect @sitepoint forums javascript
          getproduct(p);
          if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site') {
            sidebarli[i].onclick=function() {   
              var p=this.id; 
              f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>';
            }
          } else {
            alert(this.id+': Requiers 5 digit input.');
          }   
        }
      }
    }
    See the difference?

    Basically, if you type a { , you indent the next line (by a tab, or spaces, or whatever). Then if you want to close that block you go to the same position on further line and put a } there.
    It would be really helpful for you if you tried and understand this and implement it. It really makes coding a lot easier. In fact, your code with everything crammed to the left makes me a little bit dizzy...
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #35
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RESOLVED!!! Use Id as Table name in query

    Quick thanks to Every One. felgall SWAMI BAGWAN. Your post as usual was over CRYPTIC and maybe the teaching style of erra I personally shall not miss. It would have been a lot more efficient to say 'TRY CLOSSING YOUR FUNCTIONS IN THE IF OR ELSE STATEMENTS.' That was the issue and the following script works to prove it.
    Also ScallioXTX, as I mentioned previously to avoid scrolling to see the complete code I refuse to stagger the writing of functions. To see the script as it's final writing maybe imposible as it might well be one line by time it is crunched for production and all students would be better served viewing code line by line left aligned text as the staggering/indent of lines by braces did not point out to me the fact that several closing braces where not there. Once again that is the teaching style of an erra I personally shall not miss. It is far quicker to crunch your code when you learn to write it without extra white space and that method of instruction was primarly a visual aid which we have overcome with magnification ability witch exaserbates the stagering technique problem. People need to know how to write code exactly so that code needs the littlest amount of manipulation to get it running. I am a 'No Extra White Space' type of guy particularily when I am on my P'S and Q'S. At least every other month for an hour or two

    A SCRIPT THAT SURVES MY PURPOSE TO FOLLOW

  11. #36
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up RESOLVED!!! Use Id as Table name in query

    Thanks agin to SITEPOINT and all of the persons that contributed to this post.

    The SKRIPT THAT WORKS!
    Code:
    var sidebarli=document.getElementById('sidebar').getElementsByTagName('li');   
    for(i=0;i<sidebarli.length;i++){   
    if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='prod'){ 
    sidebarli[i].onclick=function(){   
    alert(this.id+': Is sending your request.');
    var p=this.id; 
    getproduct(p)}; //closed nested function with '};'
    }
    else if(sidebarli[i].className=='item' && sidebarli[i].parentNode.className=='site'){
    sidebarli[i].onclick=function(){   
    var p=this.id;
    f1.innerHTML='<object id='+p+'html class=htmlpage type=text/html data='+p+'.html></object>'}; 
     //closed nested function with '};'
    }
    else if(sidebarli[i].id=='searchlist' && sidebarli[i].parentNode.className=='bwi'){
    sidebarli[i].onclick=function(){
    alert(this.id+': Requiers 5 digit input.')}; //closed nested function with '};'
    }}
    Any further comments greatly appreciated.
    Hands up, all the people who agree there is 'To Much Extra 'White Space' in the House, Senate and Congress.'
    Children, please remember to close your functions properly, particularly if they are contained within any statements.

  12. #37
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah centered effect a switch statement would be the modern way of doing it and probably the correct way to change the 'onclick' method in the first two cases to 'onchange' for the last li element case which contains a text type input that querys the entire database. How to apply case3:if li element childNode 0 onchange is ' ' would be the trick. I'll work on something and postback. This post is getting to long? Will try latter with switch statement to alter event in third case.

    Best regards, Heinz Stapff

  13. #38
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Actually (and I hoping someone corrects me if I am wrong), that my first proposed method was the more modern way of doing this. As you can see the if/else if/else can get long, unreadable and unmaintainable - especially when you are not indenting your code (and adding spaces too!).

  14. #39
    SitePoint Addict
    Join Date
    Dec 2009
    Posts
    221
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RESOLVED!!! Use Id as Table name in query

    centered effect You are probably correct and the EMCA Script versions will hold the record of the chronology of switch/case and if/else syntax. Either way you can't knock the old school guys. They had to do what we try to do with 286 cpus. I was thinking of adding functionality with switch/case syntax like changing the logo background image and playing a sound when the li element was clicked but I've done that with if/else and lack of experiance with switch/case has stopped the endeavour with switch/case. I'll write it as soon as I get the chance. I guess we are all creatures of habit and are not comfortable stepping out of those habits. Might just be the deadline though mine was 9 years ago! The number one advantage of switch/case would be clarity of process in the function as written. I guess we choose to live in a more ambiguous world anyway.


Bookmarks

Posting Permissions

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