Monthly Archives: July 2012

Why jQuery write less and do more?

jQuery is known as Write Less, Do More, JavaScript Library. Did you ever try to know why it is? Even thought I haven’t tried to know why is it but today’s morning I just checkout our one of client’s application report and realize why jQuery is known as write less and do more JavaScript library?

Let’s understand by a example.

Make JavaScript function to check or uncheck all checkboxes at one click. Here I try to explain both jQuery and core JavaScript function.

Here we list out counties list with check box ahead each county and check or uncheck all country using header checkbox.

<div>
<input id=”checkall” onclick=”fun_checkall(this)” type=”checkbox” name=”checkall” /> Check All Countries. <br />
<input id=”country1″ type=”checkbox” name=”country” /> India
<input id=”country2″ type=”checkbox” name=”country” /> USA
<input id=”country3″ type=”checkbox” name=”country” /> Australia
<input id=”country4″ type=”checkbox” name=”country” /> Japan
</div>

Using jQuery:

It’s only one line code to check or uncheck counties checkboxes.

jQuery(‘input[name=country]’).attr(‘checked’,obj.checked);

In order to check all check boxes there is no need to make function. You can write above code directly in onclick attributes. Just change obj.checked to this.checked;

A Complete jQuery function:

function fun_checkall(obj){
jQuery(‘input[name=country]’).attr(‘checked’,obj.checked);
}

or

<input id=”checkall” onclick=”jQuery(‘input[name=country]’).attr(‘checked’,this.checked)” type=”checkbox” name=”checkall” />

** You have to add jQuery library in order to use jQuery functions. Click to get jQuery library.

Using Core JavaScript:

In order to check all checkboxes using core JavaScript. You need to go through loop to check all counties.

var status = obj.checked;
var elements = document.getElementsByName('country');
var elementsLen = elements.length;
for(i=0; i<elementsLen; i++){
elements[i].checked = status;
}

How above code works:

I just hold value of current attributes value. It may either true or false. Checked value return true and uncheck box value return false. elements having array of country checkboxes. Now go with for loop and check one by one country check box.

A Complete Code of core JavaScript functions:

function fun_checkall(obj){
var status = obj.checked;
var elements = document.getElementsByName('country');
var elementsLen = elements.length;
for(i=0; i<elementsLen; i++){
elements[i].checked = status;
}
}

After reading both methods you realize why is it?

DEMO

A full example:

That is why I love jQuery. Do you?

HR Interview Question

Always be positive and give your answer on positive way.

Tell me about yourself.

Just focus on your professional life for this answer. Don’t try to say about your family detail whenever HR Person will not ask about your family background or want more about you in detail. Don’t accommodate childhood experience. Just tell about your education, field of interest and your hobbies (keep in mind about hobbies and area of interest should be in your resume). Try to give short and sweet answer.

Why are you applying for this particular job?

You can say that you are interested in field on which your company works and you can show evidence from your past work or certificate if you have that supports your interest.

What do you know about our company?

In order to get this answer you should be prepare information about company from where you will go for interview. Probably you get information from newspaper, yellow pages, may be got full information on company’s website, other employees etc. It may make good impression on them.

What makes you qualified for this particular job?

Tell them why you are interested in this job and give evidence about your interest. Such as, educational qualification. Include all past skill sets and experience that suitable for the job. If your pass skill set not relevant with the job, find elements of the job.

What can you do for us that someone else can’t?

Show your strengths, how good you are in your skills, your characteristics. How much are you strong for the job.

Why should we hire you?

You should explain new thought will improve company. Be honest, build and maintain relationship with client, keep learning, work with team environment and so on.

What do you look for in a job?

You can say you are looking forward for this job as you like to face different challenges you face. See above question’s answer for the same.

Why are you looking to make a career change?

Explain your interests, skills set, experience be carefully while giving this question answer. It is common to career switch however to show your heavy interest in new career. You can also use your past experience to build your interest.

Why did you leave your last job?

