Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/74769-solved-xmldom-looping-through-xml-file/
Share on other sites

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 

*/
?>

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

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

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>");
          
      }

  }



<?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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.