2014-11-15



Construct a Dynamic RSS Feed With PHP and MySQL

NOTE: Anywhere you see [url] please insert your internet sites url in place of [url].

All over I seem to be I will be able to in finding tutorials however infrequently are they complete. The academic I’m about to write down is supposed to be a whole tutorial. Positive you can add extra choices to the RSS file itself but what I imply through complete is that it will work for someone if the principals which can be outlined here are followed. Without additional prolong lets jump proper into build a dynamic RSS feed the usage of PHP and MySQL.

To start our PHP based RSS feed we wish to do one little thing first. Take a look at your ".htaccess" file which is most often located within the root directory of net server. It’s the file that’s used for doing URL rewrites, 301 redirects etc. Each Apache server should have it so look for it. If you are the usage of dreamweaver an easy to get the file is to create it within the web page manager, i.e. Proper-click and create a new file. Rename the file ".htaccess" after which right click on on your newly created file and click "Get". Again this is just for users of Dreamweaver. I am making this more sophisticated than it should be. Search for.htaccess for your web server root. Sufficient mentioned.

Once we’ve our.htaccess file found we need to make a metamorphosis. Due to the fact our RSS file will be of the PHP extension i.e. Rss.php and not of the xml type. We’d like.htaccess to understand that information with the kind.xml shoule be interpreted as.php recordsdata. To do that we enter the next in our.htaccess file:

AddType application/x-httpd-php.xml.

With our.htaccess file ready to move we now need to commence writing the PHP for our RSS feed. Our file will likely be damaged into four sections. The primary is the header which tells the browser that the file is of type XML. The 2d section is the head of our RSS file. Its the whole lot that you want to make static concerning the channel, i.e. The elemental info related to the RSS feed. Our 1/3 sections is the place it gets nifty. We create a database connection and use it to create our RSS feed. We then want to loop via each of the gadgets we want in our database and output them as xml. Eventually our fourth part is the entire output that’s required to shut the channel. There is little to it apart from just a few echo commands however it’s a separate section in its personal minor manner.

First we need to create a brand new php file, I chose to name mine rss.php, that you would be able to identify yours no matter you love. As soon as we have now our file open we begin with our first part:

//Set XML header for browser.

Header('Content material-type: textual content/xml');

?>

Before we do anything else we wish to ship a message to the browser that informs it that it is coping with an XML file. If we did not move this data to the browser our dynamic RSS feed would not work. As simple as it sounds that’s it for our first part of our RSS file.

In our subsequent section we wish to commence developing the structure of our XML file. There are few ways to do this. One is with the echo command. The opposite is thru using variables. I have used both and for my part favor the variable method since it appears simpler in my thoughts. Due to this fact that is the approach I’ll exhibit. Here is our code (it includes the code from our first section):

//Set XML header for browser.

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

//Create the heading/channel information for RSS Feed.

$output = '';

$output.= '';

$output.= '';

$output.= 'Your RSS Feed Description';

$output.= ' [url]';

?>

Here is what we did with the 2d section. First we created a variable $output. We set it equal to. However once we’ve the value set we do not overwrite it. We as an alternative use the operator ".=" because of this merely add to the present price. So as an instance if we mentioned that $some-variable = "a string". We then used our ".=" operator to add to the worth like so $some-var.= " like some var". Subsequent if we had been to echo our variable $some-variable it will learn "a string like some var". We are taking our variable and adding all the xml tags to it to create one contiguous xml report saved in a single variable, $output. One thing more to note is that for the title, description and hyperlink tags you will have to add on your websites knowledge. Whereas I don't think any person would intentionally use the info I had between the tags it is simple to disregard little issues like that, which is why I point out it.

For our third part we get right down to the meat and bones of our php generated RSS page. What we are going to do is connect to a MySQL database and take hold of all of the pertinent data we’d like. Then we’re going to create person xml gadgets for each of the new entry or article that we have now. This is all carried out when the person accesses the RSS page. Not earlier than. The page is dynamic now not static. For intents and functions we don't have an RSS web page unless any person accesses it. Now lets get to the code:

//Set XML header for browser.

Header('Content-kind: text/xml');

//Create the heading/channel information for RSS Feed.

$output = '';

$output.= '';

$output.= '';

$output.= 'Your RSS Feed Description';

$output.= ' [url]';

//Individual Objects of our RSS Feed.

//Hook up with a database and and show each and every new merchandise in our feed.

//Connect to DB.

$host = "localhost"; //Title of host.

$user = "cmsuser"; //Person name for Database.

$cross = "mypass"; //Password for Database.

$db = "my_database"; //Name of Database.

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

Mysql_select_db($db);

//Create SQL Question for our RSS feed.

$sql = "SELECT `title`, `hyperlink`, `description`, `date` FROM `articles`ORDER BY `date` DESC LIMIT 0, 15";

$consequence = mysql_query($sql) or die ("Question couldn't be done");

//Create Loop for the individual components within the RSS item part.

Whereas ($row = mysql_fetch_array($end result))

$output.= '';

$output.= '';

$output.= ' '.$row['link'].'';

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

$output.= '

'.$row['date'].'';

$output.= '';

?>

