Posts

Showing posts from 2011

HTML Input Place Holder

A Simple way to implement placeholder in a Input Field. Although HTML 5 by default provides placeholders[placeholder attribute], it falls short in Cross Browser Compatibility Construct A Simple Form Username: Let's Implement Placeholders Now [With jQuery] Change the HTML To HTML: Username Result Username Apply CSS and Overlay placeholder over Input Element CSS: .place-holder { position:relative; left:-245px; color:#AAA; z-index:1; } .name-field { width:250px; background:#FFF; z-index:0; } Result Username Handle Focus and Blur Event :) jQuery: $().ready(function () { function hideHolder() { $('.place-holder').hide(); } function showHolder() { if($('.name-field').val() == '') { $('.place-holder').show() } } $('.name-field').focus(hideHolder); $('.name-field').blur(showHolder); $('.place-holder').click(function () { $('.name-field').trigger('focus'); }); }); ...

JS Automatic Semicolon Insertion

Semicolon; Why should I care? "Javascript is the only language which people dare to use before learning" - Crockford Actually I, myself belong to that category of people whom crockford mentions :) But trying to be out.. So, What's new today? Just a informative writeup about ASI ASI? Ya, Javascript Automatic Semicolon Insertion What is a legal statement in Javascript? Following are some var a=10; a; b++; b+=1; ;;; // 3 Empty Statements +a var a = function() { }; {   a   b   c     }; Lets start Try the following var a=10; function test() {     var b;     b = a;     b+=1 } console.log(a) Even though I didn't insert any semicolon [Statement Terminator] in line 5 and 7, the JS engine never throws an syntax error. Reason: ASI Construct Rules to remember:     ** ASI will insert one for you, if you specify a line terminator @ [no line terminator] mentioned        in the gra...

Making Javascript Good :)

Somethings I learnt from Crockford :) Keep it right always :) function a()  //Seems to be working but not :) {     return     {         name: 'a'     };     } console.log(a().name); function a() {  //Keep braces always right. It works :)     return {         name: 'a'     };     } console.log(a().name); There is no concept of block level scoping in javascript but the syntax exists. So, the former falls in that block hole and returns undefined. It's not c or c++ var i = 'tamil'; for(var i = 0;i < 10;i++){      } console.log(i); This is misleading because declaring 'var i' @ initialization of for-loop doesn't mean 'i' has the scope of the loop & it doesn't interfere with the one out. There is no other scope than function scope in javascript. All our declarations are hoisted up to function ...

A Javascript Introduction

