Write XML File using PHP/MySQL

admin

Administrator
Staff member
Hi,

I've written an xml file to write data to a classified listing I've set up on my site that will be queried daily by another site to update and retrieve new data and am having trouble in the following areas.

1. Looping new listings
2. Getting proper image corresponding to the current listing from another table in the db.
3. Selecting between current and not current listings.
4. After current listings are selected sorting between type of listing.
5. Error when generating xml code stating:
XML Parsing Error: xml declaration not at start of external entity
Location: <!-- m --><a class="postlink" href="http://www.fooBar.com">http://www.fooBar.com</a><!-- m -->
Line Number 3, Column 1:<?xml version="1.0" encoding="utf-8"?>
^

The logic for points 3 and 4 has not yet been coded as I'm not sure how and where to place it.

All variables are saved in a separate file and necessary files such as database connections are included

Here is my code:

<?php
header('Content-type: text/xml');

$_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

//db connections
$conn = DBConnect();
$sql = "SELECT * FROM props WHERE rec_id != ''";
$result1 = mysql_query($sql);
@$row = mysql_fetch_array($result1);
$sql2 = "SELECT file FROM pics WHERE rec_id = '$id'";
$result2 = mysql_query($sql2);
@$row2 = mysql_fetch_array($result2);

// php loop to get information from properties table and parse into the feed
for ($i = 0; $i < mysql_num_rows($result1); $i++) {

$_xml .= "<property>\n";
$_xml .= "<location>\n";
$_xml .= "<street-address>$propAddy</street-address>\n";
$_xml .= "<city-name>$propCity</city-name>\n";
$_xml .= "<county></county>\n";
$_xml .= "<state-code>$propSt</state-code>\n";
$_xml .= "<zipcode>$propZip</zipcode>\n";
$_xml .= "<display-address>Yes</display-address>\n";
$_xml .= "<location></location>\n";
$_xml .= "<details></details>\n";
$_xml .= "<price>$propPrice</price>\n";
$_xml .= "<year-built>$propYear</year-built>\n";
$_xml .= "<num-bedrooms>$propBed</num-bedrooms>\n";
$_xml .= "<num-bathrooms>$propBath</num-bathroos>\n";
$_xml .= "<apartment-number></apartment-number>\n";
$_xml .= "<lot-size></lot-size>\n";
$_xml .= "<square-feet></square-feet>\n";
$_xml .= "<date-listed></date-listed>\n";
$_xml .= "<date-sold></date-sold>\n";
$_xml .= "<description>$propDescription</description>\n";
$_xml .= "<mlsId></mlsId>\n";
$_xml .= "<details></details>\n";
$_xml .= "<agent></agent>\n";
$_xml .= "<broker-name></broker-name>\n";
$_xml .= "<agent-name></agent-name>\n";
$_xml .= "<phone-number></phone-number>\n";
$_xml .= "<email></email>\n";
$_xml .= "</agent>\n";
$_xml .= "<status>$propCategory</status>\n";
$_xml .= "<property-type></property-type>\n";
$_xml .= "<site>\n";
$_xml .= "<site-url>http://www.foobar.com</site-url>\n";
$_xml .= "<site-name>FooBar</site-name>\n";
$_xml .= "</site>\n";
$_xml .= "<pictures>\n";
$_xml .= "<picture>\n";
$_xml .= "<picture-url>$propImg</picture-url>\n";
$_xml .= "</picture>\n";
$_xml .= "</pictures>\n";
$_xml .= "<landing-page>\n";
$_xml .= "<lp-url>$propertyLink</lp-url>\n";
$_xml .= "<has-virtual-tour></has-virtual-tour>\n";
$_xml .= "</landing-page>\n";
$_xml .= "</property>\n";
}

$_xml .= "</properties>";

print $_xml;
 
Back
Top