How to create xml structure so that it looks one single set of each invoice

hi all

i can easily create xml file with php.

But how to create a xml data structure so that it keeps the relation between tables data.

this xml is being generated from 3 database tables data with php mysql

i will be fetching data from xml file with simplexml library

invoice table and customer table will echo result once

but invoice detail table will echo result multiple times due to multiple products purchased by


i am able to fetch data using foreach loop

problem comes when there are multiple products in invoice detail table

how to create xml structure and keep relation between these 3 tables

means these 3 tables data should be combined to one set or group (dont know what its called)

so that when i want to load result of invoice-id=105 then only its related data should show.


<?xml version="1.0" encoding="ISO-8859-2"?>
// invoice table

//invoice details table
// first product details
<product_name>samsung mobile</product-name>
// second product details
<product_name>NOKIA mobile</product-name>

customer table

Is it possible to show the code you are using to query the data and as well the code to build the xml? The reason i ask is it is very hard to see what is happening without reading through your code and understanding where the logic can be extended for the intended results.

A couple of thoughts

I don’t think you need to repeat the invoice-id several times.

Wrapping the product details in <product>...</product> might also help.

hi jgetner

i dont need any help on php code.
i will modify my php code according to final xml structure

i need help on basic structuring the xml so that it is created as one set
you can change my above static xml structure


hi gandalf458

thanks for the reply
how does xml assume or interpret it as a product of same invoice ??
i know we can create any node or tag with any name
but how do xml relate it to same invoice


Because it’s all wrapped in <invoice>...</invoice>

hi all

here is my xml file

<?xml version="1.0" encoding="ISO-8859-2"?>
<product_name>Samsung Galaxy</product_name>
<product_name>Samsung Fast</product_name>


this is my php page which is using simplexml to fetch data

$products = simplexml_load_file("");
foreach ($products as $prod) {
    $invoice_id = $prod->invoice_id;
    $invoice_date = $prod->invoice_date;
    $shipping = $prod->shipping;
    $invoice_total = $prod->invoice_total;
    $product_name = $prod->product_name;
    $product_price = $prod->product_price;
    $quantity = $prod->quantity;
    $total_cost = $prod->total_cost;

   echo $invoice_id."<br>";
   echo $invoice_date ."<br>";
   echo $shipping ."<br>";
   echo $invoice_total."<br>";
   echo $product_name."<br>";
   echo $product_price."<br>";
   echo $quantity."<br>";
   echo $total_cost."<br>";


the php script only echo invoice data.

product data like product_name,price, quantity etc is not echoed.

the problem is that product items are multiple

so the script is not echoing the product data from xml

how to fetch product listing or data from xml file


It looks to me as though you need two nested foreach statements. The inner to process a product and the outer to process an invoice.

1 Like

Yes, unfortunately simply naming the variable $products doesn’t mean that’s the node it will use.

As gandalf458 posted you will need to loop through the <product> nodes inside a loop of the <invoice> nodes.

1 Like

yes thanks

i did it with two foreach loops


1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.