Popular Posts

Tuesday, 19 August 2014

Building rss feed script using php and mysql

mysql.
First of all here it is the source that is going to be shown as plain text
<?xml version="1.0" encoding="ISO-8859-1" ?>



<rss version="2.0">



<channel>
  <title>Rss feeds on php and mysql</title>
  <link>http://www.thecodertips.com</link>
  <description>Web developement tutorials</description>
  <item>
    <title>Using blogger tags to beautify template and creating plugins</title>
    <link>http://www.thecodertips.com/2013/03/using-blogger-tags-to-beautify-template.html</link>
    <description>Blogger tips</description>
  </item>
  <item>
    <title>Php tutorial, Login script with Jquery</title>
    <link>http://www.thecodertips.com/2012/11/php-tutorial-login-script-with-jquery.html</link>
    <description>Php script</description>
  </item>
</channel>

</rss>



If you don't know how to use phpmyadmin please read how to post to database
So to explain
This is what we are going to build



Go to phpmyadmin, create a new table, name it 'posts' and create the fields:

Save it and then go to insert(phhmyadmin), and write 2 new posts, fill in all the fields except id.
Now create a php file called config.php, this file makes connection to database


<?php
mysql_connect("localhost", "root", "") or die("could not connect"); // host, user, password
mysql_select_db("database") or die("could not select database"); // database name
?>


Now create another php file, name it for ex rss.php


<?php
header("Content-Type: application/xml; charset=ISO-8859-1");
include("config.php");
?>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">

<channel>
  <title>Rss feeds on php and mysql</title>
  <link>http://www.thecodertips.com</link>
  <description>Web developement tutorials</description>
<?php
$query = mysql_query("SELECT title, link, description FROM posts");
while($row = mysql_fetch_array($query)){
echo '<item>
    <title>'.$row['title'].'</title>
    <link>'.$row['link'].'</link>
    <description>'.$row['description'].'</description>
  </item>';
}
echo '
</channel></rss>';
?>



You are done, for better preview view the php file in mozilla firefox, you can also use mod rewrite to change extension form .php to .xml or .rss

Note:-

Edit! I forgot adding the header to rss.php so just paste this code in line 2
header("Content-Type: application/xml; charset=ISO-8859-1");



..............:)

No comments:

Post a Comment

Magento: How to get last order id

There are many ways to get last order id:   1. From checkout session: $lastOrderId = Mage::getSingleton('checkout/session'...