473,671 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generate XML files with PHP and MySQL

4 New Member
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(D B_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_compan y, delivery_street _address, delivery_suburb , delivery_city, delivery_postco de, delivery_state, delivery_countr y FROM zen_orders WHERE orders_id >'1'" );
$result_fld2 = mysql_query( "SELECT orders_id, products_model, products_quanti ty FROM zen_orders_prod ucts WHERE orders_id >'1'" );


while( $row1 = mysql_fetch_row ($result_fld) ) {
$output .="<ShipTo>";
$output .="<Address>" ;
$output .="<Po>\"$row 1[0]\"</Po>";
$output .= "<Line1>\"$ row1[1]\"</Line1>";
$output .= "<Street1>\"$ro w1[3]\"</Street1>";
$output .= "<Street2>\"$ro w1[4]\"</Street2>";
$output .= "<City>\"$r ow1[5]\"</City>";
$output .= "<State>\"$ row1[7]\"</State>";
$output .= "<Zip>\"$ro w1[6]\"</Zip>";
$output .= "<Country>\"$ro w1[8]\"</Country>";
$output .= "<Country>\"$ro w1[0]\"</Country>";
$output .= "<Country>\"$te st[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($db handle);
?>

Thanks again for your help
Apr 9 '07 #1
7 3129
Motoma
3,237 Recognized Expert Specialist
What is wrong with this? Errors? Bad formatting? Improper structure?
Apr 10 '07 #2
Gfmultimediadesigns
4 New Member
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 Recognized Expert Top Contributor
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
Gfmultimediadesigns
4 New Member
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
mihai123
13 New Member
you need to change your sql query

SELECT orders_id.zen_o rders, delivery_name, delivery_compan y, delivery_street _address, delivery_suburb , delivery_city, delivery_postco de, delivery_state, delivery_countr y, orders_id.zen_o rders_products, products_model, products_quanti ty FROM zen_orders, zen_orders_prod ucts WHERE orders_id.zen_o rders=orders_id .zen_orders_pro ducts

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
Gfmultimediadesigns
4 New Member
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 Recognized Expert Top Contributor
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_e xists($id,$line s){ #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'.$lin es[$id]] = $order['products_quant ity'];
//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
10393
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 function included in PHP to do that for me? I didn't find it, yet.
4
46961
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 you can see what I mean. http://members.shaw.ca/petermichaux/store/XML_XSLT_method.html I am interested in critisim of how I implemented each method. However my main problem is deciding between methods. Which method is better for
1
740
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 remote host, so I will need to come up with a solution that I can run via something like phpmyadmin, mysqlcc, or TOAD for mySQL. Any help would be appreciated.
1
3504
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 remote host, so I will need to come up with a solution that I can run via something like phpmyadmin, mysqlcc, or TOAD for mySQL. Any help would be appreciated.
23
2395
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 F, whilst use Jane might be able to access files in folders A, D and E. I would want each user to only see text and links relevant to the files in their chosen folders...
0
8917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7437
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4407
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.