Keen-EGS
Keen-EGS

Reputation: 23

whats wrong with my Looping?

XML Response:

<NewDataSet xmlns="">
<Table diffgr:id="Table1" msdata:rowOrder="0">
    <Code>106377</Code>
    <Name>Blackened red snapper</Name>
    <Category>123</Category>
    <Yield>4</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table2" msdata:rowOrder="1">
    <Code>303570</Code>
    <Name>Celery soup</Name>
    <Category>123</Category>
    <Yield>1</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table3" msdata:rowOrder="2">
    <Code>303675</Code>
    <Name>Challah French Toast</Name>
    <Category>123</Category>
    <Yield>6</Yield>
    <YieldUnit/>
</Table>
<Table diffgr:id="Table4" msdata:rowOrder="3">
    <Code>303681</Code>
    <Name>Challah french toast</Name>
    <Category>123</Category>
    <Yield>4</Yield>
    <YieldUnit/>
</NewDataSet>

HTML codes

<div data-role="output">
    <ul id="RecipeList" data-role="listview" data-split-icon="star" data-split-theme="e" data-inset="true">
    </ul>
</div>

JS Codes

var CodeObj = new Array();
var NameObj = new Array();
var Codeindex = 0;
var Nameindex = 0;

$(req.responseText).find('Name').each(function () {
    NameObj[Nameindex] = $(this).text();
    Nameindex += 0;

    for (var i = 0; i < NameObj.length; i++) {
        $(this).append(NameObj[i] + "<br/>");

        $(req.responseText).find('Code').each(function () {
            CodeObj[Codeindex] = $(this).text();
            Codeindex += 0;
            for (var a = 0; a < CodeObj.length; a++) {
                $(this).append(CodeObj[a] + "<br/>");
            }
        });

        var $content = $('<li><a href="#"><img src="../../img/album-bb.jpg"><h3>Name: ' + NameObj[i] + '</h3><p>Code: ' + CodeObj[a] + '</p></a><a href="#purchase" data-rel="popup" data-position-to="window" data-transition="pop">Add to favorites</a></li>');
        $('#RecipeList').append($content).listview('refresh');
    }
});

Listview Output

Name : Blackened red snapper
Code : 106377
Name : Celery soup
Code : 106377
Name : Challah french toast
Code : 106377
Name : Challah french toastr
Code : 106377

The Problem : Always same Code. Can anyone know whats wrong with my loop? thank you. I tried many ways. but still can get the right output for Name

Upvotes: 0

Views: 91

Answers (3)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Nameindex += 0 and Codeindex += 0 are no-ops, so they probably have something to do with it.

Instead, try this:

$(req.responseText).find("Table").each(function() {
    var item = $(this),
        name = item.find("Name").text(),
        code = item.find("Code").text();
    $("#RecipeList").append('<li><a href="#"><img src="...." />'
       +'<h3>'+name+'</h3>Code: '+code+'</a>.....');
});

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167212

The problem is with the increment. Replace:

Nameindex +=0;
Codeindex +=0;

With

Nameindex++;
Codeindex++;

or

Nameindex +=1;
Codeindex +=1;

Upvotes: 1

hieu.do
hieu.do

Reputation: 59

Seems

Nameindex +=0; Codeindex +=0;

is wrong.Shouldn't it be Nameindex+=1?

Upvotes: 0

Related Questions