looping problem: get distinct values for one person from multiple entries in XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PC Rider
    New Member
    • Nov 2011
    • 5

    looping problem: get distinct values for one person from multiple entries in XML file

    Hello everyone,

    I want to make a page that select distinct values for one person from an XML file into a table. The XML has multiple entry's for this person. I can get all of the data, or just the first one, but I want all of one person, represented by his or hers "INLOGKODE" . I try it with a loop, while a condition is true. It just does n't work this way I guess...

    Here's what I've got so far:

    The XML:( History.xml on the server uploaded)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot>
    <History>
    <INLOGKODE>Beer4</INLOGKODE>
    <DATUM>2011-04-18T00:00:00</DATUM>
    <SCORE>38</SCORE>
    <HCP-OUD>21.5</HCP-OUD>
    <HCP-NIEUW>20.7</HCP-NIEUW>
    <OPMERKING>Damesmiddag</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Beer4</INLOGKODE>
    <DATUM>2011-05-17T00:00:00</DATUM>
    <SCORE>32</SCORE>
    <HCP-OUD>21</HCP-OUD>
    <HCP-NIEUW>21</HCP-NIEUW>
    <OPMERKING>ESO2011 gp1 dag2 Vnd</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Beer4</INLOGKODE>
    <DATUM>2011-05-20T00:00:00</DATUM>
    <SCORE>19</SCORE>
    <HCP-OUD>21.1</HCP-OUD>
    <HCP-NIEUW>21.2</HCP-NIEUW>
    <OPMERKING>ESO2011 finale</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Trox214</INLOGKODE>
    <DATUM>2011-05-08T00:00:00</DATUM>
    <SCORE>31</SCORE>
    <HCP-OUD>17.3</HCP-OUD>
    <HCP-NIEUW>17.4</HCP-NIEUW>
    <OPMERKING>Maandbeker2011 mei</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Trox214</INLOGKODE>
    <DATUM>2011-07-05T00:00:00</DATUM>
    <SCORE>34</SCORE>
    <HCP-OUD>17.8</HCP-OUD>
    <HCP-NIEUW>17.8</HCP-NIEUW>
    <OPMERKING>Herenmiddag</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Trox214</INLOGKODE>
    <DATUM>2011-05-10T00:00:00</DATUM>
    <SCORE>34</SCORE>
    <HCP-OUD>17.4</HCP-OUD>
    <HCP-NIEUW>17.4</HCP-NIEUW>
    <OPMERKING>Herenmiddag</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Trox214</INLOGKODE>
    <DATUM>2011-04-02T00:00:00</DATUM>
    <SCORE>23</SCORE>
    <HCP-OUD>16.9</HCP-OUD>
    <HCP-NIEUW>17</HCP-NIEUW>
    <OPMERKING>Maandbeker2011 april</OPMERKING>
    </History>
    <History>
    <INLOGKODE>Trox214</INLOGKODE>
    <DATUM>2011-05-31T00:00:00</DATUM>
    <SCORE>33</SCORE>
    <HCP-OUD>17.4</HCP-OUD>
    <HCP-NIEUW>17.4</HCP-NIEUW>
    <OPMERKING>Herenmiddag</OPMERKING>
    </History>
    </dataroot>
    The site is in Wordpress, I extract the currently logged in user in PHP and then pass it to Javascript on the page:

    Code:
    <?php session_start();
    $_SESSION["user_login"] = $DataArray[2];
    $_SESSION["display_name"] = $DataArray[3];
    require_once( '../../../../wp-load.php' );
    global $current_user;
    get_currentuserinfo();
    // A welcoming (in Dutch)
          echo 'Welkom '.$current_user->display_name . '. Uw inlogkode is: '.$current_user->user_login. "\n";
    ?>
    
    <script type="text/javascript"> // The approach of the data XML//
    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","History.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("History");
    var persoon =new Object();
    var persoon = "<?php echo  $current_user->user_login ?>";
    
    with ({xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue}); == persoon);
      
      { 
      document.write("<tr><td>");
      document.write(xmlDoc.getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(xmlDoc.getElementsByTagName("SCORE")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(xmlDoc.getElementsByTagName("HCP-OUD")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(xmlDoc.getElementsByTagName("HCP-NIEUW")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(xmlDoc.getElementsByTagName("OPMERKING")[0].childNodes[0].nodeValue);
      document.write("</td></tr>");
      }
    document.write("</table>");
    </script>
    I tried this "with" loop, but get an error:
    missing : after property id

    Anyone any idea? Or maybe just a wrong method for what I want? Some help would be appreciated..
    Last edited by gits; Nov 9 '11, 01:40 PM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You could use a while loop or a for loop. Your syntax is incorrect.

    There's different ways you could do this. One way would be to loop over the INLOGKODEs, compare its values and then get the corresponding data values, e.g.
    Code:
    // first get the elements, e.g.
    var inlogkodes = xmlDoc.getElementsByTagName("INLOGKODE");
    for (i = 0; i < inlogkodes.length; i++) {
        // check value against persoon
        ...
        // if value matches, then get the [i]i[/i]th index of the data, e.g.
        datum[i]
    Also, try to avoid document.write( ), though in this case, it's a synchronous request. It does beg the question why you don't just use PHP to parse and display...

    Comment

    • PC Rider
      New Member
      • Nov 2011
      • 5

      #3
      Thank you acoder for looking into my silly scripting attempts.

      Aside from your other questions, which I will answer in another post, here's what I've come up to for now:

      Code:
      var inlogkode = "<?php echo  $current_user->user_login ?>";
      var persoon = xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue;
      
       
        for (i=0;i<persoon.length;i++) // The iteration process
        if ( persoon == inlogkode )    // Checking the value of array against current user
      
                                       // If so, write value's to the table..
      
        { 
        document.write("<tr><td>");
        document.write(x[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
      // and so on...
      The code doesn't give any value's . From Firebug I can see the XML is loaded, variables are defined, but nothing happens in the writing value process.

      Is it anything with the iteration process? Or can't I define the condition like the way I did?

      Appreciate any further tips or hints.

      Comment

      • PC Rider
        New Member
        • Nov 2011
        • 5

        #4
        the asides...

        I 've read about avoiding document.write( ), but don't fully understand the reason of that warning. All tutorials I find use it and most snippets do the work...

        Anyone with a good introduction on this subject? Please feel free!

        Than the other one: Why not use PHP? Just because I feel more at ease with Jscript. Though not very experienced with both, Jscript was just more "readable" to me. Maybe some other time, without the urge to just get it done as soon as possible without too many diving into it. Although I feel that alone for performance reasons server side will be the better option. Maybe later I will read some more about PHP and SQL and rewrite the whole into more efficient coding.

        Any help is welcome...thank s in advance!

        Comment

        • PC Rider
          New Member
          • Nov 2011
          • 5

          #5
          Checking with: if ...else

          After reading previous attempts, it is now obvious to me why the loop didn't give any result.

          So I tried this nested loop: a for.. loop with a conditional "if .. else" inside.

          Helas, no result either. So where do I go wrong?

          Fragment of the script:

          Code:
          .......
          var x=xmlDoc.getElementsByTagName("History");
          var persoon = "<?php echo  $current_user->user_login ?>";
          var inlogkode = xmlDoc.getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue;
          
          for (i = 1; i < inlogkode.length; i++)                //loop through the XMLdoc//
              {
            inlogkode =(x[i].getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue); 
             
          if ( inlogkode == persoon )  //See if value corresponds to current user//
                
            {                          //if so, write the table //
          
          for (j = 0; j < inlogkode.length; j++)      
          
            document.write("<tr><td>"); 
            document.write(x[j].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
          // and so on..
            
            }
            else                        // if not, return to the first loop and increment i //
            {
            (i=i+1)
            }
             
            }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            On line 2, call "x", "history" to make things clearer.

            On line 6, you need to loop over this history object, not inlogkode. Also, you won't need line 4. Any reason why the loop index starts from 1 and not 0?

            You also won't need lines 22-24 - that's the job of the i++ in the for loop declaration.

            Test/debug your code with breakpoints, watching variables and stepping through it line by line using, e.g. Firebug

            Comment

            • PC Rider
              New Member
              • Nov 2011
              • 5

              #7
              Almost solved...

              Thank You, Acoder!

              These tips helped me quite further. I get values from the current user, so far, so good.
              But, it spits out the table quite odd: The date does not get formatted into the first row of the table!

              Here a small example of the first three rows:
              Welkom Luc Trox. Uw inlogkode is: Trox214
              2011-05-08T00:00:002011-07-05T00:00:002011-05-10T00:00:00
              31 17.3 17.4 Maandbeker2011 mei
              34 17.8 17.8 Herenmiddag
              34 17.4 17.4 Herenmiddag

              Here's the script from the table definition on:

              Code:
              document.write("<table border='1'>");
              var history=xmlDoc.getElementsByTagName("History");
              var persoon = "<?php echo  $current_user->user_login ?>";
              for (i = 0; i < history.length; i++) 
                {
                inlogkode =(history[i].getElementsByTagName("INLOGKODE")[0].childNodes[0].nodeValue);
                 
                 if ( inlogkode == persoon )                       // Here the control
                    
                  {
              
              for (j = i; j < inlogkode.length; j++)                // This did the trick, get them from the ith index number
               
                document.write("<tr><td>");
                document.write(history[j].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue);
                document.write("</td><td>");
                document.write(history[j].getElementsByTagName("SCORE")[0].childNodes[0].nodeValue);
                document.write("</td><td>");
                document.write(history[j].getElementsByTagName("HCP-OUD")[0].childNodes[0].nodeValue);
                document.write("</td><td>");
                document.write(history[j].getElementsByTagName("HCP-NIEUW")[0].childNodes[0].nodeValue);
                document.write("</td><td>");
                document.write(history[j].getElementsByTagName("OPMERKING")[0].childNodes[0].nodeValue);
                document.write("</td></tr>");
                   }   
                 } 
                 
                document.write("</table>");
              Why the odd behavior? Or better abandon the "document.write " and use something like this?

              Code:
               {
                    txt=txt + "<tr>";
                   
                      xx=x[i].getElementsByTagName("DATUM");
                      {
                      try
                        {
                        txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                        }                    
                                                            //and so on
              I think with some more help I will be able to get it right, please some more help on this.

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                There's no need for the second for loop.

                Once you find a match, you just need to get the data for that "History" element, i.e.
                Code:
                history[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue

                Comment

                Working...