Monday, August 20, 2012

Create a dynamic RSS feeds with PHP and MySQL


NOTE: Whenever you see [url] enter your website url instead of [url].

Everywhere I look I can find tutorials, but rarely are they complete. The tutorial I am going to want to be writing a full tutorial. Sure you can add more options to the RSS file itself, but what I mean is that the complete work for anyone, if the principles which are described herein. Without further ado lets you jump right in building a dynamic RSS feeds with PHP and MySQL

To begin our RSS feed based on PHP we need to do a little thing first. Take a look at yours. "Htaccess" file which is usually the root of the web server. And 'the file that is used to rewrite URLs, 301 redirects etc. Each server should have Apache so look for him. If you use Dreamweaver's easy to get a file is to create the site manager, that is, right click and create a new file. Rename the file ". Htaccess" and then right-click the file you just created and click on "Get". Even in this case is only for users of Dreamweaver. I'm making this more complicated than it should be. Watch for.htaccess in the root of your web server. Enough said

Once we found our.htaccess file we need to make a change. Because our RSS file will be the PHP extension rss.php that is to say, and not of type xml. Need.htaccess We know that files with the file shoule be interpreted type.xml as.php. To do this we enter the following in your our.htaccess:

AddType application / x-httpd-php.xml

With files our.htaccess ready to go now we have to start writing PHP for our RSS feed. Our file will be divided into four sections. The first is the header that tells the browser that the file is of type XML. The second section is the head of our RSS file. Its everything you could do static channel, ie the basic information relating to the RSS feed. Our third section is where it gets nifty. We create a database connection and use it to create our own RSS feed. We then need to scroll through each of the elements that we want in our database and output as xml. Finally, the fourth part is output at all that is needed to close the channel. There is little to be part of a command to a few, but it is a separate section in its own way lower.

First we create a new php file, I chose to name my rss.php, your name can be whatever you want. Once we have our file open we begin with our first section:



Before you do anything we want to send a message to the browser informing him that it is an XML file. If your browser does not transmit this information to our RSS feed dynamic would not work. As simple as it seems that for our first part of our RSS file.

In our next section we need to start building the structure of our XML file. There are a few ways to do it. One is with the echo command. The other is through the use of variables. I've used both and personally prefer the variable method since it seems easier in my mind. Therefore, this is the method that will demonstrate. Here is our code (which includes the code of our first section):

/ / Set XML header for the browser

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

/ / Create the channel information of the voice / to the RSS Feed

$ Output ='';

$ Output =''.;

$ Output =''.;

$ Output = 'Your description RSS Feed'.;

$ Output = '[url]'.;

?>

Here's what we did with the second section. First we created a variable $ output. We set equal to. However, once we set the value will not overwrite. We instead use the operator. "=" That just adds to the current value. So, for example, what if we said-that $ var = "a string". We then used our own. "=" Operator to add value in this way some $ '-var. = "Like a var". Next if we were to echo what our variable $ variable-that would be read "as a string var". We are taking our variable and the addition of all the XML tags to create an XML document stored in a contiguous variable $ output. Another thing to note is that for the tag title, description, and link you should add your sites information. While I do not think anyone would intentionally use the information that I had between the tags is easy to forget the little things like that, that's why talk about it.

For our third section, we put the meat and bones of our php generated RSS. What we are going to do is connect to a MySQL database and make all relevant information we need. Then we are going to create individual XML elements for each of the new item or article that we have. All this is executed when the user accesses the page RSS. Not before. The page is dynamic, not static. For effects do not have a RSS page until someone accesses it. Now lets get to the code:

/ / Set XML header for the browser

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

/ / Create the channel information of the voice / to the RSS Feed

$ Output ='';

$ Output =''.;

$ Output =''.;

$ Output = 'Your description RSS Feed'.;

$ Output = '[url]'.;

Entries / / of our individual RSS Feed

/ / Connecting to a database and e see each new element in our diet

/ / Connect to DB

$ Host = "localhost" / / hostname

$ User = "cmsuser", / / ​​Username for the database

$ Pass = "mypass" / / password for the database

$ Db = "my_database" Name / / Database

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

mysql_select_db ($ db);

/ / Create SQL query to our RSS feed

$ Sql ​​= "SELECT` title `,` link `,` description `,` date `` `` FROM articles ORDER BY date `DESC LIMIT 0, 15";

$ Result = mysql_query ($ sql) or die ("Query could not be executed");

/ / Create loop for individual items in the RSS item

while ($ row = mysql_fetch_array ($ result))

{

$ Output =''.;

$ Output =''.;

$ Output = $ row''. ['Link'].''.;

$ Output ='' $ row ['description'] ...'';

$ Output. = '

'. $ Row ['date'].'';

$ Output =''.;

}

