jester626 Posted October 25, 2007 Share Posted October 25, 2007 This topic could go into so many different areas it is hard to determine which one, Sorry if I post this in the wrong area. I have a PHP Page that is Pulling an XML file in using XMLDOM which is javascript. I can traverse down the nodes and select the elements that I want to display buy using this code: document.getElementById("ColumnName").innerHTML=xmlDoc.getElementsByTagName("ColumnName")[0].childNodes[0].nodeValue; Then using an id tag to display. The problem is I have no clue how to make the php page display (loop through) every "record" inside the XML file. At present it only gives me the first record. Any help would be greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/ Share on other sites More sharing options...
jester626 Posted October 25, 2007 Author Share Posted October 25, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378015 Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 What version of PHP are you using? Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378056 Share on other sites More sharing options...
jester626 Posted October 25, 2007 Author Share Posted October 25, 2007 5.1.2 Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378074 Share on other sites More sharing options...
premiso Posted October 25, 2007 Share Posted October 25, 2007 Have you tried http://php.net/simplexml ??? Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378076 Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 For example <?php $str = '<?xml version="1.0"?> <plays> <play id="1"> <title>The Tempest</title> </play> <play id="2"> <title>Romeo and Juliet</title> </play> <play id="3"> <title>Loves Labours Lost</title> </play> <play id="4"> <title>King Henry V</title> </play> </plays>'; $xml = simplexml_load_string($str); foreach ($xml->play as $a) { echo "ID : {$a['id']}<br />"; echo "Title : $a->title <br /><br />" ; } /* gives--> ID : 1 Title : The Tempest ID : 2 Title : Romeo and Juliet ID : 3 Title : Loves Labours Lost ID : 4 Title : King Henry V */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378107 Share on other sites More sharing options...
jester626 Posted October 26, 2007 Author Share Posted October 26, 2007 I have tried something very similiar, but due to the way the XML file is formatted, I am having a problem stepping down through the data tags. xml example: <YourResults> <SALES> <IDN>1234567891234567</TDN> <ReadDate>10/24/2007 10:55:00 AM</ReadDate> <TotalVends>1</TotalVends> <TotalSales>1.2500</TotalSales> <CashIn>1.2500</CashIn> <CashOut>0</CashOut> <BoxCash>0.2500</BoxCash> <TubeCash>0</TubeCash> <BillsToStacker>1.0000</BillsToStacker> <Items> <Item> <ColumnName>1</ColumnName> <Price>1.2500</Price> <Vends>0</Vends> <Sales>0</Sales> </Item> ..... ColumnName 2 through 9 in here .... <Item> <ColumnName>10</ColumnName> <Price>1.2500</Price> <Vends>0</Vends> <Sales>0</Sales> </Item> </Items> </SALES> </YourResults> The XML file could have multiple "ReadDate" meaning <SALES>....</SALES> could be displayed multiple times. I am trying to get the data into a MySQL database(which once I get the reporting part inplace I'll be able to perform the INSERT). The DB structure is as follows: [iDN][ReadDate][ColumnName][Price][Vends][sales] So using you example How do I get it to step through each ReadDate then through each ColumnName of Each Read Date? <CODE> $str = file_get_contents('report3.xml'); $xml = simplexml_load_string($str); echo $xml; foreach ($xml->SALES[0]->TDN as $a) { echo "TDN : {$a['TDN']}<br />"; echo "Title : $a->title <br /><br />" ; } </CODE> Thanks for any insight you can provide Jester Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378373 Share on other sites More sharing options...
jester626 Posted October 26, 2007 Author Share Posted October 26, 2007 I have made some progress using the following code: <?PHP $xml = simplexml_load_file("report3.xml"); //echo $xml->getName() . "<br />"; foreach($xml->SALES->Items->Item[0] as $child01) { echo $child01->getName() . ": " . $child01 . "<br />"; } ?> The problem is that this example only provide me with the very first ColumnName Record of the very first ReadDate: ColumnName: 1 Price: 1.2500 Vends: 0 Sales: 0 It is some progress but not much. Thanks for your help Jester Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378382 Share on other sites More sharing options...
jester626 Posted October 26, 2007 Author Share Posted October 26, 2007 I have solved my own problem, I don't know exactly know how I did it, but this is what I came up with. If anyone would like to comment and explain what is actually happening here, I am sure we all would appreciate an explanation. foreach ($YourResults->SALES as $SALES) { // Use this section to insert into the machinesales info table print("<B>"); printf("ReadDate: %s\n", $SALES->ReadDate); print("</B>"); print ("<BR>"); // use this section to insert into columnsales info table foreach ($SALES->Items->Item as $Item) { printf("Coil: %s\n", $Item->ColumnName); printf("Price: %s\n", $Item->Price); printf("Vends: %s\n", $Item->Vends); printf("Sales: %s\n", $Item->Sales); print ("<BR>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378391 Share on other sites More sharing options...
premiso Posted October 26, 2007 Share Posted October 26, 2007 <?php // Where ever $yourresults is defined it is an object of an array of objects // For every array of objects in Sales make them the variabel $sales foreach ($YourResults->SALES as $SALES) { // Use this section to insert into the machinesales info table print("<B>"); // Since $SALES is just an object now you can now access individual properties, such as ReadDate etc printf("ReadDate: %s\n", $SALES->ReadDate); print("</B>"); print ("<BR>"); // $SALES->Items is an array of an item object, so loop through those and assign // the current object to $Item // use this section to insert into columnsales info table foreach ($SALES->Items->Item as $Item) { printf("Coil: %s\n", $Item->ColumnName); printf("Price: %s\n", $Item->Price); printf("Vends: %s\n", $Item->Vends); printf("Sales: %s\n", $Item->Sales); print ("<BR>"); } } ?> See comments above. Quote Link to comment https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/#findComment-378589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.