Generate single XML file based on data from two tables or more

Jemsaftem

New Member
kI1Fx.png
There are two tables, which have a relationship(one-to-many). There are \[code\]Meals\[/code\] table which contains values such as Breakfast(\[code\]id: 1\[/code\]), Brunch(\[code\]id: 2\[/code\]), Lunch(\[code\]id: 3\[/code\]), Tea(\[code\]id: 4\[/code\]), Dinner(\[code\]id: 5\[/code\]) and Supper(\[code\]id: 6\[/code\]). Then there are \[code\]Foods\[/code\] table which contains various foods and each food(i.e. Hamburger) is linked to a particular type of meal(i.e. Lunch) via a foreign key. So, the row \[code\]idfood: 12; idmeal: 3; foodname: "Hamburger"\[/code\] would mean that \[code\]"Hamburger"\[/code\] is of \[code\]Lunch\[/code\] meal type.I created an XML generator file which pulls the contents of the whole \[code\]Food\[/code\] table into an XML file. Problem is I want to translate \[code\]idmeal\[/code\] foreign key values into actual meal names from the \[code\]Meal\[/code\] table for each food element inside XML. The simple way would be to include the foreign key number from \[code\]Food\[/code\] table, but that would be too cryptic(i.e. Hamburger is eaten during 3 time). Instead I wanted to make it so based on whatever the foreign key number is - the XML would insert the right value from the \[code\]Meals\[/code\] tables(\[code\]mealname\[/code\]) into that one XML file with foods so that I could print something like "Hamburger is eaten during Lunch time". GenerateXML.php:\[code\]<?phprequire("includes/credentials.php");$doc = new DOMDocument("1.0");$node = $doc->createElement("foods");$parnode = $doc->appendChild($node);$connection=mysql_connect($host, $usr, $pass);if (!$connection) { die('Not connected : ' . mysql_error());}$db_selected = mysql_select_db($database, $connection);if (!$db_selected) { die ('Can\'t use db : ' . mysql_error());}$query = "SELECT * FROM food";$result = mysql_query($query);if (!$result) { die('Invalid query: ' . mysql_error());}header("Content-type: text/xml");while ($row = @mysql_fetch_assoc($result)){ $node = $doc->createElement("food"); $newnode = $parnode->appendChild($node); $newnode->setAttribute("idfood", $row['idfood']); $newnode->setAttribute("idmeal", $row['idmeal']); $newnode->setAttribute("foodname", $row['foodname']);}echo $xmlfile = $doc->saveXML();?>\[/code\]
 
Back
Top