Go with positive thought. Don’t tell against your previous employers. It may be raise bad impression. You can go with some answer like.. You are looking for new learning opportunity.

What are your strengths?

You should use keywords such as Quick learner, result oriented, good team player, work very well under pressure, very creative, and very strong client relationship skills may be appropriate depending on your chosen field.

What are your weaknesses?

Make your sound positive. Such as ‘Want to learn everything in details’, ‘Always exists to learn new thing’ etc.

How would you describe yourself?

You can describe yourself as intelligent, hardworking, quick to learn, enthusiastic, honest, efficient, productive, ambitious person.

How would your colleagues describe you?

Do not bring up anything negative here. You can tell as my colleagues describe me as ever helping and hard working person.

What did you most like/ dislike about your past job?

Don’t tell anything against the employee you has work with. Keep it lightly and in your favour, You will have loved your past job and would like to achieve the success and job satisfaction in a more challenging work.

How do you work under pressure?

You should tell them how can you work under pressure if you have done in past.

What kind of hours would you like to work?

Interviewer want’s to know your flexibility to do work so show your flexibility on your side as well.

Do you have any questions for me?

YES you do. By this you can create a good impression on the Interviewer. Ask questions that show you know something about the company or the job.

***** BEST OF LUCK *****

Auto refresh page without JavaScript

In order to automatically refresh page generally(I use) we set JavaScript and mostly use setTimeout() method.

Here I explain one more method for refreshing page without JavaScript. May you know? Yes, exactly it’s Meta tag.

Put below line into your’ page <head> section. It starts to refreshing page. You can also set redirection after given time interval as.

Content : Give time interval(in seconds) when you want to refresh page or redirect on another page or url.

Refresh Same Window:

<meta http-equiv=Refresh content=’10; URL=/—–.htm’>

Redirect on another page or url:

<meta http-equiv=Refresh content=’10; URL=http://www.sourcecodemart.com’>
<meta http-equiv=Refresh content=’10; URL=/—–1.htm’>

Full example:

SQL IFNULL(), NVL() AND ISNULL() functions

Look, below itemdetails table.

Item Id Item Name Unit Price Unit
1 Item 1 10 2
2 Item 2 30
3 Item 3 50 2

Here, Unit is optional column so it contains either some value or NULL.

Now let’s execute below query

select itemname, (unitprice * unit) as price
from itemdetails

Output should be:

item1 20
item2 NULL
item3 100

In above example, if any of “unit” values are NULL, the result is NULL.

The IFNULL(), NVL() and ISNULL() functions are used to replace NULL values.

Syntex:

IFNULL(UNIT, 0);
NVL(UNIT, 0);
ISNULL(UNIT, 0);

In this case you want NULL values to be zero.

SQL Server / MS Access

SELECT itemname , (unitprice*isNULL(unit,0))
FROM itemdetails

Oracle

Oracle hasn’t ISNULL() OR IFNULL() function. It used NVL() to produce same result:

SELECT itemname , (unitprice*NVL(unit,0))
FROM itemdetails

MySQL

MySQL hasn’t ISNULL() and NVL() function.

If you want to produce same result on MySQL so you need to use IFNULL() function:

SELECT itemname, (unitprice*ifNULL(unit,0))
FROM itemdetails

coalesce in MySql and MSSql

COALESCE function returns first not null value from list of all arguments. If all passed arguments are NULL, COALESCE returns NULL. COALESCE has N number of arguments.

It works same for both MSSql and MySql.

Syntax:

COALESCE(arg1, arg2, arg3 [, argn]….);

How it works?

COALESCE is very easy to use. Let’s see how can you use COALESCE?

SELECT COALESCE(1, NULL, NULL);
— Output: 1
SELECT COALESCE(NULL, 1, NULL);
— Output: 1
SELECT COALESCE(NULL, NULL, 1, NULL, NULL, 3);
— Output: 1
SELECT COALESCE(NULL, NULL, NULL, NULL);
— Output: NULL

