February 23, 2007

Show Hide elements

Here are some JavaScript Functions for Showing or Hiding block(s) of HTML elements on any javascript event like: onClick, onMouseOver, onBlur etc etc...

These functions can be used to make good effects on the HTML pages.
These functions are Browser compatible, To use, you just need to pass the ID(s) of the elements(s) you want to show/hide with some event on client side.


Alter Display: It checks the current display status of the element with the ID passes, and alters that. If element is displaying on screen, it will be hide and vice-versa.
//input -> ID of the HTML element (DIV, SPAN, TABLE, TR etc)
function alterDisplay(id)
{
if(document.getElementById(id))
{
e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = "";
else
e.style.display = "none";
}
}


hideIDs(): This function hides all the IDs passed in one go.
It accepts the multiple IDs in comma separated form.
Note: Input is one string only, there is no space between comma (,) & ID
//input -> comma separated IDs of the HTML element (DIV, SPAN, TABLE, TR etc)
//e.g. 1 -> hideIDs('div1,div2,tr1'); //Multiple IDs
//e.g. 2 -> hideIDs('div1'); //Single ID

function hideIDs(IDs)
{
var IDs = IDs.split(',');
for(i=0;i < IDs.length;i++)
{
e = document.getElementById(IDs[i]);
e.style.display = "none";
}
}

showHiddenIDs():
This function shows (un-hide) all the IDs passed in one go. It accepts the multiple IDs in comma separated form.

Note: Input is one string only, there is no space between comma (,) & ID
//input -> comma separated IDs of the HTML element (DIV, SPAN, TABLE, TR etc)
//e.g. 1 -> showHiddenIDs('div1,div2,tr1'); //Multiple IDs
//e.g. 2 -> showHiddenIDs('div1'); //Single ID

function showHiddenIDs(IDs)
{
var IDs = IDs.split(',');
for(i=0;i < IDs.length;i++)
{
e = document.getElementById(IDs[i]);
e.style.display = "";
}
}

February 22, 2007

Print This Page

This JavaScript function is very useful where we have "Print this page" button.
Example: Print your invoice

Conventionally, the logic for implementing the functionality is:
1. Whole of the page is displayed [with a button(Print) on top/bottom ]
2. This generated page could be a Static HTML page, or a Dynamic page(Created in scripting language like PHP, ASP, C#.net etc).
3. On clicking on "Print" button, a new window(or pop-up) is opened, where full code for generating the same HTML (for required part of page) is written again.
4. Then page is printed.

With this function, You just need to:
1. Have all the required part (needed to print) into a DIV tag.
2. OnClick of the "Print" Button call this function & pass the ID of DIV tag.
3. It's done :)

function PrintThisPage(print_div_id)
{
var popup_content = document.getElementById(print_div_id).innerHTML;
var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
sOption+="scrollbars=yes,width=620,height=460,left=100,top=25";

var winprint=window.open("","",sOption);
winprint.document.open();
winprint.document.write(popup_content);

winprint.print();
winprint.document.close();
winprint.focus();
}

This is tested in Mozilla FireFox, Internet Explorer.
Hopefully it will work in all other browsers too.

Random Password

This function returns you the Random password (text) of the required length. Default password length is given 8. This function is useful to give a temporary password, in case of 'User Registeration' and/or 'Forgot password'


function getRandomPassword($length=8)
{
$chars = "abcdefghijkmnopqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 1;
$pass = '' ;

#Default: 8 digit password
while ($i <= $length)
{
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;}
return $pass;
}

February 21, 2007

Make Clickable Links

This is very small & useful function that finds & converts all the Email text, URL text to a clickable text. You just need to pass the full text & use the returned text.

Here is the code & usage example


### Use: To convert text parts(Email address, URLs) to clickable text
### Author: Akash Dwivedi
### Date: 27 June 2005

  1. function makeClickableLinks($text)
  2. {
  3. $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  4. '\\1">\\1', $text);
  5. $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
  6. '\\1\\2">\\2', $text);
  7. $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
  8. '\\1">\\1', $text);
  9. return $text;
  10. }

# Example Usage
#Email address example
$text = "Mail me: you@example.com";
echo makeClickableLinks($text);

# URL example
$text = "My URL is: http://www.tech-akash.blogspot.com";
echo makeClickableLinks($text);

# FTP URL example
$text = "FTP is: ftp://ftp.example.com Here is the trailing text";
echo makeClickableLinks($text);
?>

I hope this is useful.

SQL Server 2000 Vs MYSQL v4.1 ( Limits)

Here you can find comparison of Limits.. between 'SQL Server 2000' & 'MySQL 4.1'


Feature SQL Server 2000 MySQL 4.1
column name length 128 64
index name length 128 64
table name length 128 64
max indexes per table 250 32
index length 900 1024
max index column length 900 255
columns per index 16 16
max char() size 8000 1048543
max varchar() size 8000 1048543
max blob size 2147483647 1048543
max number of columns in GROUP BY Limited only by number of bytes (8060) 64
max number of columns in ORDER BY Limited only by number of bytes (8060) 64
tables per SELECT statement 256 31
max columns per table 1024 2599
max table row length 8036 65534
longest SQL statement 16777216 1048574
constant string size in SELECT 16777207 1048565