?>

Now much has occurred in this section so as to leave groped to explain everything clearly. First lets start with the comment "Connecting to DB". Here we need to connect to a database. Normally I write a function previously and simply call him when I connect to a database and query. However I can not think that you have already written one so we will write together. First of all to define the variables that will house the necessary information for the "mysql_connect" and "mysql_select_db" functions. The information that we need to conserve our hostname, usually its "localhost", our database user name, password and database name. Once we have saved that we use in "mysql_connect" function that is used to establish a connection to a mysql database, once the connection we then need to select a database with the statement "mysql_select_db". Now that we have linked to our database allows you to examine how we can do to get the information we need.

Now that we are connected, you must run a query to get the information we need. For example, I made some assumptions that the first is the name of the database that contains four articles and columns: `title`, `link`, `description`, `date` and appointed as such. I also limited the result to 16, using the statement "... 0.15 LIMIT", which means showing only the rows from 0 to 15. You can set to whatever you want or you can remove it completely and have no limit to the number of entries in your RSS feed. It's fine for small sites, terrible for the older children. Use your discretion here. Now that we have built the query I want to point out one thing. Usually you see people using "SELECT * ..." statement during the execution of the query. Not only do I think his bad habit, but because they have more information that you need, it takes longer and makes your site perform only ever so slightly slower. Therefore I recommend that when you form the SQL query that implicitly indicates the fields that you want instead of using a "SELECT * ..." declaration. Now that we have our query we need to run it using the "mysql_query" command and pass the results into a variable, cleverly known as $ result. If you notice that after our "mysql_query ($ sql)" I statement "or die (...)". What that statement is not whether there is an error kills the query and then terminates the function echoes any error message instead of square brackets. Handy for figuring out where things can go wrong.

So far we have linked to a database query and output the results into a variable $ result. Now we need to put all this in a small RSS item ordered. To do this we need to create a cycle, that our cycle will go through our query row by row and extract information from each row and do what we want with it. In this case we want to save it. To do this we created a while loop that reads the lines while there are virtually remained in our outcome variable. We must do everything that the code between the brackets {... }. There are other ways for this cycle were formed, but for now the most direct method is the one I listed. Now that we have our file into a variable $ row we need to add them to our XML file. To do this we use our good friend. "=" And at the end add the information for each element you want to create. There are many other tags you can use with your RSS feed. I just chose to use the "title", "link", "description", "pubDate", since that was all I felt like I needed, and this is not an article on the structure of RSS, but how to generate dynamic.

We have completed three of our four steps. Since we created all of our articles, we have done previously with the while loop to iterate through each result in the database and add it to our variable with the appropriate tags, we finish our file and display to the user. To do this we use the following code:

/ / Set XML header for the browser

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

/ / Create the channel information of the voice / to the RSS Feed

$ Output ='';

$ Output =''.;

$ Output =''.;

$ Output = 'Your description RSS Feed'.;

$ Output = '[url]'.;

Entries / / of our individual RSS Feed

/ / Connecting to a database and e see each new element in our diet

/ / Connect to DB

$ Host = "localhost" / / hostname

$ User = "cmsuser", / / ​​Username for the database

$ Pass = "mypass" / / password for the database

$ Db = "my_database" Name / / Database

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

mysql_select_db ($ db);

/ / Create SQL query to our RSS feed

$ Sql ​​= "SELECT` title `,` link `,` description `,` date `` `` FROM articles ORDER BY date `DESC LIMIT 0, 15";

$ Result = mysql_query ($ sql) or die ("Query could not be executed");

/ / Create loop for individual items in the RSS item

while ($ row = mysql_fetch_array ($ result))

{

$ Output =''.;

$ Output =''.;

$ Output = $ row''. ['Link'].''.;

$ Output ='' $ row ['description'] ...'';

$ Output. = '

'. $ Row ['date'].'';

$ Output =''.;

}

/ / Close channel RSS

$ Output =''.;

$ Output =''.;

/ / Display output in the browser

echo $ output;

?>

Here is our complete code in all its glory, all that we have added two statements is that append our variable with the following tag "" and "" that closes our channel and RSS tag, respectively, we have previously opened . After that we need to display the information so that the web browser can view it. To do this, simply use the command echo and echo the $ output variable we used to store all the information above. Now you should be able to see what I meant earlier in this article, when I suggested that you could use echo instead of adding information to a variable, but that's besides the point. What matters now is that you have a fully functional RSS feed that you should not play again unless its to add more information. I hope you enjoy and can put to good use!

No comments:

Post a Comment