473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Generate XML files with PHP and MySQL

Hello,
I'm attempting to generate an xml file through php which pulls from two seperate tables in a single database. I will be the first to admit that I am still learning and it is probably a very simple solution but for some reason i can't seem to figure it out. So any help would be very kind. Here is the code for my recent attempt.

// connection to the database
$dbhandle = mysql_connect(DB_SERVER, DB_USER, DB_PASS)
or die("Unable to connect to MySQL");

// select a database to work with
$selected = mysql_select_db(DB_NAME, $dbhandle)
or die("Could not select examples");

$output = "<?xml version=\"1.0\" ?>\n";
$output .= "<batch>";

$output .="<order>";

// iterate over each table and return the fields for each table


$result_fld = mysql_query( "SELECT orders_id, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_country FROM zen_orders WHERE orders_id >'1'" );
$result_fld2 = mysql_query( "SELECT orders_id, products_model, products_quantity FROM zen_orders_products WHERE orders_id >'1'" );


while( $row1 = mysql_fetch_row($result_fld) ) {
$output .="<ShipTo>";
$output .="<Address>";
$output .="<Po>\"$row1[0]\"</Po>";
$output .= "<Line1>\"$row1[1]\"</Line1>";
$output .= "<Street1>\"$row1[3]\"</Street1>";
$output .= "<Street2>\"$row1[4]\"</Street2>";
$output .= "<City>\"$row1[5]\"</City>";
$output .= "<State>\"$row1[7]\"</State>";
$output .= "<Zip>\"$row1[6]\"</Zip>";
$output .= "<Country>\"$row1[8]\"</Country>";
$output .= "<Country>\"$row1[0]\"</Country>";
$output .= "<Country>\"$test[0]\"</Country>";

$output .="</Address>";
$output .="</ShipTo>";

while($test = mysql_fetch_row($result_fld2)){
if($test[0] == $row1[0] ) {
$output .="<Line>";
$output .= "<model>\"$test[1]\"</model>";
$output .= "<q>\"$test[2]\"</q>";
$output .="</Line>";
}
}

$output .="</order>";

$output .= "</batch>";

// tell the browser what kind of file is come in
header("Content-type: text/xml");
// print out XML that describes the schema
echo $output;

// close the connection
mysql_close($dbhandle);
?>

Thanks again for your help
Apr 9 '07 #1
7 3119
Motoma
3,237 Expert 2GB
What is wrong with this? Errors? Bad formatting? Improper structure?
Apr 10 '07 #2
my bad, the problem i am running into is i need it to pull the products from the second query and each product is a seperate row with the order # as the key value but when the loop structure runs it goes by a row to row basis so its not pulling all the rows ie if a customer orders more then one product it will only show the first product. I can't seem to figure out what i need to do to get it to pull multiple rows based on the order #
Apr 10 '07 #3
code green
1,726 Expert 1GB
The industry standard for what you are trying to achieve is done by having two tables (at least). An `orders` table with the customer details and total order value and an `order_id`. The second table `orderlines` has multiple entries for each `order_id` matching the number of lines ordered. Without this structure you are barking up the wrong tree.
Apr 10 '07 #4
Thats pretty much how the database is setup. the first query pulls from the customer info table while the second is pulling from the order_products table. its just i can't seem to figure out how to have it pull multiple rows from the second table based on the order_id. the code works but it goes row by row within the loop so it only pulls one of the however many products they order. the solution is probably something simple but for some reason i can not grasp the answer.
Apr 10 '07 #5
you need to change your sql query

SELECT orders_id.zen_orders, delivery_name, delivery_company, delivery_street_address, delivery_suburb, delivery_city, delivery_postcode, delivery_state, delivery_country, orders_id.zen_orders_products, products_model, products_quantity FROM zen_orders, zen_orders_products WHERE orders_id.zen_orders=orders_id.zen_orders_products

I'm not sure that this query will work but for sure if you'll use a join method there that will be just fine.
Google it: mysql join left syntax examples
Apr 11 '07 #6
Awesome, I went ahead and implemented the suggested code and it returns this table
Orders_id Delivery_name Etc. Orders_id Products_model
2 Test2 etc 2 Model3
3 Test3 etc 3 Model5
4 Test4 etc 4 Model2
5 Test5 etc 5 Model2
5 Test5 etc 5 Model4

Now my final question ( I think ) is I need it to loop through and spit out the delivery info and the products ordered info in an xml format. From what I understand when using a loop it will run through each row, but if someone orders more then one product it creates a new row for each product model ordered. So is it possible to join rows based on the order id? Or is there a specific loop structure I should use? I’ve tried while() as well as the foreach() I’ve also tried a nested while() but that keep sending back errors. So at this point I’m dumbfounded. Thanks again for any assistance!
Apr 11 '07 #7
code green
1,726 Expert 1GB
mihai123 has given you a query that generates one big recordset with all orders and orderlines. I prefer to use your original idea of one query to capture the orders and a second to capture the orderlines. It is a little bit tidier to handle and allows the early identification of errors. But back to handling the array you have.

[PHP]//Not necessary but good practice
$lines = array();
$custorder = array();

foreach($record as $order){
$id = $order['order_id'];
if(!array_key_exists($id,$lines){ #is it a new order number?
$lines[$id]= 0; #New id flag

//Collect the basic order details
$custOrder[$id]['name'] = $order['delivery_name'];
//etc
}
else{ #Collect the order lines
$lines[$id] ++; #increment the no of lines ordered
$custOrder[$id]['model'.$lines[$id]] = $order['products_model'];
$custOrder[$id]['quantity'.$lines[$id]] = $order['products_quantity'];
//etc
}
}[/PHP]This is off the top of my head so excuse the syntax and there are certainly other methods. But you should end up with a 2d array of the format
Expand|Select|Wrap|Line Numbers
  1. Array(id=>first_order_id( 
  2.    Array(name=>delivery_name,.. etc, model1=>orderline,quantity1=>quantity,
  3.    model2=>orderline,quantity2=>quantity etc)
  4.    id=>second_order_id Array(second order details) .. 3rd() etc)
Apr 12 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Lars Plessmann | last post by:
how can I generate a mysql timstamp value of the current time? sample: 20040605042505 YYYYMMDDhhmmss is it only possible by concat year.month.day.hours.minutes.seconds or is there any final...
4
by: petermichaux | last post by:
Hi, I am interesting in using PHP to dynamically create an XML document and then use XSLT to transform to XHTML. I have come across two methods for doing this. I made two equivalent examples so...
1
by: JStrummer | last post by:
Any recommendations on how to generate an INSERT SQL script for the records in a tab-delimietd text file? The limitation is that, while I do have command-line mySQL access locally, I do not at my...
1
by: JStrummer | last post by:
Any recommendations on how to generate an INSERT SQL script for the records in a tab-delimietd text file? The limitation is that, while I do have command-line mySQL access locally, I do not at my...
23
by: Geoff Cox | last post by:
Hello, Is it possible to use php to generate different menus for users who have access to files in different folders? For example, user Fred might be able to access files in folders A, B and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.