PDA

View Full Version : Nested for loop



rhamej
March 7th, 2008, 02:05 PM
Im trying to wrap the data in a div or a p tag. But for some reason the whole data is wrapping in a p tag. How do I run the first for loop 6 times while checking the nested for loop only one time?



<script type="text/javascript">
var xmlDoc = null;
if (window.ActiveXObject) {// code for IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
} else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc.
xmlDoc = document.implementation.createDocument("", "", null);
} else {
alert('Your browser cannot handle this script');
}
if (xmlDoc != null) {
xmlDoc.async = false;
xmlDoc.load("Products.xml");
document.write("<div id='main'>");
var x = xmlDoc.getElementsByTagName("Product");
alert(x.length);
for (i=0; i<x.length; i++) {
var y = xmlDoc.getElementsByTagName("Property");
document.write("<p>");
for (i=0; i<y.length; i++){
document.write(y[i].attributes.getNamedItem("value").value);
document.write("<br />");
}
document.write("</p>");
}
document.write("</div");
}
</script>

kdd
March 7th, 2008, 10:32 PM
What??? I'm so confused... :?:

Something else, why do you have for ( i = 0 ...) and then again for ( i = 0 ... ) nested? Just use a diff. var. var i = 0, j = 0; Then check ( i == 6 ), I guess. I'm not really understanding what your question is...

rhamej
March 8th, 2008, 09:59 AM
lol, ya, Im a little confused too :P

Here is the xml.


<?xml version="1.0" encoding="UTF-8"?>
<Products>
<Product>
<Properties>
<Property name="ProductID" value="Widget 1"/>
<Property name="Description" value="This is text about widget 1"/>
<Property name="Price" value="100.0000"/>
</Properties>
</Product>
<Product>
<Properties>
<Property name="ProductID" value="Widget 2"/>
<Property name="Description" value="This is a description of widget 2"/>
<Property name="Price" value="131.7700"/>
</Properties>
</Product>
<Product>
<Properties>
<Property name="ProductID" value="Widget 3"/>
<Property name="Description" value="These are the details of widget 3"/>
<Property name="Price" value="77.0000"/>
</Properties>
</Product>
<Product>
<Properties>
<Property name="ProductID" value="Widget 4"/>
<Property name="Description" value="This is text about widget 4"/>
<Property name="Price" value="32.0000"/>
</Properties>
</Product>
<Product>
<Properties>
<Property name="ProductID" value="Widget 5"/>
<Property name="Description" value="This is widget 5 text"/>
<Property name="Price" value="44.1000"/>
</Properties>
</Product>
<Product>
<Properties>
<Property name="ProductID" value="Widget 6"/>
<Property name="Description" value="This is text about widget 6"/>
<Property name="Price" value="4.2000"/>
</Properties>
</Product>
</Products>
I need to wrap each one in a div. Hope that makes since :)

kdd
March 8th, 2008, 12:14 PM
Ah. That makes a bit more sense... :)

Try doing what I said before. Change the variable of the 2nd loop (the nested one) to var j. Then y[j].attributes...

I still doubt your code will work.

Solution:



var x = xmlDoc.getElementsByTagName("Property");
for ( var i = 0, var j = 0 ; i < x.length ; i++, j++ )
{
if ( j == 2 )
{
//output <div>
}
//output data[i] + <p />
//output data[i] + <p />
//output data[i] + <p />
if ( j == 2 )
{
//output </div>
j = 0;
}
}


When you getElementsByTagName("Property"), it'll get all elements that have Property as the tag. If you want to look inside, you'll need to use childNodes[...].nodeValue stuff.

Hope this helps.

rhamej
March 8th, 2008, 01:47 PM
Got it.
Thanks kdd =)


var xmlDoc = null;
if (window.ActiveXObject) {// code for IE
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
} else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc.
xmlDoc = document.implementation.createDocument("", "", null);
} else {
alert('Your browser cannot handle this script');
}
if (xmlDoc != null) {
xmlDoc.async = false;
xmlDoc.load("Products.xml");
var y = xmlDoc.getElementsByTagName("Property");
for (i=0, j=0; i<y.length; i++, j++) {
if (j == 0) {
document.write("<div>");
}
document.write(y[i].attributes.getNamedItem("value").value);
document.write("<br />");
if (j == 2) {
document.write("</div>");
j = -1;
}
}
}

kdd
March 8th, 2008, 02:24 PM
No problem, bro. Glad to help. :)