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

customer

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.

vineet

<?xml version="1.0" encoding="ISO-8859-2"?>
<Feed>
<Invoice>
// invoice table
<invoice-id>105</invoice-id>
<invoice-date>2016-10-07</invoice-date>
<invoice-amount>1000.00</invoice-amount>
<customer-name>vineet</customer-name>

//invoice details table
// first product details
<invoice-id>105<invoice-id>
<product_name>samsung mobile</product-name>
<product_price>150.00</product-price>
<product_quantity>2</product-quantity>
<product-total>300.00</product-total>
// second product details
<invoice-id>105<invoice-id>
<product_name>NOKIA mobile</product-name>
<product_price>150.00</product-price>
<product_quantity>3</product-quantity>
<product-total>450.00</product-total>

customer table
<customer_name>vineet</customer_name>
<customer_phone>123456</customer_phone>
<customer_city>india</customer_city>
</Invoice>
</Feed>

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

thanks
vineet

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

vineet

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

hi all

here is my xml file

<?xml version="1.0" encoding="ISO-8859-2"?>
<Invoices>
<Invoice>
<invoice_id>2246</invoice_id>
<invoice_date>2016-09-10</invoice_date>
<shipping>10</shipping>
<invoice_total>1078.00</invoice_total>
<product>
<product_name>Samsung Galaxy</product_name>
<product_price>250.00</product_price>
<quantity>1</quantity>
<total_cost>250.00</total_cost>
</product>
<product>
<product_name>Samsung Fast</product_name>
<product_price>89.00</product_price>
<quantity>1</quantity>
<total_cost>89.00</total_cost>
</product>

</Invoice>
</Invoices>

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

<?php
$products = simplexml_load_file("http://www.mywebdomain.com/invoices_xml.xml");
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

vineet

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

vineet

1 Like

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