Use Javascript navigator.useragent to detect mobile devices

There are many ways to detect a device. Today, i suggest  a method of using JavaScript to detect a device

Navigator.useragent allows you to detect mobile devices including ipad


var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }};
}

and use it as the following:

 if( isMobile.iOS() ) alert('iOS');

or simply including all in a function


function detectmob() {
     return !!navigator.userAgent.match(/iPad|iPhone|Android|BlackBerry|Windows Phone|webOS/i));
}
 

Insert and Update multiple records at once

INSERT Statement
If you want to insert a numbers of records, try the following:

INSERT INTO TABLE_NAME1 ('COLUMN_A', 'COLUMN_B')
VALUES
(VALUE_A11, VALUE_B12),
(VALUE_A21, VALUE_B22);

For MySQL syntax, please visit MySQL :: MySQL 5.1 Reference Manual :: 13.2.5 INSERT Syntax
For PostgreSQL syntax, please visit PostgreSQL: Documentation: 9.3: Inserting Data
UPDATE Statement

UPDATE TABLE_NAME1 AS T1
SET(A, B) = (
SELECT VALUE1, VALUE2 FROM TABLE_NAME2 AS T2
WHERE T1.COL1=T2.COL2
)

There’s a problem with above UPDATE statement. In the subquery “SELECT VALUE1, VALUE2 FROM TABLE_NAME2 AS T2 WHERE T1.COL1=T2.COL2”, if it returns NULL values, your query could update with those NULL values. Hence, you should have a WHERE condition to prevent this problem, for example:

UPDATE TABLE_NAME1 AS T1
SET(A, B) = (
SELECT VALUE1, VALUE2 FROM TABLE_NAME2 AS T2
WHERE T1.COL1=T2.COL2
)
WHERE A > 5

For MySQL syntax, please visit MySQL :: MySQL 5.1 Reference Manual :: 13.2.10 UPDATE Syntax
For PostgreSQL syntax, please visit PostgreSQL: Documentation: 9.3: UPDATE

Persistance and Duplication jQuery cookie

Using jQuery cookies to maintain user states becomes more popular. However, i’ve recently found a problem where state didn’t appear to be persisting and on viewing the cookie information, using the web developer toolbar, i had multiple versions of the same cookie.

when a cookie is created, its path is set default to ‘/’ meaning that the cookie is valid throughout the whole domain

Cookies can also be set per directory and they’ll be valid for a certain set of pages.

Therefore, to overcome this problem, instead of a simple name, value pair:

$.cookie('the_cookie', 'the_value');

you need explicitly set the cookie’s path as "/"

$.cookie('the_cookie', 'the_value', { path: "/" });

How to edit images in Adobe Fireworks CS6

Just follow these steps to cut your image areas:

  1. Select the Crop tool from the Select area of the Tools panel or from the Image editing panel
  2. Click and drag to select the area you want to keep when the crop is complete
  3. Press the Enter and Return key

the image is cropped to the selected area

If you’re working with a wireframe of a larger web page design, you might want to export only a small area not the rest of the image. To crop it, follow these steps:

  1. Click and hold the Crop tool to select the Export Area tool
  2. Click and drag to select the area you want to keep when the image is exported
  3. Double-click in the middle of the cropped area
  4. Right click to copy
  5. Create new document and paste it
  6. Then select File to export, you can select any type of GIF, BMP, JPEG, PNG, … to export

Preload images with jQuery

$(function(){
    var cache = []; // declare array to store our images
    $.preLoadImages = function(){
        var args_len = arguments.length;
        for(var i = args_len; i--;){
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    };
 }); // call our predefined function $.preLoadImages('./images/sample1.png', './images/sample2.png'); 

Reference:

http://engineeredweb.com/blog/09/12/preloading-images-jquery-and-javascript/

PHP Unit Test – part 1

Prerequisites:

  1. PHPUnit Framework installed
  2. extends class PHPUnit_Framework_TestCase

phpunit

  • Test number
class StackTest extends PHPUnit_Framework_TestCase
{
    public function testFailureNumber(){
        $this->assertEquals(1, 1);
    }
}
  • Test string
public function testFailureNumber(){
    $this->assertEquals(1, 1);
 }
  • Test xml
public function testFailureXml(){
    $expected = new DOMDocument;
    $expected->loadXML('<foo><bar/></foo>');
    $actual = new DOMDocument();
    $actual->loadXML('<foo><bar/></foo>');
    $this->assertEquals($expected, $actual);
 }
  • Test object
public function testFailureObject(){
    $expected = new stdClass();
    $expected->foo = 'foo';
    $expected->bar = 'bar';
    $actual = new stdClass();
    $actual->foo = 'foo';
    $actual->bar = 'bar';
    $this->assertEquals($expected, $actual);
 }