View Full Version : how PHP print out RSS?

01-13-2010, 02:19 PM
How PHP able to print out a rss html page like this:

this is my code below , asking php to print out a xml data , how can it into a rss like the above one?

echo "<?xml version=\"1.0\"?>\n";
echo "<data>\n";

while($line = mysql_fetch_assoc($results)) {
echo "<sndata sn=\"" . $line["serial_no"]."\">\n";
echo "<companyname>" . $line["company_name"] . "</companyname>\n";
echo "<numberid>" . $line["number_id"] . "</numberid>\n";


01-13-2010, 03:03 PM
Take a look at http://www.supadupawebdesign.co.uk/tutorials/PHP-1/Easy-Dynamic-RSS-Feed-in-PHPMySQL-11 (of course, it gets data from mysql DB, but you may easily modify it)

01-13-2010, 03:48 PM
Firstly use single quotes to write your xml data structure and also sprintf your values into a template for the looping process. It makes your code so much cleaner to read. Example

$output = '<?xml version="1.0"?>

$datatemplate = '
<sndata sn="%s">

while($line = mysql_fetch_assoc($results)) {
$output .= sprintf($datatemplate, $line['serial_no'], $line['company_name'], $line['number_id']);

$output .= '

header('Content-Type: text/xml');
echo $output;Note that I also used the text/xml header to signify to your browser that this is an xml document

01-13-2010, 05:28 PM
hey thanks experts , it helps +rep and everything !