The Passionate Craftsman

Ruby, PHP, MySql, Software engineering and more.

Wednesday 21 April 2010

How to detect browser type and version in PHP

You can use a simple class provided here: http://chrisschuld.com/projects/browser-php-detecting-a-users-browser-from-php/

A simple exmple:

if( $browser->getBrowser() == Browser::BROWSER_IE && $browser->getVersion() == 6 ) {
//do nothing
}
else {
// ok show something for non IE6 users
}

Labels:

Friday 16 April 2010

Scooter Framework

After reading an article on InfoQ, I leant that the new Scooter Framework for J2EE is inspired by Ruby on Rails. The resemblance to Rails is incredible and it is very simple too! In am sure that it will become very popular.

I suggest you watch the Hello World screencast:




Then the Twitter example shows you how the delopment works. There is an easy testing framework, but the the controllers cannot be tested yet.

Labels:

Date and time functions in MySQL

MySQL has many date and time functions to manipulate and compare dates and time, the reference documentation form 5.1 version. If you want to add a day to a datetime or date field:

SELECT now(), now() + INTERVAL 10 DAY;

If you run the query above you will see the current date and the current date plus ten days. You can add or subtract even hours, second and more.

SELECT *
FROM article
WHERE approved=1
AND (
(birth <> now( ))
OR (news=1 OR never_expires=1)
)

The example above shows a query which loads the articles between the bith and now and between now and the expiry (death) date. If hte article is a news or should not expire, the date comparison is not taken into consideration.

If you want the current time:

SELECT CURTIME();

If you want to get the day of a date:

SELECT DAY('2009-12-15');

If you want the difference, in days, between two dates:

SELECT DATEDIFF('2007-12-30 23:59:59','2007-12-20');

The result is 10 because MyQSL subtract the second value from the first. The documentation is quite clear and complete.

Labels:

Monday 12 April 2010

Find duplicate fields in SQL

Let's say that you have a table named users, where you keep your application users. If you has forgotten to set the username field as unique and you have some duplicate fields, you can use the following query:

SELECT username, COUNT(*)
FROM users
GROUP BY username
HAVING COUNT(*) > 1

Labels: ,