November 07, 2009

Javascript String truncate function

Javascript String truncate function, This function helps us to truncate the given string to the particular given character length.

function truncate(text, length, ellipsis) {

// Set length and ellipsis to defaults if not defined
if (typeof length == 'undefined') var length = 100;
if (typeof ellipsis == 'undefined') var ellipsis = '...';

// Return if the text is already lower than the cutoff
if (text.length < length) return text;

// Otherwise, check if the last character is a space.
// If not, keep counting down from the last character
// until we find a character that is a space
for (var i = length-1; text.charAt(i) != ' '; i--) {
length--;
}

// The for() loop ends when it finds a space, and the length var
// has been updated so it doesn't cut in the middle of a word.
return text.substr(0, length) + ellipsis;
}

var some_text = 'This is example text This is example text This is example text This is example text This is example text ';

Call to the above function is,
truncate(some_text, 10);

Output,

This is ex...

October 13, 2009

The names of the three wise monkeys?

They are:
1. Mizaru(See no evil)
2. Mikazaru(Hear no evil)
3. Mazaru(Speak no evil)

October 12, 2009

Function to check file exists in javascript

This function returns true if the file exists in the given URL. The below mentioned code is optimized to be use with Google Chrome also.
Instead of "return true, return false", "return 1, return 0" is used - this is because Google chrome avoids false and true statements.

function file_exists (url) {
// Returns true if filename exists

var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (!req) {throw new Error('XMLHttpRequest not supported');}
// HEAD Results are usually shorter (faster) than GET
req.open('HEAD', url, false);
req.send(null);
if (req.status == 200){
return 1;
}
return 0;
}

September 19, 2009

date = date + 30 days in mysql

Add 30days to the current date and update in mysql. Try this code in PHP to get the future date (30 days interval),

<?php

$current_date = date("d-m-Y");
list($day, $month, $year) = split("-", $current_date);
$timeStamp = mktime(0,0,0,$month,$day,$year);
$timeStamp = $timeStamp + (30 * 24 * 60 * 60); #Add 30 days, in seconds
$future_date = date("d-m-Y", $timeStamp); #output the result

echo $future_date;

?>

August 22, 2009

Joins in Mysql

Joins in Mysql,

INNER JOIN - only rows satisfying selection criteria from both joined tables are selected.

LEFT OUTER JOIN - rows satisfying selection criteria from both joined tables are selected as well as all remaining rows from left joined table are being kept along with Nulls instead of actual right joined table values.

RIGHT OUTER JOIN - rows satisfying selection criteria from both joined tables are selected as well as all remaining rows from right joined table are being kept along with Nulls instead of actual left joined table values.

FULL OUTER JOIN - rows satisfying selection criteria from both joined tables are selected as well as all remaining rows both from left joined table and right joined table are being kept along with Nulls instead of values from other table.


Reference : http://www.gplivna.eu/papers/sql_join_types.htm

August 07, 2009

What is difference between TRUNCATE & DELETE in Mysql?

TRUNCATE is a DDL command and cannot be rolled back. All of the memory space is released back to the server.
DELETE is a DML command and can be rolled back.

Both commands accomplish identical tasks (removing all data from a table), but TRUNCATE is much faster.

TRUNCATE : You can't use WHERE clause
DELETE : You can use WHERE clause