Categories
General

Assemble a Dynamic RSS Feed With PHP and MySQL

Assemble a Dynamic RSS Feed With PHP and MySQL

NOTE: Wherever you see [url] please embed your sites url instead of [url].

Wherever I look I can discover instructional exercises yet once in a while are they finished. The instructional exercise I am echo info  going to compose is intended to be a finished instructional exercise. Sure you can add more alternatives to the RSS record itself yet what I mean by complete is that it will work for anybody if the principals that are sketched out here are followed. Immediately lets hop directly into assemble a dynamic RSS channel utilizing PHP and MySQL

To start our PHP based RSS channel we have to do one seemingly insignificant detail first. Investigate your “.htaccess” document which is commonly situated in the root index of web server. The document is utilized for doing URL changes, 301 sidetracks and so forth. Each Apache server ought to have it so search for it. On the off chance that you are utilizing dreamweaver a simple to get the document is to make it in the site administrator, for example right-click and make another document. Rename the record “.htaccess” and afterward right snap on your recently made document and snap “Get”. Again this is just for clients of Dreamweaver. I am making this more confused than it ought to be. Glance for.htaccess in your web server root. Enough said

When we have our.htaccess record discovered we have to roll out an improvement. Since our RSS document will be of the PHP augmentation for example rss.php and not of the xml type. We need.htaccess to realize that documents with the type.xml shoule be deciphered as.php records. To do this we enter the accompanying in our.htaccess document:

AddType application/x-httpd-php.xml

With our.htaccess record all set we currently need to start composing the PHP for our RSS channel. Our document will be broken into four segments. The first is the header which tells the program that the record is of type XML. The subsequent area is the leader of our RSS document. Its beginning and end that you could make static about the channel, for example the essential data identified with the RSS channel. Our third areas is the place it gets clever. We make a database association and use it to make our RSS channel. We at that point need to circle through every one of the things we need in our database and yield them as xml. At last our fourth part is all the yield that is required to close the channel. There is little to it beside a couple of reverberation orders however it is a different segment in its own minor manner.

First we have to make another php record, I decided to name mine rss.php, you can name yours whatever you like. When we have our document open we start with our first segment:

/Set XML header for program

header(‘Content-type: content/xml’);

?>

Before we would anything we like to make an impression on the program that advises it that it is managing a XML document. In the event that we didn’t pass this data to the program our dynamic RSS channel would not work. As basic as it sounds that is it for our initial segment of our RSS document.

In our next area we have to start making the structure of our XML document. There are hardly any approaches to do this. One is with the reverberation order. The other is using factors. I have utilized both and by and by incline toward the variable strategy since it appears to be less difficult in my brain. Along these lines that is the technique I will illustrate. Here is our code (it incorporates the code from our first area):

/Set XML header for program

header(‘Content-type: content/xml’);

/Create the heading/channel information for RSS Feed

$output = ”;

$output.= ”;

$output.= ”;

$output.= ‘Your RSS Feed Description’;

$output.= ‘ [url]’;

?>

Here is the thing that we did with the subsequent segment. First we made a variable $output. We set it equivalent to. Anyway once we have the worth set we don’t overwrite it. We rather utilize the administrator “.=” which implies basically add to the present worth. So for instance on the off chance that we said that $some-variable = “a string”. We at that point utilized our “.=” administrator to add to the worth like so $some-var.= ” like some var”. Next if we somehow happened to reverberate our variable $some-variable it would peruse “a string like some var”. We are taking our variable and including the entirety of the xml labels to it to make one coterminous xml record put away in one variable, $output. One other thing to note is that for the title, depiction and connection labels you should include your destinations data. While I don’t figure anybody would purposefully utilize the data I had between the labels it is anything but difficult to overlook seemingly insignificant details that way, which is the reason I notice it.

For our third segment we get down to the meat and bones of our php produced RSS page. What we will do is associate with a MySQL database and get all the appropriate data we need. At that point we will make individual xml things for every one of the new passage or article that we have. This is totally done when the client gets to the RSS page. Not previously. The page is dynamic not static. For expectations and purposes we don’t have a RSS page until somebody gets to it. Presently lets get to the code:

/Set XML header for program

header(‘Content-type: content/xml’);

/Create the heading/channel information for RSS Feed

$output = ”;

$output.= ”;

$output.= ”;

$output.= ‘Your RSS Feed Description’;

$output.= ‘ [url]’;

/Individual Items of our RSS Feed

/Connect to a database and show each new thing in our feed

/Connect to DB

$host = “localhost”;/Name of host

$user = “cmsuser”;/User name for Database

$pass = “mypass”;/Password for Database

$db = “my_database”;/Name of Database

mysql_connect($host,$user,$pass);

mysql_select_db($db);

/Create SQL Query for our RSS channel

$sql = “SELECT ‘title’, ‘connect’, ‘portrayal’, ‘date’ FROM ‘articles’ORDER BY ‘date’ DESC LIMIT 0, 15”;

$result = mysql_query($sql) or pass on (“Query couldn’t be executed”);

/Create Loop for the individual components in the RSS thing segment

while ($row = mysql_fetch_array($result))

{

$output.= ”;

$output.= ”;

$output.= ‘ ‘.$row[‘link’].”;

$output.= ”.$row[‘description’].”;

$output.= ‘

‘.$row[‘date’].”;

$output.= ”;

}

?>

Presently an incredible arrangement has happened in this segment so allowed me to attempt to clarify everything plainly. First lets start with the remark “Associate with DB”. Here we have to interface with a database. Ordinarily I compose a capacity prior on and essentially call it when I need to associate with a database and run a question. Anyway I can not expect that you have just kept in touch with one so we will experience keeping in touch with one together. First we characterize factors that will house the important data for the “mysql_connect” and “mysql_select_db” capacities. The data we have to store is our hostname, by and large its “localhost”, our client name for the database, our secret key, and the name of the database. When we have that spared we use it in the capacity “mysql_connect” which is utilized to make an association a mysql database, when we set up the association we at that point need to choose a database with the “mysql_select_db” explanation. Since we have associated with our database lets analyze how we approach getting the data we need.

Leave a Reply

Your email address will not be published. Required fields are marked *