Here is a simple steps to install JSON for PHP in *nix server. The sample command below are running under Centos OS.

yum update "*php*"

Make sure you have install PHP Pear.
yum install php-pear

and The pecl command can then be found at /usr/bin/pecl

Then, we can start install PHP JSON.
pecl install json
cd /etc/php.d/
echo “extension=json.so” >> json.ini
service httpd restart

If you are using JQuery with JSON, take a look at this JQuery Json Library. Easy to use and simple.

Higlighting Table row in JQuery

Recently when refactoring the JQuery old code, about a year ago and i found an old and un-JQuery solution where using inline Javascript in HTML. Inline Javascript are bad which are the curse of HTML.

For example :

 <table> 
    <tbody>
       <tr onclick="rowClick('carID') onmouseover=rowOver('carID') >
          <td>whatever here </td>
       </tr>
   </tbody>
</table>

1. Above inline Javascript will not be cache. Every time when the user refresh the page, it have to be reloaded again.
2. Hard to maintain and read something. You have to write everything in 1 line if the line is too long else you will get an error message like this “unterminated string literal” due to line break.
3.It is bad for javascript handling because it wont be able to access event object and
by reading you cant decide what this references to

One of the way to improve the code using jquery and using html class attribute.

<table> 
    <tbody>
       <tr class="carDetails">
          <td>whatever here </td>
       </tr>
   </tbody>
</table>

Since the mouse over event are tie to “this” object, we can get or do anything that are wraped around this object.

$(document).ready(function() {

   $('tr.carDetails').mouseover(function(e) {
       $(this).closest('tr').addClass('highlightSelectedRow');
   });

});

To get the selected row index you can follow the example. Using JQuery closest is one of the way :

    //this will return the row index
    var rowIndex = $(this).closest('tr').parent()[0].sectionRowIndex;

or

    //this will return the row index as well
    var rowIndex = $(this).closest('tr').removeClass('highlightSelectedRow').prevAll().length;

In the above example, when the user move the mouse over the table row, the tr will be added with css class ‘highlightSelectedRow’ to highlight the row color. You can see that we are using JQuery traversing/closest which are available in JQuery 1.3. The new version of JQuery 1.4, allowed array to be passed in. What it does are it get a set of elements containing the closest ancestor element that matches the specified selector, the starting element included.

New example :

<div id="a">
   <div id="b"><b class="carName">ABC</b>
   <div id="c"><b class="carName">CDE</b>
   <div id="d"><b class="carName">DEF</b>
</div>
$(document).ready(function() {

   $(".carName").mouseover(function() {
      //return id of div b or c or d which depends on the text that the mouse event is trigger.
      var id = $(this).closest("div").attr("id"); 
   });

});

Quote from JQuery website :

Closest works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn’t match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.

JQuery get $.getJSON data

Imagine you have server side data return to your Javascript client side using Ajax and JSON as datatype return.

var groupid = null; //try outer scope
$.getJSON(url, data, function(result){

    groupid = result.groupid;  //json type data
    //do whatehever you like here
    //you wanted to return the data that you have been parsed or assign

});
alert(groupid); //error : will return null because the JSON already happern after the callback executed.

So instead doing that, we need to use delegate or callback function that JQuery have provided to us.
Remember the $.fn. thing.

Now, let see how we can use the delegate function to return us the data:

$.getJSON(path +'?request=url', function( data ) {

var selectOption = '';
//do what ever you like with the json data

selectOption =  selectOption + 'testing' + data; //example only
$.fn.delegateJSONResult( selectOption ); 

} ); 

$.fn.delegateJSONResult = function{  alert( data );  }

With JQuery delegates, it makes your life easier as developer and the most common used to all my event handling.

To understand better about delegates, look at JQuery code to see how it actually works behind the background or alternatively try to play around with this sanbox.

The above source code highlighting was proudly developed by WordPress. Many thanks.

My Favourite Web Development tools.

Every few months, i will be Googling or check out new web development tools for my work and personal projects. Unbelievably,  new cool and fantastic tools kept popping up, something you even have problem which best tool to used. The tools i meant here, are tools like IDEs, plug-in, library, framework, CMS, test-suite and more. Below, are the list of tools that currently i am using and found it very useful and increasing my productivity. All the tools are OPEN  SOURCE which i am a very big fan of.

Firefox Add-ons

——————-

1.  Again, Firebug win my heart. This tool let you inspect and edit CSS on the fly. Not only css, debugging javascript with it, save tons of your time to look for javascript errors.

Firebug screen shot

Firebug screen shot

2. iOpus iMacros – from a Firefox add-ons : to record form filling and replay the task you had been recorded. Save your time kept repeating testing for web forms, useful for web testing, web scraping and more.

IMacro

IMacro

3. Web development– from a Firefox add-ons : Great tools with lots of functionality for web developers. Such as markup validation,  cookies management, http header management and more.

Web Development toolbar (Firefox add-ons)

Web Development toolbar (Firefox add-ons

4. XDebug for Javascript

5. Yahoo YSLOW – Javascript profiler : A great tools to check how to faster up your website. Their also provide best practice and guideline how to improve and tune website loading performances.

Framework

————–

1. Zend framework : started using recently and fall in love with it. Strong MVC architecture support and database adapter.

2. jQuery : A Cool Javascript framework that i have been using almost 2 years since it released. JQuery have strong community support, easy to learn, good documentation (at least better than some), more books on JQuery and more.

CSS

—-

1. 960 Grid System from MIT. Very easy to use and faster up your layout  design for a new website without worrying if the new row or column will expand accordingly.

Forum

——–

1. Although this part is a bit not relevant, but i insist to introduce the best question and answer programming/IT forum : http://www.stackoverflow.com

2. Slashdot : Ah!!..sometimes you need a break from your IDE or notepad.

Time Tracker

—————-

1. I use RTM : Remember the Milk task list which integrate well with Gmail Task List.

2. Google Calendar : Use it for weekly planner and task remimder which sent free sms reminder to your mobile phone.

jQuery

Have been using jQuery since my new projects. As a results, i am very satisfy with the Javascript framework and external UI Plug-in.

I used the guidelines provided by reindel blog to choose the best javascript framework to meet best for my new projects. My first round-up choice are between Mootools, Prototypes/script.aculo.us and jQuery. I use Slickspeed website to run and check how fast the website run, and jQuery gave me the best results. Since jQuery run much more faster than others, except Mootools (on the version that i had tested).

The reason why i choose jQuery over other javascript frameworks ——————————————————————–

1. The learning curve for jQuery are much more short and there are tons of tutorials for jQuery.

2. One of my requirements, is to optimise the performance when running on client side.

3. Slow staging server (no budget), cause slow execution of scripts at client side if i am using other javascript framework.

4. Both Microsoft and Nokia have decided to add jQuery in their .Net and Symbian OS. (Future mobile implementation).

5. jQuery official website provide more demo type tutorials compare to others, which are very handful when it comes to using new functions.

Quite like the syntax and idea of Prototypes/script.aculo.us features and might be consider it with my own personal project.

These reasons were only my personal opinions, which i do not mean that other javascript framework are bad. It really depends on how it is going to fit with your best need such as environment, learning curve, your own coding style and more.