I have two arrays from two different xml files, one stores category information, one stores products under categories. They are connected via categoryID. Samples of the xml files are below. I read these xml files and save the data using ajax.
categories.xml
<Categories>
<CategoryID>1</CategoryID>
<CategoryName>Beverages</CategoryName>
<Description>Soft drinks, coffees, teas, beer, and ale</Description>
</Categories>
<Categories>
<CategoryID>2</CategoryID>
<CategoryName>Condiments</CategoryName>
<Description>Sweet and savory sauces, relishes, spreads, and seasonings</Description>
</Categories>
products.xml
<Products>
<ProductID>1</ProductID>
<ProductName>Chai</ProductName>
<CategoryID>1</CategoryID>
<QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
<UnitPrice>18</UnitPrice>
</Products>
<Products>
<ProductID>2</ProductID>
<ProductName>Chang</ProductName>
<CategoryID>1</CategoryID>
<QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
<UnitPrice>19</UnitPrice>
</Products>
I read two files like below
$.ajax({
type: 'GET',
url: 'categories.xml',
dataType: "xml",
success: function(data) {
$(data).find('CategoriesRoot Categories').each(function(i){
cateid = $(this).find('CategoryID').text();
catename = $(this).find('CategoryName').text();
catedescription = $(this).find('Description').text();
categoryarray[i] = new Array();
categoryarray[i][0] = cateid;
categoryarray[i][1] = catename;
categoryarray[i][2] = catedescription;
});
$.ajax({
type: 'GET',
url: 'products.xml',
dataType: "xml",
success: function(data) {
$(data).find('ProductsRoot Products').each(function(j){
proid = $(this).find('ProductID').text();
proname = $(this).find('ProductName').text();
catid = $(this).find('CategoryID').text();
quantity = $(this).find('QuantityPerUnit').text();
price = $(this).find('UnitPrice').text();
productarray[j] = new Array();
productarray[j][0] = proid;
productarray[j][1] = proname;
productarray[j][2] = catid;
productarray[j][3] = quantity;
productarray[j][4] = price;
});
And I need to show nested order list like below
i.food
a) product
b) another product
ii. drink
a) coke
b) juice
To do that, I have the loop inside ajax below
for(var k=0;k<categoryarray.length;k++){
if(categoryarray[k][0]!==""){
$('.cate ol').append('<li id="Cate_' + k + '">'+categoryarray[k][1]+'</li>');
for(var l=0;l<productarray.length;l++){
if(categoryarray[k][0]==productarray[l][2]){
$('#Cate_' + k).append('<ol id="Pro_' + l + '" type="a"></ol>');
$('#Pro_' + l).append("<li>"+productarray[l][1]+"</li>");
}
}
}
}
But it shows the first category and then fills all the products and other categories under it with a repeated and random order like this. At the end, the other seven categories are listed with order as ii-viii.
i.Beverages(1st cate)
a.Chai(1 pro of 1st cate)
b.Condiments(2nd cate)
a.Aniseed Syrup(1st pro of 2nd cate)
b.Confections(3rd cate(no pro under it))
c.Dairy Products(4th cate(no pro under it))
d.Grains/Cereals(5th cate)
a.hamburger bun(1st pro of 5th cate)
b.Meat/Poultry(6th cate)
a.beef burger(1st pro of 6th cate)
b.Produce(7th cate)
c.Seafood(8th cate)
a.tiger prawns(1st pro of 8th cate)
b.cooked prawns(2nd pro of 8th cate)
a.chicken burger(2nd pro of 7th cate)
b.Produce(7th cate)
c.Seafood(8th cate)
.....
Can anyone help me figure out where I did wrong? P.S. I put only a part of codes here. If you are confused about what I am asking, feel free to ask me! Many thanks!!!!