Creating XML Feeds Using PHP?

Cathy V

New member
I'm trying to dynamically create an XML file by using the PHP write function

1. The file is named "feeds.xml" and it's empty
2. Here is my PHP code

<?
//connection

Connection to database

//General query

$query="SELECT * FROM `tablename` ORDER BY `Date added` DESC LIMIT 0,20";
$data=mysql_query($query);

$top=
" "<"."?"."xml version=\"1.0\""."?".">"."<br>
"<rss version=\"2.0\"><br>

<channel><br>

<title>Recently added stores and businesses</title><br>" ";




$i=1;
while($row=mysql_fetch_assoc($data))
{
$a=$row['Company'];
$b=$row['Website'];
$c=$row['Products'];
$d=substr($c, 0,200);

$middle="

<item><br>
<title><? echo $a ?></title><br>
<description><? echo $d ?></description><br>
<link><? echo $b ?></link><br>
</item><br>";

$i++;

}

$bottom="
</channel><br>

</rss>";




//write function

$file = "feeds.xml";
$open = fopen($file, 'w') or die("can't open file");
$rss=$top.$middle.$bottom;
fwrite($open, $rss);

fclose($open);

//end write function

mysql_close();


?>

It won't work because I know the code is bad. Your help please!!!
1. I have changed the code. It looks like this:

<?
//connection

Connection to database

//General query

$query="SELECT * FROM `tablename` ORDER BY `Date added` DESC LIMIT 0,20";
$data=mysql_query($query);

$xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
$xml.="<rss version=\"2.0\">\r\n";
$xml.="<channel>\r\n";
$xml.="<title>Recently added stores and businesses</title>\r\n";

$i=1;
while($row=mysql_fetch_assoc($data))
{
$a=$row['Company'];
$b=$row['Website'];
$c=$row['Products'];
$d=substr($c, 0,200);

$xml.="<item>\r\n";
$xml.="<title>".$a."</title>\r\n";
$xml.="<description>".$d."</description>\r\n";
$xml.="<link>".$b."</link>\r\n";
$xml.="</item>\r\n";
$i++;
}
$xml.="</channel>\r\n";
$xml.="</rss>";

$file ="feed.xml";
$open = fopen($file, 'w') or die("can't open file");
fwrite($open, $xml);

fclose($open);


mysql_close();

2. The "feed.xml" file looks properly created. But the browser won't display it
 
Back
Top