window.location.href not working in Chrome

When your Javascript is not working in browser (IE or FF or Chrome latest version), you can replace the codes  window.location.href (or) document.location.href with the one below. Somehow, setTimeOut works across all browsers with latest version (IE 8 &9, FF 8.0.x, Chrome 23.0.1271.x

function redirect_home(){
setTimeout(function(){document.location.href = "ext_search.php?mode=dropoff"},500);
}

How to develop JSON with PHP

JSON is pretty flexible when you want to pull data from MySQL database and feed it to the front end software such as Web-based applications or Desktop software or even Mobile applications like Android, iPhone, iPad. I’ll just give you a simple example that is running PHP to feed data to a front end app.

On the Android, you’ll build an interface that let user input a word in English (for example: eat), then hit the Submit button to translate this word into Italian and Vietnamese. Once the request transfer to the server, PHP will process the data and return an object that include a word with the same meaning in Italian and English. This is what you’ll build in your PHP page, so that Android app can call that page and pass the parameter as GET to the server.

switch(strtoupper($_SERVER['REQUEST_METHOD'])){
 case 'GET':{
  $english = strtolower(trim($_REQUEST['par']));
  $t = $Db->GetRow("select * from $database_mns.android_evi_dictionary where english = @1@", $english);

  //build array lists of keys and values to feed the Object
  $ar_keys = array('comp_logo', 'developer_name', 'ip_address', 'english', 'italian', 'vietnamese');
  $ar_values = array('www.YourDomain.com/images/logo_jpg.jpg', 'Jane Doe', $_SERVER["REMOTE_ADDR"], $t['english'], $t['italian'], iconv("UTF-8", "ISO-8859-1", $t['Italian']));

  $obj = new stdClass();
  for($i=0;$i<count($ar_keys); $i++){
   $obj->{$ar_keys[$i]} = $ar_values[$i];
  }
  echo json_encode($obj);
  break;
 }
 default:{
  header('HTTP/1.0 405 Method Not Allowed');
 }
}

What is JSON?

According to IT Wiki, JSON (/ˈdʒeɪsən/), or JavaScript Object Notation, is a text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for many languages.
The JSON format was originally specified by Douglas Crockford, and is described in RFC 4627. The official Internet media type for JSON is application/json. The JSON filename extension is .json.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

Data types, syntax and example

JSON’s basic types are:

Number (double precision floating-point format in JavaScript, generally depends on implementation)
String (double-quoted Unicode, with backslash escaping)
Boolean (true or false)
Array (an ordered sequence of values, comma-separated and enclosed in square brackets; the values do not need to be of the same type)
Object (an unordered collection of key: value pairs with the ‘:’ character separating the key and the value, comma-separated and enclosed in curly braces; the keys must be strings and should be distinct from each other)
null (empty)

Non-significant white space may be added freely around the “structural characters” (i.e. the brackets “[{]}”, colon “:” and comma “,”).

The following example shows the JSON representation of an object that describes a person. The object has string fields for first name and last name, a number field for age, contains an object representing the person’s address, and contains a list (an array) of phone number objects.

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 45,
    "address": {
        "streetAddress": "21 Vinson Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10251"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "123 456-7899"
        },
        {
            "type": "fax",
            "number": "123 456-7889"
        }
    ]
}

SQL Stored Procedures

Sometimes in your Database Admin career path, you’ve got to create a procedure to save your time and reduce loads of work on server. For example, everyday you need to run a report of how many customers have signed up for a newsletter, or you need to pull out a list of customers who purchased items for the past 12 hours… In these cases, you don’t want to re-write a SQL query every time; instead, you’ll create a procedure and store it in the Database Stored Procedure Library so that you can reuse it every day by calling it with a simple calling query such as Call GetListOfUser();

Procedure:
DELIMITER //
CREATE PROCEDURE GetAllCustomers()
BEGIN
SELECT * FROM customer where signup_date > ‘2012-01-26’;
END //
DELIMITER ;

Usage:
Call GetAllCustomers();

Advantages:
– Performance: Stored procedure increases performance of application. Once created, stored procedure is compiled and stored in the database catalog.
– Efficient: It runs faster than uncompiled SQL commands which are sent from application.
– Reduce traffic: Stored procedure reduced the traffic between application and database server because instead of sending multiple uncompiled long SQL commands statement, application only has to send the stored procedure name and get the result back.
– Reusable: Stored procedure is reusable and transparent to any application that wants to use it.
– Save time: Stored procedure exposes the database interface to all applications so developer doesn’t have to program the functions that are already supported in stored procedure.
– Security: Stored procedure is secured. Database Admin can grant the right to application which to access which stored procedures in database catalog without granting any permission on the underlying database table.

Disadvantages:
– Stored procedure make the database server high load in both memory and processors.
– Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations or a complex of business logic which is not the role of it.
– Stored procedure only contains declarative SQL so it is very difficult to write a procedure with complexity of business like other languages in application layer such as PHP, Java, C#, C++…
– You cannot debug stored procedure in almost RDBMSs and in MySQL. There are some workarounds on this problem but it still not good enough to do so.
– Writing and maintain stored procedure usually required specialized skill set that not all developers possess. This introduced the problem in both application development and maintain phrase.

So when developing application you should balance between them to choose whether to use stored procedure or not.

What is the difference between Java and JSP?

Everyday I’ve learned new things. I came across java2s.com website and read Java tutorials. There’re so many things to learn about Java compared to PHP. The Java language and its libraries are very complex; that’s why it has a broader usage such as building apps for devices, TV, Websites, applications…, you name it.

As Java is a Programming language you can use the Java to design data application software, Web applications , Embedded Circuit Programming and a lot more. It is like the VB programming lanuage but remember Java is a Platform Independent. Whereas JSP uses the Syntax of Java Programming for Server Side Scripting. As you know that any Web Page is nothing but a collection of text that’s why the Jsp is designed to automatically renders the script code into HTML/Text format.

The way you write classes and objects for both Java and JSP are totally different, too.