matty84 Posted November 22, 2006 Share Posted November 22, 2006 Hi I am interested in how the code below could be altered so that instead of creating the XML from array data, the xml is created from database information. Any suggestions?[code]<?php $employees = array(); $employees [] = array( 'name' => 'Albert', 'age' => '34', 'salary' => "$10000" ); $employees [] = array( 'name' => 'Claud', 'age' => '20', 'salary' => "$2000" ); $doc = new DOMDocument(); $doc->formatOutput = true; $r = $doc->createElement( "employees" ); $doc->appendChild( $r ); foreach( $employees as $employee ) { $b = $doc->createElement( "employee" ); $name = $doc->createElement( "name" ); $name->appendChild( $doc->createTextNode( $employee['name'] ) ); $b->appendChild( $name ); $age = $doc->createElement( "age" ); $age->appendChild( $doc->createTextNode( $employee['age'] ) ); $b->appendChild( $age ); $salary = $doc->createElement( "salary" ); $salary->appendChild( $doc->createTextNode( $employee['salary'] ) ); $b->appendChild( $salary ); $r->appendChild( $b ); } echo $doc->saveXML(); $doc->save("write.xml") ?> [/code] Link to comment https://forums.phpfreaks.com/topic/28140-php-and-xml/ Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 At beginning,[code]$sql = "SELECT name, age, salary FROM employee";$res = mysql_query($sql) or die (mysql_error());[/code]Changeforeach( $employees as $employee )towhile ($employee = mysql_fetch_assoc($res)) Link to comment https://forums.phpfreaks.com/topic/28140-php-and-xml/#findComment-128701 Share on other sites More sharing options...
matty84 Posted November 22, 2006 Author Share Posted November 22, 2006 That generates the correct number of tags for the results but does not populate them...I assumed that 'salary' in [code]$doc->createTextNode( $employee['salary'] ) [/code] would relate to the table field by that name but that doesnt seem to work, any ideas?matt Link to comment https://forums.phpfreaks.com/topic/28140-php-and-xml/#findComment-128725 Share on other sites More sharing options...
Barand Posted November 22, 2006 Share Posted November 22, 2006 I've just set up a test table and ran the codeGot -->[pre] <?xml version="1.0" ?> - <employees>- <employee> <name>aaa</name> <age>21</age> <salary>10000</salary> </employee>- <employee> <name>bbb</name> <age>30</age> <salary>20000</salary> </employee>- <employee> <name>ccc</name> <age>35</age> <salary>30000</salary> </employee> </employees>[/pre] Link to comment https://forums.phpfreaks.com/topic/28140-php-and-xml/#findComment-128783 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.