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) - <?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: - <?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..
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. - history[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue
7 2203
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. - // 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 ith 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...
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: -
-
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.
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...thanks in advance!
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: -
.......
-
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)
-
}
-
-
}
-
-
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
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: - 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? - {
-
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.
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. - history[i].getElementsByTagName("DATUM")[0].childNodes[0].nodeValue
Sign in to post your reply or Sign up for a free account.
Similar topics
by: CJ |
last post by:
Can someone look at the code below and tell me whats going on?
Here is the problem. I can successfully telnet one of our routers and
successfully put in a command in the while statement, but...
|
by: CanoeGuy |
last post by:
I have an array that I am populating from a database. I am looping through
the array to write the values to a datagrid. At this point, I'm stuck. I
can't figure out how to read the first four...
|
by: vasilijepetkovic |
last post by:
Hello All,
I have a problem with the program that should generate x number of txt
files (x is the number of records in the file datafile.txt).
Once I execute the program (see below) only one...
|
by: Nicole |
last post by:
Hello! I hope there is someone out there who can shed some light on
this for me. I have a module that is supposed to look at an access
table, pull out each bid record, link to another table to...
|
by: Bruce Lawrence |
last post by:
I'm running Access 97 and my modules are looping if someone puts an
invalid value in.
The setup:
3 macros - get_clock_num, verify_clocknum, append_to_history
3 functions. each in their own...
|
by: Mike |
last post by:
Hello,
This game will not loop. It compiles, builds, and executes fine, but
the game will not loop around no matter what I do. What is wrong with
the code? What do I have to add to make it loop...
|
by: Kelvin |
last post by:
Hi All,
I wrote a program insert new row between rows while the datatable is
looping. It display the error message.
Exception Details: System.ArgumentException: This row already belongs
to this...
|
by: nasirmajor |
last post by:
dear all,
i have a listbox whose items are displayed in listbox e.g
('1','3','4',)
my problem is the last comma (, after 4) i don't want this comma as i
have to use
this string in sql query....
|
by: Steven Smith |
last post by:
Hi everyone,
This code makes sense to me, but when I run it, it says "Loop Without Do".
Any Suggestions?
Thanks,
Steven Smith
Dim FirstPaymentDate As Date
|
by: Promextheus Xex |
last post by:
First php post...
Hi there,
I'm writing some code for my website to list people listening to my music. I
have a loop for an array that dosen't seem to display all people. most of
the time it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |