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

July 18, 2009

Total number of php built-in functions

Hi all, you know that the total number of php built-in functions available is 1263

get_defined_functions() will return the built-in function list in php

To view those function list just execute the following code and have a look on yours

<?php

$functions_list = get_defined_functions();

$i = 1;
foreach($functions_list['internal'] as $function_name) {
echo $i . “. ” . $function_name . “<br/>”;
$i++;
}

?>

June 22, 2009

Unable to find the socket transport “ssl” - did you forget to enable it when you configured PHP?

Last week while implementing google docs API i got this error "Unable to find the socket transport “ssl” - did you forget to enable it when you configured PHP?". I searched for the solution and fixed at last.

Here is the solution to the above problem,

uncommented the line in php.ini
;extension=php_openssl.dll

Other related blog post

http://www.boringguys.com/2007/07/20/unable-to-find-the-socket-transport-ssl-did-you-forget-to-enable-it-when-you-configured-php/

"Ran into this error recently… here’s how to fix it, you have to install OpenSSL on your system. For OpenSSL your PHP config values look like this:

–with-openssl[=DIR] Include OpenSSL support (requires OpenSSL >= 0.9.6)

So, if you’ve compiled from scratch, you can just recompile adding this flag to your configure command."

May 21, 2009

Jcrop plugin for image crop in jQuery

Jcrop is a cross-browser jQuery image crop plugin, a quick and easy way to add image cropping functionality to your web application. It combines the ease-of-use of a typical jQuery plugin with a powerful cross-platform DHTML cropping engine..







Some features of Jcrop:

* Attaches simply to any image in your HTML page
* Supports aspect ratio locking
* Callbacks for selection done, or while moving
* Keyboard support for nudging selection
* Support for CSS styling
* Advanced API including animation support

An example of this plugin here & a sample PHP code to complete the server-side of the crop here.

Requirements: jQuery
Compatibility: All Major Browsers
Website: http://deepliquid.com/content/Jcrop.html
Demo: http://deepliquid.com/content/Jcrop_Examples.html

May 15, 2009

Solved problem with setAttribute () in javascript

This week i faced a problem with setAttribute() in javascript, i used this to set a border for an <li> tag when the particular image is clicked.
i used like this,

var LiObj = document.getElementById('img_1');
LiObj.setAttribute("style", "border: 2px solid red;");

this one perfectly works in Mozilla Firefox (since the setAttribute works fine in FF). Same opposite in IE, IE does not support setAttribute(). At this stage what i will do?

i modified the above code like this,

var LiObj = document.getElementById('img_1');
LiObj.style.border = '2px solid red';

This is an simple issue when we look at outside, when there is an deadline on any project this small design issue can trouble your head to lose the hair.

try like this tweaks to fix the browser compatible.

May 08, 2009

Brendan Eich Father of Javascript


Brendan Eich (born 1961) is a computer programmer and creator of the JavaScript programming language. He is the Chief Technology Officer at the Mozilla Corporation.

April 25, 2009

Send mail with attachment in php

This code will help you with attachment of files in anytype in sending a mail with php

include this input field inside "form" tag
<input type="file" name="fileatt" size="22" />
<input type="submit" name="career_submit" size="22" />

When the page is submitted use the following code to send the mail to the receipent with the attachment.

if(isset($_REQUEST['career_submit'])) {
/*@@@@@@@@@@@--- File Type --- @@@@@@@@@@@*/
$fileatt_name = $_FILES['fileatt']['name'];
$fileName = $_FILES['fileatt']['tmp_name'];
$fileatt_type = "application/octet-stream";
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

$email_from = $_REQUEST["email"]; // Who the email is from
$email_subject = "Mail Subject"; // The Subject of the email
$email_to = "example@example.com"; // Who the email is too

$headers = "From: ".$email_from;

/*@@@@@@@--- File Permissions --- @@@@@@@@*/
$file = fopen($fileName,'rw');
@$data = fread($file,filesize($fileName));
@fclose($file);
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

$email_message .= 'Your Message.....'; // Mail Content Comes Here

$email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);
}

April 24, 2009

Allow only selected filetype in attachment using javascript

This code in javascript checks the attachment type,

You can add this code in javascript validation on form submit event to upload only specific file types,

if(document.formname.fieldname.value != "") {
MyFile = document.formname.fieldname.value /* replace with the correct value */
FileArray = MyFile.split("\\")
FileName = FileArray[FileArray.length-1]
ExtArray = FileName.split(".")
Ext = ExtArray[ExtArray.length-1]
Ext = Ext.toUpperCase(Ext)

if(!(Ext=="DOCX" || Ext=="DOC" || Ext=="TXT" || Ext=="PDF" || Ext=="RTF"))
{
alert("Invalid Upload File! Upload doc or txt or pdf or rtf file only")
return false
}
}

Explanation of the code:

The selected file for attachment is assigned to the variable "MyFile". With the build-in javascript split() the filename and the file extensions are splitted. To allow browser compatible converting the file extension to uppercase using "toUpperCase" function. When the if condition satisfied "return false" to terminate the form submission.

April 23, 2009

Max length for Mysql database TEXT types

MySQL supports 4 TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT) and this post looks at the maximum length of each of these field types.

MyISAM tables in MySQL have a maximum size of a row of 65,535 bytes, so all the data in a row must fit within that limit. However, the TEXT types are stored outside the table itself and only contribute 9 to 12 bytes towards that limit. (For more information about this refer to the MySQL Manual - Data Storage Requirements chapter).

TEXT data types are also able to store much more data than VARCHAR and CHAR text types so TEXT types are what you need to use when storing web page or similar content in a database.

The maximum amount of data that can be stored in each data type is as follows:
TINYTEXT 256 bytes
TEXT 65,535 bytes ~64kb
MEDIUMTEXT 16,777,215 bytes ~16MB
LONGTEXT 4,294,967,295 bytes ~4GB

In most circumstances the TEXT type is probably sufficient, but if you are coding a content management system it's probably best to use the MEDIUMTEXT type for longer pages to ensure there are no issues with data size limits.

April 22, 2009

Enter only numbers in a text field using javascript

We can able to restrict entering special characters inside the text input field in a form

Call "forceNumber(event)" function the "onKeyPress" event on input field like this,

<input name="text" maxlength="17" onkeypress="return forceNumber(event);" />

Use the following function to check in javascript itself


<script language="javascript">
function forceNumber(event){
var keyCode = event.keyCode ? event.keyCode : event.charCode;
if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 32 && keyCode != 37 && keyCode != 39 && keyCode != 40 && keyCode != 41 && keyCode != 43 && keyCode != 45 && keyCode != 46)
return false;
}

</script>


This function will allow numbers from 0-9 and characters "+ ()-"
and it will be more useful in telephone number validation inside a form

Note: the function "forceNumber()" should be inside <head>.