Had a chance to read a nice javascript tutorial on variables and data types :) Let's share :) Q: How will I declare a variable? var variablename; Q: I don't like variablename I wish to have $$$$$, Is it possible? A: Ya you can there is no restriction on varible names except 1. It should start with either a alphabet or $ or _ 2. It should not use any reserve words like function, break, var, etc., 3. It should not use any of the operators [!@#%^&*()-+=] Q: Is there any javascript practise to declare variables? A: The answers is upto you. But most common practise is using CamelCase [Inherited from java] var testvariable; const TEST_CONSTANT; function testFunction() { } testObject = new Object(); Another way is Hungarian Notation - appending type of the field/variable to its name var intAge = 19; //Interger var strName = "xxxx"; //String var bSet = false; //Boolean var fpRate = 12.22; //Floating Point var fMember = true; //Flag Advantage: Fol...

CQL(Cassandra Query Language) Reference

Hi, I think now I'm eligible to publish a blog post regd CQL. I wish this blog to be a tutorial rather a Syntax provider for various CQL queries Points to remember 1. CQL doesn't support Composite Types[likely to chage] 2. CQL doesn't support Super columns 3. Counter values are returned in native format [atleast in php/using cqlsh] Why should I prefer CQL? 1. Readability 2. Ease of use 3. SQL like 4. Stable Support: PHP => My Posts using PHPCassa Python => Refer Here and Download it here Java => JDBC Driver and Example is here Ruby => This might help Creating a Keyspace: cqlsh> CREATE KEYSPACE sample WITH strategy_class = 'org.apache.cassandra.locator.SimpleStrategy' ... AND strategy_options:replication_factor = 2; Note: : => Option Specifier Use a Keyspace: cqlsh> USE sample; Note: Don't forget to USE before use 'Sample' and 'sample' are different. Create Column Family: cqlsh...

Cassandra CQL PHP Part 2

I have explained the way to execute CQL Queries from PHP via PHPCassa in my previous blogpost here  and tutorial on cql queries here It was difficult for me to remember the syntax of update and select CQL queries for Cassandra. Hence I wrote wrappers for them which are listed below Update Counter Query: /* * $counterMap => Associative array of CounterColumn names and corresponding value for given $key * Ex: UPDATE TestCounterFamily SET 'counter'='counter'+1, 'counter2'='counter2'+3 WHERE KEY='test' */ function generateUpdateCounterQuery($columnFamily, $counterMap, $key) { $colArgs = ""; foreach($counterMap as $counter => $value) { $colArgs=$colArgs."'".$counter."'='".$counter."'+".$value.", "; } $colArgs = rtrim($colArgs,", "); return "UPDATE ".$columnFamily." SET ".$colArgs." WHERE KEY='".$key."...

Auto-Complete in Eclipse&Zend

[Try right click on project and see if there is Configure > Add PHP Support, If it is there use it] This is a inference of mine while using Eclipse/Zend as IDE for PHP in linux [Not sure abt windows] I was able to notice that PHP perspective over a SVN checked out PHP project didn't function as it was doing for a regular PHP project created via eclipse. For Ex: 1. No syntax errors where highlighted/notified 2. CTRL press over a variable or a class or a function didn't give the option to navigate to the point its declaration 3. Autocompletion was completely disabled After lots of searching, I figured out that whenever we create a project via eclipse, it will create two configuration files along with it .project .buildpath SVN projects might have these files not being configured properly or might even miss them So, inorder to overcome this, either you can create one by hand or create a new PHP project and export the files you checked out in to the newly creat...

Abt Some Linux Commands

Interesting Linux Terminal Commands Notation => | - Cursor CTRL + w = Delete a word of the command typed Ex: $ echo this is a line| $ echo this is a |[After ^w] CTRL + b = Move the cursor one character left[Backward] Ex: $ cat filename| $ cat filenam|e CTRL + a = Move to the start of the command [Equivalent to Home] Ex: $ cat filename| $ |cat filename CTRL + u = Clear the command from the cursor's point Ex: $ cat file1 |file2 $ file2 CTRL + r = Reversed search, Searchs for the pattern in the commands entered in the past and displays one after another continuously [Last entered command first] CTRL + e = Move to the end of the command Ex: $ cat |file1 file2 $ cat file1 file2| !! = Executes the last entered commmand !$ = Returns the last argument of previously entered command Ex: $ mkdir /home/user/temp $ cd !$ !pattern = Executes the last command with this pattern !pattern:p = Prints the last command with this pattern instead exec...

Cassandra CQL PHP

After lots of googling I figured out how to access counter columns value via PHPCassa + CQL. Though it might not be efficient, this is all I was able to figure out after a long struggle :) First Connecting to and Selection from Cassandra Instance via PHP using PHPCassa $pool = new ConnectionPool("Stats",$servers); $raw = $pool->get(); $rows = $raw->client->execute_cql_query("SELECT 'a' FROM Impressions WHERE KEY='xxx'", cassandra_Compression::NONE); To Return Connection $pool->return_connection($this->raw); To Update Counter value via PHPCassa $pool = new ConnectionPool("Stats",$servers); $raw = $pool->get(); $rows = $raw->client->execute_cql_query("UPDATE TestCounter SET 'counter'='counter'+1 WHERE KEY='xxx'", cassandra_Compression::NONE); You can find a wrapper here So, once you are done with fetch successfully. This is the result you may get object(cassandra_CqlResult)#...

Crisps abt scalability Part 3

New to this post?? Go on with  Crisps abt Scalablity Part 1  and Part 2 So are you convinced with Master Slave. Let us move on to Sharding or Shared Nothing Architecture What is it?  Before this I'll answer a different question. For Whom is it??  If the application you built can be partitioned. A scenario, You have 1 billion customers and all the customers are currently being maintained by only one data store. And they are being served from the only available data store. But the beauty of your application is there is nothing being shared between two customers. So, what is the use of it?? Just split them. Because they all are going to read data about them and they are going to write to data about them. Put up in another way, No one is going to read or write to your data except you. So, what you can do is just split your billion customers in to half a billion and make another data store to handle them. I hope now you understood what it is.  That is aw...

Crisps abt Scalability Part 2

If you are reading this part for the first time. First go on with the previous post Crisps abt Scalability Part 1  of mine . So what are the two alien terms I mentioned earlier.  WT* is Master and Slave Architecture?? Simple as it name depicts. There ll be one master and the slaves ll listen to him. Soooorrry :P. Let me be clear. One of the implementations of master slave architecture is all writes will go only to the master and reads can be done from any of the master or slave. Ex: Redis Cache. And another way of implementation [I Hope] is master will act as a place holder of metadata about the location of data in its slaves. Ex: HBase (But this is totally different from the former, will be explained soon). What is the advantage of this???  Consistency -> [Conditions apply] How?? Simple, consider the same scenario of shopping cart. The problem was not about reading the data but writing.. right?? So, we converge all the writes to one common lo...

Crisps abt Scalability

Hi all, It was a great day with "Cassandra - Definitive Guide". The best thing i liked abt that book is the way they explained scalability(ofcourse abt Cassandra too :P).  So,  What is scalability? It is nothing but yet another performance measure. To be clear, it is the measure on how well the application you built performs, if you add more resource to the environment it operates on. Ex. Adding more cores to processor, Increasing main memory's size, etc., Why should I care abt it? Because it is not true that if you add more resource, performance of your system(ur app) should increase. Also there is a probability that performance of your system to decrease. Can You Explain more in detail? Yes, For sure. Actually there are two ways you can scale the processing environment. Horizontally and Vertically.  Horizontally refers to adding more systems instead enhancing the existing system. Ex: Adding more web servers to serve the requests and using a load balancer ahead....

Abt jquery drilldown

It was a boring weekend once again and I wished to author a jquery plugin for drilldown operation. Atlast I did it and commited in GIT Link to Git Repo Really awesome job by jquery team. I never felt that it is difficult to create a plugin. It was full of fun and my day passed by in a useful way :) Abt my plugin, This is a plugin which helps to implement drill down in jquery. To start working on it u need jquery latest version U need to include either jquery.drilldown.min.js/jquery.drilldown.js in your javascript src Usage: $(selector).drillDown({ animate: true; // Default 'true' *Not required container : ".divname|#spanName" //No Default values *Required listParam : "list" // Default 'list', see the demo for explanation direction : 0 // Default 0->vertical, 1->horizontal callBack : test // Default 'null', Call back function to be used on selection }); Styles: Elements inside Drill Down are li elements with c...

Something abt PoLymOrphISM

A most prominent interview question regd OOPs I: What is Polymorphism? S: (Ha ha ;D) Polymorphism is of two types runtime and compile time. Methods of Compile time polymorphism are Function Overloading and Operator Overloading Methods of Runtime polymorphism are virtual functions I: Kool....What is virtual functions or Method Over riding? S: (Super :D :D)It is possible that we can assign reference of a derived class object to base class In Such a case if derived class implements a function with same signature as its base then an ambiguity will be arised on resolving function call So, inorder to overcome that we use virtual functions. I: Good... Why are they refered as runtime polymorphism? What do u mean by runtime?? S: (Pochu da :( :( ) "Actually" the resolution of function call takes place at runtime. "That is" Compiler don't know which function to call ... Blah Blah .... I: Why should the resolution take place at runtime? Why not at com...

It's abt Brain F**K

It was a Boring Saturday again :P So jus started exploring Brain F**K an awesome weird programming language It has got just 8 alphabets + - < > , . [ ] that's it :) This is like Maagi of programming languages :D . - Prints the current byte + - Increments the byte pointed to - - Decrements the byte pointed to < - Moves to previous byte > - Moves to the next byte [ - Executes the instructions until ] ] - Executes the instructions from [ until the current byte carries 0 , - getchar() U wanna know the C representation of Brain F**K char array[30000]; char *pointer; That's it :) It has a global array of size 30000 with a pointer pointing to it :) So I'm curious and tried this one +++++ +++++ [>+>+++++++ >+++++++>++ ++++++ >>+++. >+++.+++.>++++++. >>+++. ---- . >>>+[[-] ,.------- --- ] So now UR curious? Jus Click the link to Execute this 1. Copy past...

Crisps abt Design Patterns

Problems are hard solve more than that, our solution should not make our appln disobey OCP (Open to extensions and Closed to Modifications) but what if they have a quality solution pre-existing :) That's what design patterns are :P They are generic reusable solutions for commonly occuring problems in S/W Design Design patterns are of various categories if u wanna know them in detail follow the link below Design Patterns :) I was just going thro some of the most commonly used Design Patterns in PHP and I wish to give them all a small introduction Singleton: A common scenario for this is a LOG BOOK. We might be recording various events at various times. So, whenever we need to record something we ll be declaring an object for the log book class and will be WRITING using some member functions of it. But what if we want a "GLOBAL LOG BOOK" for entire appln? Then choose singleton patterns. This pattern allows instantiation of object for a class only once. For Furthe...

all* abt PHP OO :P *conditions apply :D

Hi Folks spent a day with PHP in action Book An awesome book for developers :) Wanna share some useful tips out of it 0. When ISA (Inheritance)? When HASA (Composition)? Only way to decide it is relationship if it is going to be 1:1 go for ISA 1:Many go for HASA :) Cool but true :) 1. Predict what would happen $object1 = new base(); $object2 = $object1; in PHP 4 a photocopy of present appearance of the object1 is copied to object2, any changes in object1 later ll not affect the latter :D in PHP 5 it means shallow copy :) jus the reference is returned to object2 so changes on former or latter ll reflect on latter or former too :D wanna do the PHP 5 way in PHP 4 then do $object2 = &$object1; Explicit way :) But this has the difference with the PHP5 way, in this jus the symbol table is adjusted (Clever no) :) 2. Can abstract functions be private? Don't scold me if u know the meaning of abstract class :D The answer is NOOOOOOOOO obviously :) Becaus...

Google Facebook Fun

Jus a funny fact to start with Jus Search for "Google" @ http://www.google/com and "Facebook" in the same :) Search results found for Google 10,490,000,000 But for facebook 13,410,000,000 :D