World.PHP
Quickly add feed capabilities to your PHP application
Jul 28th
Although feeds aren’t a new invention, lately they are getting more popular. Using feeds, we have an easy and convenient way to publish content, either news or any other sources (blogs, articles, etc.)
I had to implement feeding capabilities to a PHP application I’m working on, and I’ve been suggested to use FeedCreator, an open source library written, by Kai Blankenhorn, which provides support for the following feeds:
- RSS (0.91, 1.0, 2.0)
- PIE (0.1)
- MBOX
- OPML
- ATOM
- HTML
- JS
The most diffused are undoubtedly RSS and Atom, but if you need less known format you can.
Adding feeds to your application is as simple as writing the following code (taken from the library documentation):
include("feedcreator.class.php");
$rss = new UniversalFeedCreator();
$rss->useCached(); // use cached version if age<1 hour
$rss->title = "PHP news";
$rss->description = "daily news from the PHP scripting world";
//optional
$rss->descriptionTruncSize = 500;
$rss->descriptionHtmlSyndicated = true;
$rss->link = "http://www.dailyphp.net/news";
$rss->syndicationURL = "http://www.dailyphp.net/".$_SERVER["PHP_SELF"];
$image = new FeedImage();
$image->title = "dailyphp.net logo";
$image->url = "http://www.dailyphp.net/images/logo.gif";
$image->link = "http://www.dailyphp.net";
$image->description = "Feed provided by dailyphp.net. Click to visit.";
//optional
$image->descriptionTruncSize = 500;
$image->descriptionHtmlSyndicated = true;
$rss->image = $image;
// get your news items from somewhere, e.g. your database:
mysql_select_db($dbHost, $dbUser, $dbPass);
$res = mysql_query("SELECT * FROM news ORDER BY newsdate DESC");
while ($data = mysql_fetch_object($res)) {
$item = new FeedItem();
$item->title = $data->title;
$item->link = $data->url;
$item->description = $data->short;
//optional
item->descriptionTruncSize = 500;
item->descriptionHtmlSyndicated = true;
$item->date = $data->newsdate;
$item->source = "http://www.dailyphp.net";
$item->author = "John Doe";
$rss->addItem($item);
}
// valid format strings are: RSS0.91, RSS1.0, RSS2.0, PIE0.1 (deprecated),
// MBOX, OPML, ATOM, ATOM0.3, HTML, JS
echo $rss->saveFeed("RSS1.0", "news/feed.xml");
What makes it so special is that it is really simple to use.
The libray is open source, released under the LGPL license.
For more information and to download the library, click here
How to prevent validation errors from being displayed in CodeCharge Studio
Jul 11th
I have a registration form which uses 2 dependent list boxes Country and State, where State is populated basing on the Country selection.
To dynamically load the State list box I had to add a client OnChange event to the Country list box, which submits the form. But at this point the form displays all errors (mostly missing fields) and I don’t want this to happen.
This is the solution I found, which uses client side JavaScript:
The name of my form is RegisteredUser
- I have added a hidden field to the form, with name
Validation, Control Source Type = "Code Expression", type Boolean and default value = True
- I’ve added a client OnChange event to the Country list box, with the following code:
- In the HTML code, I’ve located the {Errors} row, and set the id and name attributes to phErrors – it should look like:
- I’ve added a client OnLoad event to the form, with the following code:
This code reads the value of the Validation hidden field: if the value is No, the phErrors row is hidden, otherwise it is shown.
It seems to work correctly, and also it shouldn’t add the row to the database, because the validation occurs at server side, but it isn’t displayed at client side (although included in the HTML).
