Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Thursday, July 27, 2006

Quickly add feed capabilities to your PHP application

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

Tuesday, July 11, 2006

How to prevent validation errors from being displayed in CodeCharge Studio

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

  1. I have added a hidden field to the form, with name
    Validation, Control Source Type = "Code Expression", type Boolean and default value = True
  2. I've added a client OnChange event to the Country list box, with the following code:
    document.forms["RegisteredUser"].Validation.value = "No";
    document.forms["RegisteredUser"].submit();
  3. In the HTML code, I've located the {Errors} row, and set the id and name attributes to phErrors - it should look like:
    <!-- BEGIN Error --> 
    <tr class="Error" id="phErrors" name="phErrors"> 
      <td colspan="2">{Error}</td> 
    </tr> 
    <!-- END Error --> 
  4. I've added a client OnLoad event to the form, with the following code:
    var phErrors = document.getElementById ("phErrors");
    if (phErrors != null) 
    { 
      if (document.forms["RegisteredUser"].Validation.value == "No") 
      { 
        document.forms["RegisteredUser"].Validation.value = "Yes"; 
        phErrors.style.visibility = "hidden"; 
        phErrors.style.position = "absolute"; 
      } 
      else 
      { 
        phErrors.style.visibility = "visible"; 
        phErrors.style.position = "relative"; 
      } 
    } 
    

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).

Monday, July 3, 2006

How to better appreciate your most preferred language

The universal technology

I've seen a lot of developers telling that Linux is better than Windows, C# is better than Java, Delphi is better than VC++, and so on. But if you ask them if they know the other language they are comparing with, they say no.

I was one of them, since I believed that the best IDE was Delphi, the best platform was Windows, and the best language was C++. At least, until I started working on "real projects" and not on applications made just for fun. When I had to learn Java and use it to develop server applications on Sun platforms, I started to appreciate the language and the Unix-like environments.... and I started to believe that these technologies were better than those I thought were the best. Soon I realized that every technology is the best, depending on where it is applied.

There is no universal technology or language which solves all problems

Now I don't believe anymore that there is the universal technology or language which solves all problems. Every time I start a new project, if the customer doesn't have a specific preference, I always spend some time trying to understand what are the best tools to implement the project - and the result depends from the project itself. In web development my favorite preference is the .NET technology, for several reasons - but this is not a choice that can be done every time for every project. For example, if one of the requirements is that the application must run on open source environments, .NET wouldn’t be the proper choice.

The proper tools

During my professional experience I've tried to learn as many languages as possible. If I get confident knowledge of each language, I'm able to choose the best solution for every problem, from both the developer point of view (how simple or hard is the development using a specific language) as well as from the customer/project point of view (what are the benefits if I choose a technology instead of another). When I learned Java, I appreciated how life is easier with a memory manager which automatically disposes objects no longer referenced. When I learned C#, I appreciated the integration with the .NET technology, the foreach statement, but I also noticed how exception handling was better in Java, since exceptions must be explicitly handled within each method or the method itself must be declared to throw those exceptions which aren't handled in its body. I also, and still, like the C++ philosophy: "The developer always knows what he's doing". This is the reason why C++ offers more power with pointers and access to memory. It's irrelevant that it's not always true the developer always knows what he is doing :-) The key point is that you have the power to do almost everything, although at a price (usually more attention when dealing with pointers and dynamically allocated memory, and more debugging....)

PHP Revolution

Lately I've appreciated a language I've used in the past, and which I didn't like: PHP. The reason why I don't like PHP is that I prefer strongly typed variables, variable declarations, as well as to know from the compiler if I wrote something wrong in my code, such as the name of a variable (a common mistake in PHP which leads to unpredictable results) or a function call (which usually leads to a run-time error). But I have to say that it's extremely practical the ability to save your PHP code and test it immediately without the need of recompilation, deployment, etc. - a perfect solution for the project I'm currently working on. And I wouldn't had chosen it if the customer hadn't explicitly asked for it. We never stop learning!! Now I'll definitively choose it again in the future, if the prerequisites for its usage are good, and the project will benefit from its usage.

Can I say that C# is better than Java?

Yes and no. It depends. As outlined above, in order to state that a language or technology is better than another, I need to:

  1. have proficient knowledge of both
  2. compare pro and cons applied to a specific project

But the result of the comparison is limited to the project - on a different project, the choice could be different:

  • if I need to care about speed, maybe the best solution would be C++, but not for web applications
  • if I need portability, Java is best - but I wouldn't choose it if the application has a user interface
  • if I need modularity and easy of development on a web application, my choice would fall on ASP.NET
  • if I need low licenses cost, the choice could be PHP, Java/JSP, or Ruby On Rails

Your opinion

How do you choose the technologies and languages on your projects? Do you focus on a single technology, and use others only when the customer explicitly asks for them?

Copyright © 2013. All Rights Reserved.