Archive for November, 2007

Javeline / Ajax.org, PHP

Beyond black magic: Regex gotcha

Personally I love using regular expressions. It simply the best way to parse/split/replace/validate pieces of text. Many people look at regular expressions as black magic. They might be able to create a regex, but don’t understand the flow. When you are creating more advanced expressions, it is important to understand the inner workings for many reasons, including performance.

Let’s have a look at the following regular expression: ^(\d+)(\d+)(\d+)$. It matches one or more digits and does that three times. It is an illogical expression, but bear with me.

Let’s match the string “12345678901234567890″. When we look at the inner workings of the regex engine we can see what is happening:
Continue Reading »

MySQL, PHP, Replies

Submit a form with AJAX using TelePort

This is a re-post of an article I wrote in back in August. We’re getting ready to release a new release of Javeline PlatForm at the end of this month. This release will be XForms compliant and has a lot of other new features as well. Before I start writing about that, I would like to put some attention on the communication layer aka Javeline TelePort.


PHPBuilder posted an article about sending a form using AJAX. This article shows how to post a simple form. But looking closely at the example, you can also see what the problem is using the plain XMLHttpRequest object and writing an implementation yourself.

First of all you to write some rather difficult javascript code. Next you need to completely rewrite your page, looking nor working like a normal HTML form. Last and most important, the function fetches values using getElementById(). This is not really very flexible, because you will need not only the form, but the function as well when you add a field.

Using an AJAX library can really help you here. Javeline TelePort has got a brand new method, which automatically creates a HTTP post request of a form and sends it to the server.
Continue Reading »

PHP

How I PHP: The Output Handler

There are some techniques that I use in almost all of my projects, but I hardly see in other projects. To my opinion others are missing out, so I’ll discuss a few of them, to start off with the use of an output handler. The output buffer caches the output data before it is send to the browser. Before the data is actually send, you have the possibility to use modify it.

Let me give a practical example of how I use the output buffer when developing websites. A website usually has a part which is common across all pages, containing the HTML header, the menu and those kinds of things. This is often solved in one of four ways:

  • have the content in each script
  • the use of include(’header.php’)/include(’footer.php’) or print_header()/print_footer()
  • using a templating system (like Smarty)
  • using a single entry point

All of these methods work, however there is a better way to solve this. You guessed it, by using an output handler.
Continue Reading »

MySQL, PHP

Getting the first 5 records of type X in MySQL

I just got a question from Dirk Bonenkamp. He has a query which returns a number of contacts, but he wants to get no more than 5 contacts of per customer. After thinking really hard, I came up with a solution using variables and a subquery.

To test this idea, I used the ‘world’ database available at MySQL dev zone. This query selects no more than 5 countries per continent.

SET @a=0; SET @b="";
SELECT Name, Continent FROM (SELECT @a:=IF(@b=Continent, @a+1, 0) AS a, Name, @b:=Continent AS Continent FROM Country ORDER BY Continent) AS t WHERE a < 5;

This isn’t one of the nicest looking solutions, but it works. Do you have a better solution? Please let me know :) .

PHP

Perl like temporary variables in PHP

When writing code in the global scope, I often have a problem where I’m overwriting a variable. This happens even more often when I work on code of somebody else. Usualy has the variable which does the overwriting is usualy just a temporary variable.

It’s difficult to choose a general name for all temporary, since you want a short name, but others might have used it for other purposes. However with ${’…’}, you can use any string as variable name, even an empty string of a string with spaces. Numbers get automatically cast to a string. So I nice way of solving the problem could be:

${0} = fwrite($fh, 'Test');
if (!${0}) throw new Exception("Writing the file has failed");
 
if ((${0} = pathinfo('/var/www/index.html')) && ${0}['extension'] === 'html')
  echo "Yes boys and girls";
}