Same syntax applied in MsSql and MySql.

COALESCE is useful when you have set database table’ field set NULL default and want to return 0 whenever field value is NULL.

SELECT COALESCE(FIELD_NAME,0) FROM TABLE_NAME;

Note: There is already function exists to check NULL value in both database. isNull in MSSql and ifNull in MySql. 

send data as a POST from one to another page

Today I build redirection function. It is used to send data as a POST from one to another page through PHP. Using this function there is no need to send data as GET. So here we build one step security against hacking.

function redirectionForm($form=”,$form_action=”,$formname=’frmPostBack’){
echo “<html>”;
echo “<body>”;
echo “<form name='{$formname}’ method=’post’ action='{$form_action}’>”;
foreach($form as $key=>$value){
echo “<input type=’hidden’ name='{$key}’ value='{$value}’ />”;
}
echo “</form>”;
echo “<script language=’javascript’ type=’text/javascript’>”;
echo “window.document.{$formname}.submit();”;
echo “</script>”;
echo “</body>”;
echo “</html>”;
die;
}

How to use?

It’s very simple. Just copy n paste above function into your page from where you want to send post data and call it. Like below example.

$_REQUEST[‘msg’] = ‘Operation performed successfully.’;
redirectionForm($_REQUEST,’postpage.php’);

Here, I send all post data in main page with message to know users their submitted action performed successfully. You can set any message weather it is for success or failure.

End Date should not be less than Start Date using jQuery Date Picker

In today’s era every person wants to run report by date range. Most of times we never give date restriction to user. Such as, end date should not be less then start date and same for start date should not be greater than end date. It’s very simple to restrict over date range selection using jQuery Datepicker. It’s a few line of code.

In Above example beforeShow method disabled date as per selection.

A complete code. Click To View Demo

Require to change script and css path as per your jquery js and css directory.

Difference between echo and print

There is slight difference between echo and print. But both are used to print string variables to the browser.

Echo:

  • Echo is language Construct.
  • Echo does not return any value.
  • Echo is faster than Print.
  • Echo supports multiple parameters.
  • Personally advise you to use Echo while printing string variables.

<?php echo “hello”,”world”,”!”; ?>

Print:

  • Print is language function.
  • Print function returns 1 or 0 as per success or failure.
  • Print is slower than Echo. Recommend use echo when print string variables in browser.
  • Print does not support multiple parameters.

<?php print “hello world!”; ?>

Conclusion:

Echo is more recommending than Print. It is advisable to use Echo or Print. Don’t use both in page otherwise person will be confused while reading your code.

The Concatenation Operator in PHP

Every web technology has different concatenation operator. Such as, Java and JavaScript used (+), ColdFusion used (&) and so on…

Same like dot (.) operator used in PHP.

What is use of concatenation operator?

To concatenation two or more string variables together.

Example:

<?php
$var1 = ‘Hello’;
$var2 = ‘SourceCodeMart!’;

// Concatenate both declared PHP variables
echo $var1 . ” ” . $var2;
?>

The output of the code above will be:

Hello SourceCodeMart!

MySql Storage Engines

You may know about MySql storage Engine. Recently, I have taken back up of my one of site and set it up in my local machine and see there are two different storage engines with table. Some have MyISAM and Other have InnoDB. Let’s see what the different between each mysql engine.

MyISAM manages non-transactional tables. It provides high speed for data retrieval and full text searching.
InnoDB manages transactional tables and also support foreign key.

Refer link to know each engine in details.

Features Supported… ScaleDB InnoDB MyISAM Memory NDB
Transactions with rollback Yes Yes No No Yes
Foreign key constraints Yes Yes No No No
Locking level Row Row Table Table Row
Materialized views (faster joins) Yes No No No No
Eliminates data partitioning Yes No No No No
High-performance for long keys Yes No No No No
ACID compliance Yes Yes No No No
Full-text indexing No No Yes No No
Clustering integrated Yes No No No Yes