Now a great deal has befell on this section so let me attempt to give an explanation for the whole thing clearly. First lets begin with the comment "Connect to DB". Here we wish to connect to a database. Usually I write a function past on and easily name it when I want to hook up with a database and run a question. On the other hand I will no longer suppose that you’ve already written one so we can go through writing one together. First we define variables to be able to home the necessary information for the "mysql_connect" and "mysql_select_db" functions. The guidelines we wish to retailer is our hostname, most often its "localhost", our user title for the database, our password, and the title of the database. Once we’ve got that saved we use it in the operate "mysql_connect" which is used to make a connection a mysql database, once we set up the connection we then need to select a database with the "mysql_select_db" remark. Now that now we have linked to our database lets examine how we go about getting the guidelines we want.

Now that we are related we must run a question to get the tips we need. For the example I’ve made just a few assumptions the first being that the identify of the database is articles and that it accommodates the four columns: `title`, `hyperlink`, `description`, `date` and that they’re named as such. I also limited the outcome to sixteen, by means of the use of the remark "…LIMIT 0,15" this means that most effective exhibit rows 0 to fifteen. That you may set it to whatever you want to or that you may cast off it completely and don’t have any restrict on the collection of entries in your RSS feed. oK for small sites, horrific for large ones. Use your discretion here. Now that we have the question built I want to point out one thing. In most cases you see folks using the "SELECT *…" remark when they run queries. Not handiest do I believe its dangerous practice however why get extra information that you just need, it takes longer and makes your website online run just ever so quite slower. Therefore I like to recommend that whilst you kind your sql queries you implicitly state which fields you want rather than the usage of a "SELECT *…" statement. Now that we have our query we want run it by way of the use of the command "mysql_query" and go the consequences right into a variable, cleverly referred to as $end result. If you discover that after our "mysql_query($sql)" remark I’ve "or die(…)". What that commentary does is if there is an error it kills the question and terminates the function then echoes no matter error message you location within the brackets. At hand for determining where issues may fit unsuitable.

Thus far we have now related to a database and run a question outputting the results into a variable, $result. Now we wish to put this all into a neat little RSS item. To do this we need to create a loop, What our loop will do is undergo our query row via row and pull the ideas from each and every row and do whatever we wish with it. In this case we wish to store it. To do so we create some time loop which principally reads whereas there are rows still left in our consequence variable. We need to do no matter code is between the brackets … . There are different how to have shaped this loop but for now essentially the most direct way is the one I’ve listed. Now that we’ve got our rows in a variable $row we wish to add them to our xml file. To do that we use our just right friend ".="and principally add within the information for every item we wish to create. There are a lot of extra tags you can use together with your RSS feed. I’ve most effective chosen to use the "title", "link", "description", "pubDate" when you consider that that was all I felt like i needed and this is not an article on the structure of RSS but how to generate them dynamically.

We’ve got accomplished three of our four steps. Given that we’ve got all of our objects created, we did it up to now with the whereas loop in order to cycle through every outcome in the database and add it to our variable with the precise tags, we want to finish off our file and show it to the user. To do that we use the next code:

//Set XML header for browser.

Header('Content-kind: text/xml');

//Create the heading/channel information for RSS Feed.

$output = '';

$output.= '';

$output.= '';

$output.= 'Your RSS Feed Description';

$output.= ' [url]';

//Individual Gadgets of our RSS Feed.

//Hook up with a database and and show every new merchandise in our feed.

//Connect with DB.

$host = "localhost"; //Title of host.

$person = "cmsuser"; //User identify for Database.

$cross = "mypass"; //Password for Database.

$db = "my_database"; //Title of Database.

Mysql_connect($host,$user,$move);

Mysql_select_db($db);

//Create SQL Question for our RSS feed.

$sql = "SELECT `title`, `hyperlink`, `description`, `date` FROM `articles`ORDER BY `date` DESC LIMIT 0, 15";

$outcome = mysql_query($sql) or die ("Question couldn't be done");

//Create Loop for the person parts within the RSS item part.

Whereas ($row = mysql_fetch_array($end result))

$output.= '';

$output.= '';

$output.= ' '.$row['link'].'';

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

$output.= '

'.$row['date'].'';

$output.= '';

//Shut RSS channel.

$output.= '';

$output.= '';

//Show output in browser.

Echo $output;

?>

right Here is our accomplished code in all its glory, all we’ve delivered is two more statements that append our variable with the next tags "" and "" which closes our channel and RSS tags respectively, that we opened previous. After that we want to display the tips in order that the web browser can see it. To try this we merely use the echo command and echo our variable $output that we used to store all of the information prior to now. You should now have the ability to see what I supposed earlier within the article after I prompt that you could use echo instead of including the tips to a variable, but that's besides the point. What matters now is that you simply acquired a fully working RSS feed that you simply by no means have to toy with once more unless its so as to add extra knowledge. Hope you revel in it and might put it to excellent use!

Breman celebrates Odwira peacefully – BusinessGhana

Breman celebrates Odwira peacefullyBusinessGhanaOn extension of electrical energy to rural communities, Mr. Quansah stated about 90 % of cities and villages within the district were connected to the National Electrical energy Grid and have requested for the list of communities but to be related for …

http://agentmonhost.com/free-net-internet hosting…

Show more