...

View Full Version : How to call a looping array instead of 5 individual pulls



sinsysonline
12-07-2012, 05:08 PM
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.



<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.

007julien
12-07-2012, 06:26 PM
Try something like this :


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 !

sinsysonline
12-07-2012, 07:03 PM
UPDATE: I've updated the code to look like this:



<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)

AndrewGSW
12-07-2012, 10:28 PM
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;


var i='',d;
while (d=document.getElementById["Body"+i]) {
if (i === '') i = 0;
d.innerHTML=xmlDoc.getElementsByTagName("Body")[i++].childNodes[0].nodeValue;
}

sinsysonline
12-07-2012, 11:13 PM
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!

Old Pedant
12-07-2012, 11:18 PM
No, Andrew. Look again. He modified his HTML.

He now *IS* using


<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.

007julien
12-07-2012, 11:21 PM
Sorry I make at first a typo and updated It. With the Body0, It's :


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 !

AndrewGSW
12-07-2012, 11:29 PM
@Old Pedant

No, Andrew. Look again. He modified his HTML.
Yes, I missed that, ta.

Old Pedant
12-08-2012, 12:01 AM
[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.]

sinsysonline
12-08-2012, 12:12 AM
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!

Old Pedant
12-08-2012, 01:10 AM
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.

sinsysonline
12-10-2012, 05:38 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum