I am a definite beinner and I am having similar problems. I have managed to construct a query to read one table and output the results as an XML file (this even tentatively validates!) (Listing one) Please excuse all the commented out code which remains from the learning process.
Listing 1.
[php]<?php
// info for dbase connectiion
$host = “localhost”;
$user = “";
$pass = "”;
$database = “*******”;
// connec to dbase
$linkID = mysql_connect($host, $user, $pass) or die(“Could not connect to host.”);
mysql_select_db($database, $linkID) or die(“Could not find database.”);
//select info from table
$query1 = “SELECT * FROM jos_properties_products”;
$query2 = “SELECT * FROM jos_properties_images”;
// give back results
$resultID = mysql_query($query2, $linkID) or die(“Data not found.”);
$num = mysql_num_rows($resultID);
//Doctype declaration
/*
*/
//output as XML
$xml_output = “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n”;
$xml_output .= “\n”;
//create XML file
if ($num != 0) {
//set location of XML file
$file= fopen("./XML/kyerofeed.xml", “w”);
$xml_output ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$xml_output .="\r\n";
//while loop looking for results
while ($row = mysql_fetch_array($resultID)) {
//Make sure only published properties listed
if ($row[“published”] == “1”) {
// if statement to look through results and returns ID info
if ($row[“id”]) {
//$xml_output .="\t[id = “” . $row[“id”] . “”]\r\n";
$xml_output .="\t\t" . $row[“id”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";//$xml_output .="\t<id =“0”>\r\n";
//$xml_output .="\t\r\n";
}
// returns date info
if ($row[“refresh_time”]) {
// $xml_output .="\t[refresh_time ="" . $row[“refresh_time”] . “”]\r\n";
$xml_output .="\t\t" . $row[“refresh_time”] . “\r\n”;//$xml_output .="\t</refresh_time>\r\n";
} else {
$xml_output .="\t\t0\r\n";//$xml_output .="\t<refresh_time =“0”>\r\n";
//$xml_output .="\t</refresh_time>\r\n";
}
// returns ref info
if ($row[“ref”]) {
// $xml_output .="\t[ref ="" . $row[“ref”] . “”]\r\n";
$xml_output .="\t\t" . $row[“ref”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t<ref =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns price info
if ($row[“price”]) {
//$xml_output .="\t[price ="" . $row[“price”] . “”]\r\n";
$xml_output .="\t\t" . $row[“price”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<price =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns sale info
if ($row[“available”]) {
// $xml_output .="\t[available ="" . $row[“available”] . “”]\r\n";
$xml_output .="\t\tSale\r\n";//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t[available =“Sale”]\r\n";
//$xml_output .="\t\r\n"; */
}
// returns town info
if ($row[“locality”]) {
//$xml_output .="\t[locality ="" . $row[“locality”] . “”]\r\n";
$xml_output .="\t\t" . $row[“locality”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t<locality =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns province info
if ($row[“province”]) {
//$xml_output .="\t[province ="" . $row[“province”] . “”]\r\n";
$xml_output .="\t\t" . $row[“province”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<province =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns location info
if ($row[“address”]) {
//$xml_output .="\t[address ="" . $row[“address”] . “”]\r\n";
$xml_output .="\t\t" . $row[“address”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<address =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns bedrooms info
if ($row[“bedrooms”]) {
// $xml_output .="\t[bedrooms ="" . $row[“bedrooms”] . “”]\r\n";
$xml_output .="\t\t" . $row[“bedrooms”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t<bedrooms =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns bathrooms info
if ($row[“bathrooms”]) {
// $xml_output .="\t[bathrooms ="" . $row[“bathrooms”] . “”]\r\n";
$xml_output .="\t\t" . $row[“bathrooms”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<bathrooms =“0”>\r\n";
//$xml_output .="\t\r\n";*/
}
// returns type info
if ($row[“type”]) {
//$xml_output .="\t[type ="" . $row[“type”] . “”]\r\n";
$xml_output .="\t\t" . $row[“type”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<type =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns garage info
if ($row[“garage”]) {
//$xml_output .="\t[garage ="" . $row[“garage”] . “”]\r\n";
$xml_output .="\t\t1\r\n";//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t<garage =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
// returns pool info
if ($row[“extra_16”]) {
$xml_output .="\t[extra_16 ="" . $row[“extra_16”] . “”]\r\n";
// $xml_output .="\t\t" . $row[“extra_16”] . “\r\n”;//$xml_output .="\t</extra_16>\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<extra_16 =“0”>\r\n";
//$xml_output .="\t</extra_16>\r\n"; */
}
// returns built area info
if ($row[“covered_area”]) {
// $xml_output .="\t[covered_area ="" . $row[“covered_area”] . “”]\r\n";
$xml_output .="\t\t" . $row[“covered_area”] . “\r\n”;//$xml_output .="\t</covered_area>\r\n";
} else {
$xml_output .="\t\t0\r\n";/*$xml_output .="\t<covered_area =“0”>\r\n";
//$xml_output .="\t</covered_area>\r\n"; */
}
// returns plot size info
if ($row[“area”]) {
//$xml_output .="\t[area ="" . $row[“area”] . “”]\r\n";
$xml_output .="\t\t" . $row[“area”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n";/* $xml_output .="\t<area =“0”>\r\n";
//$xml_output .="\t\r\n"; */
}
}
}
$xml_output .="";
//write XML doc
fwrite($file, $xml_output);
//close XML doc
fclose($file);
//print results with hyperlink to link to results
echo “The Kyero XML feed has been updated.\r\n”;
//<a href=\XML"results.xml">View the XML.";
} else {
echo “No Records found”;
}
//print results
//echo $xml_output;
?>
[/php]
I tried several options for adding in data from another table by joining, for example:
[php]//select info from table
$query = “SELECT * FROM jos_properties_products, jos_properties_images WHERE jos_properties_images.parent=jos_properties_products.id” or die (“Could not join tables like this”);
[/php]
and this:
[php]//select info from table
$query = “SELECT * FROM jos_properties_products, jos_properties_images ON jos_properties_images.parent=jos_properties_products.id” or die (“Could not join tables like this”);
[/php]
and although it didn’t exit at the “or die”, the data from the second table (jos_properties_images) do not appear to have been appended.
I need to add in a couple more tables, but I am sure that when I can get the syntax right I will be OK.
I would also appreciate comment on the following, which I hope will match up a number of entries on the second table to just one ID on the first.
Listing 2.
[php]for ($count = 1; $count = 10; $count += 1) {
//read a picture file
while ($row[“parent”] = $[“id”]) {
//$xml_output .="\t[area ="" . $row[“area”] . “”]\r\n";
$xml_output .="\t\t" . $row[“rout”] . “\r\n”;//$xml_output .="\t\r\n";
} else {
$xml_output .="\t\t0\r\n"; $xml_output .="\t<area =“0”>\r\n" or die (“Invalid code”);
//$xml_output .="\t\r\n";
}
[/php]
Any help greatly appreciated. This is my first attempt at writing php script, I am almost 60 years old and the grey cells aren’t as nimble as they used to be.
Many thanks
Dave
}