Forum Moderators: open

Message Too Old, No Replies

Parsing XML using PHP

For an RSS Feed

         

tchallies

11:30 am on May 21, 2005 (gmt 0)

10+ Year Member



I'm a total novice when it comes to XML so I'm just going to post the code that was given me and hope someone can tell me what to do next!

I was given this code to allow a daily RSS feed to pull catechism questions from a database on my server. I took the following code and saved it as catechism.php. It kind of worked - my RSS reader finds it fine, but as I'm sure you can see, it appears unformatted when I browse to catechism.php.

What I'd like to have is a page that will display only the Title and Question and Answer (description field) appear on a page that I can use as in include in my site.

Does that make any sense? And please remember I'm an absolute novice with XML so don't use too many big words!

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">";

header('Content-Type: text/xml');

$link = mysql_connect('localhost', 'user', 'password');

mysql_select_db('database');

$day_of_year = date('md'); #May 19 = '0519'

$qid = mysql_query("select title from daily_catechism where day_of_year = $day_of_year");
$qid2 = mysql_query("select description from daily_catechism where day_of_year = $day_of_year");

$pub = date('r', mktime(0, 1, 0, date('m'), date('d'), date('Y')));

list($title) = mysql_fetch_row($qid);
list($description) = mysql_fetch_row($qid2);

?>

<rss version="2.0">
<channel>
<title>Catechism RSS</title>
<link>http://www.mysite.com/catechism.php</link>
<description>Daily Reformed Catechism</description>
<managingEditor>my@email.com</managingEditor>
<item>
<title><?php print htmlentities($title);?></title>
<description><?php print htmlentities($description);?></description>
<pubDate><?php print $pub;?></pubDate>
</item>
</channel>
</rss>

choster

9:36 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you mean it appears "unformatted" in your web browser? The script you've posted will output an XML file in RSS 2.0 format. Your average web browser is designed to display HTML, not RSS, so it won't have a default way of displaying what you'r esending.

You have a couple of different options: apply a CSS file to the output, i.e. insert the line <?xml-stylesheet href="mystyle.css" type="text/css"?> before <rss version="2.0"> linked to your stylesheet, or create an alternative text/html output marked up using HTML tags instead of RSS tags.