Dynamic RSS Feed Request

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Dynamic RSS Feed Request

Post by techkid »

Hello,

I'm working on a news script! Can anyone help me to create Dynamic RSS Feeds? I tried but couldn't, I'm pretty new to PHP. So If possible can anyone program a RSS Feed page for me.

Here is the template for RSS Feeds.

Code: Select all

<rss version='2.0'  xmlns:atom='http://www.w3.org/2005/Atom'>
<channel>
<title>Title</title>
<description>Description</description>
<link>Link</link>
<atom:link href='http://something.xml' rel='self' type='application/rss+xml' />
<item>
<title>Title of the post</title>
<pubDate>Date published</pubDate>
<link>Link</link>
<guid>Link</guid>
</item>
<item>
</channel>
</rss>
Here is how my database is designed:
Image

Link eg:
http://localhost/tsss/blog.php?act=view&id=1

I don't need to display news on RSS Feed Page, Just titles, link & date

Thank you,
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Dynamic RSS Feed Request

Post by AlanG »

This should do the trick. (Apologies for any syntax errors. Haven't tested it.) :)

Code: Select all

<?php
$db = new mysqli('localhost','root','','mydb'); // Create a database connection
 
// Fetch the data from the database
$query = "SELECT * FROM mydb";
$result = $db->query($query);
$rows = $result->num_rows;
 
header('Content-type: text/xml'); // Tells the browser that the content is xml
 
echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'."\n";
echo '<rss version="2.0">'."\n";
echo '<channel>'."\n";
 
while($rows > 0) {
    $item = $result->fetch_assoc();
 
    echo '<title>'.$item['title'].'</title>'."\n";
    echo '<link>http://localhost/tsss/blog.php?act=view&id='.{$item['index']}.'</item>'."\n";
    echo '<description>'.$item['news'].'</description>'."\n";
    echo '<pubDate>'.date('Y-m-d',$item['date']).'</pubDate>'."\n";
 
    $rows--;
}
 
echo '</channel>'."\n";
?>
User avatar
techkid
Forum Commoner
Posts: 54
Joined: Sat Sep 05, 2009 11:18 pm
Location: Maldives

Re: Dynamic RSS Feed Request

Post by techkid »

Error
Parse error: syntax error, unexpected '{' in C:\wamp\www\techmv.net\rss.php on line 19

so i dropped { } from {$item['index']}

and got another error
XML Parsing Error: not well-formed
Location: http://localhost/techmv.net/rss.php
Line Number 5, Column 49:<link>http://localhost/tsss/blog.php?act=view&id=1</item>
------------------------------------------------^
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Dynamic RSS Feed Request

Post by AlanG »

Sorry for delay. Was moving house. =P Yeah my bad, just remove both { and }. There's no need for them. Don't know what I was thinking. :)
Post Reply