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.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to call a looping array instead of 5 individual pulls

    Greetings All,

    I've been advised to utilize an array system to acquire the data I am pulling from an XML document, but the only way I've currently gotten it to work is by calling 5 different instances of the same code. I would really appreciate help simplifying this code by calling a loop of 5 times for pulling posts and information on how to store it into an array so I can call back on it in my other code.

    Code:
    <script>
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","...=xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    
    document.getElementById("Body").innerHTML=
    xmlDoc.getElementsByTagName("Body")[0].childNodes[0].nodeValue;
    
    
    
    document.getElementById("Body1").innerHTML=
    xmlDoc.getElementsByTagName("Body")[1].childNodes[0].nodeValue;
    
    
    
    document.getElementById("Body2").innerHTML=
    xmlDoc.getElementsByTagName("Body")[2].childNodes[0].nodeValue;
    
    
    
    document.getElementById("Body3").innerHTML=
    xmlDoc.getElementsByTagName("Body")[3].childNodes[0].nodeValue;
    
    
    
    document.getElementById("Body4").innerHTML=
    xmlDoc.getElementsByTagName("Body")[4].childNodes[0].nodeValue;
    Essentially I need to define an array that will define 5 different span ID's for the way I have the rest of the site setup.
    Last edited by sinsysonline; 12-07-2012 at 05:19 PM. Reason: Revision

  • #2
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Try something like this :

    Code:
    var i=0,d;
    while (d=document.getElementById("Body"+i])) d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes[0].nodeValue;
    EDIT : Sorry, a Body0 would be necessary to use this script !
    Last edited by 007julien; 12-07-2012 at 05:37 PM.

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    UPDATE: I've updated the code to look like this:

    Code:
    <div id="fitin">
    
    <span id="Body0"></span>
    </div>
    <hr></hr>
    <div id="slideshow">
       <div>
    <p class="small">Post 5</p>
    <span id="Body4"></span>
       </div>
       <div>
    <p class="small">Post 2</p>
    <span id="Body1"></span>
       </div>
       <div>
    <p class="small">Post 3</p>
    <span id="Body2"></span>
       </div>
       <div>
    <p class="small">Post 4</p>
    <span id="Body3"></span>
       </div>
    </div>
    </div>
    
    
    <script>
    
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","...=xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    
    var i=0,d;
    while (d=document.getElementById["Body"+i]) 
    d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes
    [0].nodeValue;
    
    </script>

    EDIT: It doesn't pull the same data that it was pulling before and no posts show up now. It's as if it's not gathering the array correctly (or posting it onto the page)
    Last edited by sinsysonline; 12-07-2012 at 07:13 PM.

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    var i=0,d;
    while (d=document.getElementById["Body"+i]) 
    d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes << I wouldn't split the line here
    [0].nodeValue;
    Code:
    var i='',d;
    while (d=document.getElementById["Body"+i]) {
        if (i === '') i = 0;
        d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes[0].nodeValue;
    }
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks! I'm away from the comp for the weekend but I'll be sure to post results when I get back in the comp on Monday!

    I'm pretty new to Javascript, but it's making more sense everyday. Thanks for the help!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,595
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    No, Andrew. Look again. He modified his HTML.

    He now *IS* using
    Code:
    <span id="Body0"></span>
    He no longer has any id of just "Body".

    I think he is going to have to learn to use a debugger. It's amazing how much time people will waste because they won't take the 20 to 30 minutes that is needed to learn to use a debugger.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Sorry I make at first a typo and updated It. With the Body0, It's :
    Code:
    var i=0,d;
    while (d=document.getElementById("Body"+i)) d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes[0].nodeValue;
    With curly brackets for the getElmentById() !

    EDIT : With parentheses or parens. Thanks Old Pedant !
    Last edited by 007julien; 12-07-2012 at 11:56 PM. Reason: corrections. Take this code please !

  • Users who have thanked 007julien for this post:

    sinsysonline (12-10-2012)

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    @Old Pedant
    No, Andrew. Look again. He modified his HTML.
    Yes, I missed that, ta.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,595
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    [QUOTE=007julien;1298190With curly brackets for the getElmentById() !
    [/QUOTE]

    Properly, in English, we would say:

    ( ) -- Parentheses (often shortened to Parens)
    [ ] -- Square brackets, usually just called Brackets
    { } -- Braces (sometimes called Curly Brackets or, redundantly, Curly Braces)

    We would never say ( ) are curly brackets.

    [I know you are French. Just trying to help with the English terminology.]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks Old Pendant. The condescation is a bit unnecessary as I'm FREQUENTLY in your position where I get frustrated that people don't do their research.

    This is one of the first times I've literally WROTE my code instead of just modifying existing snippets, but I appreciate your support with helping me understand the concept.

    I assure you, a debugger is definitely my next step. This is a project I'm doing with a timeline and although I have it working (although very sloppy and not properly coded) I'm simply trying to expand my grasp on the concept.

    In any event, any replies, even if they are advice on tools to use are greatly appreciated.

    I'll take all of the feedback here and figure it out on Monday and post any results I get. Thanks for all of the support!

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,595
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Trust me, you are far and way NOT the first or only person who has come here for help that we can't really give because we don't have access to your site. If you could give us a publicly accessible URL, then we could probably find the bug in minutes, using a debugger.

    But when you have a private site, then the only real answer is to learn to use a debugger, and that's neither condescension nor frustration. It's simply the truth.

    Honest, if you would spend 20 to 30 minutes--maybe less--leaning to use a debugger, you would be able to figure this out in minutes yourself, more than llikely.

    My own preference nowadays is for the Chrome debugger. They are all pretty much the same, but Chrome has a few features the others don't. Its biggest plus is that you can pull it up at any time and see the errors on the current page *and* set breakpoints on the current page without having to refresh the page. Neither Firebug for the FireFox browser nor the MSIE browser can do that. They make you at least refresh the page before you can do anything.

    And it's so easy to use: Just hit the F12 key and wham, there it is. If you have multiple files making up your page, there's an easy to use drop-down list on the SOURCES tab that allows you to pick the one you want to debug (e.g., a ".js" file,, or the main page itself).

    On the right side, all your currently available JS variables are neatly grouped (Global, Local, and Scope variables).

    Or set a breakpoint any where in your JS code (by simply clicking to the left of the line of code in the "Sources" pane) and, when it is reached, hover over any variable in the displayed JS code to find its value. Even hover over sxpressions. (For example, if your code has something like xmlDoc.getElementsByTagName("Body")[i++].childNodes[0].nodeValue you can hover over most any part of that. If you hovered over childNodes you'd see a list of all such nodes. And more.)

    Honest, you can learn the basics of the debugger in a few minutes and save yourself HOURS. Save yourself hours the first time you don't have to wait for an answer in the forum, for example.

    I really do encourage you--and anybody else reading this--to at least try using a debugger. If that still doesn't help...well, then it's for sure time to post here.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    sinsysonline (12-10-2012)

  • #12
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Perfect.

    I tried to delete the line break, but it seemed to delete characters. Then I reloaded page and it worked?

    I'll attempt debuggers for issues this simple in the future. Thank you all for the help!

    Issue resolved.


  •  

    Posting Permissions

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