Thursday, December 11, 2008

meta refresh problem in IE7

<meta equiv="Refresh" content="3;url=http://mysite.com">


this meta tag will redirect to the specified url in 3 seconds.
but it won't work in Internet Explorer 7.

BUT...
we can use a script...

<script language=JavaScript>

function Refresher(t) {
if(t) refresh = setTimeout("document.location='http://www.mysite.com';",
t*1000);
}

</script>

Then call it inside body tag..

<BODY onLoad="Refresher(20)">

Cheers!

Wednesday, December 10, 2008

php explode and implode

Use of Explode and implode functions

Explode - splits a string by a string
eg:
$a = explode("/","23/02/2008");
then
$a[0] = "23"
$a[1] = "02"
$a[2] = "2008"

Implode - joins array elements into string with a string

$b = implode("-",$a);
then
$b = "23-02-2008"


http://in.php.net/manual/en/function.explode.php
http://in.php.net/manual/en/function.implode.php

Getting Last generated auto increment value in mysql

Some tables have auto increment field.

after executing a insert command in mysql, sometimes we may want to know the last added value.... for that we can use
mysql_insert_id()
function

eg:
mysql_query($sql3); //executing some query
$last_num = mysql_insert_id(); //last added value


Cheers!

Wednesday, November 5, 2008

PHP - Mysql, database access

How to get data from mysql database using php?

student Table (consider this table already in database - school and having the following data)
---------------
s_no | name
---------------
1 | Regin
2 | Beschi
3 | Siva
4 | Karthik
----------------

then the code to display data:
$con = mysql_connect("localhost","root",""); //local
//for server - mysql_connect("hostname/ip","username","password")
if($con)
mysql_select_db("school",$con); //database name

$sql = "select * from student";
$rsd = mysql_query($sql);
$total_students = mysql_num_rows($rsd); //total number of records in student table
while($rs = mysql_fetch_array($rsd))
{
echo $rs['s_no']." - ".$rs['name'];
echo "
";
}

mysql_close();
?>

Output:
1 - Regin
2 - Beschi
3 - Siva
4 - Karthik

This is a simple way to connect with a database.

Cheers!

Friday, October 10, 2008

how to change month number to month name?

how to change month no to month name?
link 02 to february or Feb...

function month_name($month_no)
{
$timestmp = mktime(0,0,0,$month_no,1,2008);
return date("M",$timestmp);
}

echo month_name(10); //will return October
?>


Cheers!

Thursday, September 25, 2008

Form validation with javascript

Here is an example for form validation with javascript
<html>
<head>
<title>From Validation</title>
<script language="javascript">
function chkForm(form)
{
if(form.cname.value == ""){ alert("Enter Name"); form.cname.focus(); return false; }
if(form.age.value == "0"){ alert("Select Age Level"); return false; }
}
</script>
</head>
<body>
<div align="center">
<form name="test" action="some_page.php" method="post" onsubmit="return chkForm(this);">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td width="139">Name</td>
<td><input type="text" name="cname" size="20"></td>
</tr>
<tr>
<td width="139">Age</td>
<td><select size="1" name="age">
<option selected value="0">Select</option>
<option value="below 18">below 18</option>
<option value="18+">18+</option>
</select></td>
</tr>
<tr>
<td width="139"> </td>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</form>
</div>
</body>
</html>

Older posts
Radio button validation:
http://phpbeginners.blogspot.com/2008/06/radio-button-validation-in-javascript.html
checkbox validation:
http://phpbeginners.blogspot.com/2008/05/checkbox-validation-in-javascirpt.html

how to change the alert box title?

Want to know how to change the alert box title in javascript...
the answer is: we cant change the title of js alert box.

however, we can use popup boxes, that look like alert boxes with custom title.... but there is a chance - some browsers will block that.
Or we can use vbscript msgBox, but cant get the result in FF.
Or we can use custom designs like this:
http://www.acejs.com/scriptsfolder/121015/121015.html

BUT it is not working like exactly as a alert box. (following codes will get executed though we dont click the ok button)

My opinion: Its better not to use any other custom alert boxes.

Wednesday, August 13, 2008

Change / Increase session timeout in php

Projects using login may have the problem of expiring sessions in short time.
How to increase the session timeout in PHP?

use this as first line - before session_start
ini_set('session.cookie_lifetime',(3600*2));

In some servers it may not work.... BUT using .htaccess file it is working very fine.
Just place these lines in the .htaccess file and put the file in your root directory.
php_value session.cookie_lifetime 3600
php_value session.gc_maxlifetime 3600

and note these parameters are in seconds.
Then how to check the current session timeout?
use this:
printf("cookie: %s, gc: %s", ini_get('session.cookie_lifetime'), ini_get('session.gc_maxlifetime'));

Thats all.

Wednesday, July 23, 2008

Audio Streaming / Playing audio mp3 files in web page - HTML

How to play mp3 files in a web page - as streaming using some players available for free?...

First, lets try with windows media player.
Try this code...
<object id="MediaPlayer" height=46 classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." type="application/x-oleobject" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112">
<param name="filename" value="Bheema1.mp3">
<param name="Showcontrols" value="True">
<param name="autoStart" value="True">
<embed type="application/x-mplayer2" src="http://www.mix26.com/demo/Bheema1.mp3" name="MediaPlayer"></embed>
</object>

will Show this media player:








This one is from google... Google audio player.
<embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http://www.mix26.com/demo/om_zaraare.mp3" width="400" height="27" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" />

will Show:



This is from yahoo...
<embed src='http://webjay.org/flash/dark_player' width='400' height='40' wmode='transparent' flashVars='playlist_url=http://www.mix26.com/demo/marudhaani.mp3&rounded_corner=1&skin_color_1=0,0,0,0&skin_color_2=0,0,0,0' type='application/x-shockwave-flash' pluginspage='http://www.adobe.com/go/getflashplayer'/>

will Show:



And this one is very cute...
Add just a line (javascript) in your page and get the player...

http://mediaplayer.yahoo.com/

Cheers!

Random Number / Alpha Numeric Password Generation in PHP

To generate random no.s we can use rand() function...
try,
echo ran() ; //will generate random no between 0 and32768
echo rand(5,200); //a random no between 5 and 200

To generate alpha numeric random no - uniqid()...
try,
echo uniqid(rand(), true);
you can get a number like:
274134886dbd37292e8.16885304 (32 digit)

To generate alpha numeric passwords for user accounts use this:
$password = substr (MD5(uniqid(rand(),1)), 3, 10); //10 digit random alpha numeric password

Reference:
http://in.php.net/manual/en/function.rand.php
http://in.php.net/manual/en/function.uniqid.php
http://in.php.net/manual/en/function.md5.php

Cheers!

Friday, July 4, 2008

Doctype - margin / padding problem with HTML

Some may come across the problem...

<!DOCTYPE html PUBLIC....

if you use any doctype in our page, then some unwanted padding/margin will appear with images / tables / td s will be added automatically in your page....

You want to remove them...? Then add this css code in to your page

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, code,
del, dfn, em, img, q, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
}

Thats all...
Cheers!

Saving Textarea value with enter / Line feed / Line break in PHP

In save forms into database... the value in the text area cant be saved with the line feed that user gives.

To get them we can use the following function...

consider the textarea name is description.... then we can convert the line feed in the form of '\n' or '\r' into <br />

$desc = $_REQUEST['description'];
$desc = ereg_replace("(\r\n|\n|\r)", "<br />", $_desc);


Cheers!

Friday, June 27, 2008

Call Functions across parent and child windows using iframe

how to call child window(in iframe) function from parent window?
how to call parent window function from child window(in iframe)....

Simple.
From parent window.... framename.child_function_name()

From child window... parent.parent_function_name()


Demo

Download


Cheers!

Div over flash problem in HTML

Some may have this problem.
Always the flash object looks like placed in front of all the objects. So if we want to put a div in front of flash/ over flash we cant.

But there s a solution...
The normal flash object....
<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="519" height="255">
<param name="movie" value="center_flash.swf">
<param name="quality" value="High">
<embed src="center_flash.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="obj1" width="519" height="255" quality="High">
</object>

To make it back of all elemnts add
<param name="wmode" value="transparent">


So the actual code we want is...
<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" border="0" width="519" height="255">
<param name="movie" value="center_flash.swf">
<param name="quality" value="High">
<param name="wmode" value="transparent">
<embed src="center_flash.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="obj1" width="519" height="255" quality="High">
</object>

Demo

Download

Cheers!

Wednesday, June 18, 2008

Radio button validation in Javascript (HTML)

<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Check Test</title>
<script language="javascript">
function valBox()
{
var x = document.chkForm;
var i, flg = 0;

for( i=0; i<x.grp.length; i++ )
{
if(x.grp[i].checked)
flg=1;
}
if(!flg)
{
alert("Select One");
return false;
}
}
</script>
</head>
<body>
<form name="chkForm" method="POST" action="#" onsubmit="return valBox();">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td><input type="radio" value="V1" name="grp">1</td>
</tr>
<tr>
<td><input type="radio" value="V2" name="grp">2</td>
</tr>
<tr>
<td><input type="radio" value="V3" name="grp">3</td>
</tr>
<tr>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Friday, May 30, 2008

Number to currency format conversion - Indian Rupees - in PHP

The following code can be used to convert the number format into indian currency format (Indian rupees, INR) using PHP.

<?php
ob_start();
//Currency format conversion - Indian Rupee
$n = "1000000";
$len = strlen($n); //lenght of the no
$num = substr($n,$len-3,3); //get the last 3 digits
$n = $n/1000; //omit the last 3 digits already stored in $num
while($n > 0) //loop the process - further get digits 2 by 2
{
$len = strlen($n);
$num = substr($n,$len-2,2).",".$num;
$n = round($n/100);
}
echo "Rs.".$num;
?>

Friday, May 23, 2008

No of days between two dates in php

We can find the no days between two dates using PHP....
There are 2 methods:

1) Split the date and subtract
$date1 = "2008-05-18";
$arr = explode('-',$date1); //split the date
$date2 = $arr[2]; //stores 18
$cur_date = date("d"); // current date
echo "Difference: ";
echo $cur_date-$date2;



2) Convert dates into timestamp, subtract and then convert as we want
$date1 = "2008-04-18";
$to_day = date("Y-m-d"); //current date
$day_diff = (strtotime($to_day)-strtotime($date1))/86400; //60*60*24=86400
echo $day_diff;

//change the divider(864000 to get ur corresponding difference
$hour_difference = (strtotime($to_day)-strtotime($date1))/3600; //60*60=3600
$week_diff = (strtotime($to_day)-strtotime($date1))/604800; //60*60*24*7=604800

Monday, May 19, 2008

Checkbox array problem with Javascript and PHP

Check this solution...
Here we can use the checkbox array.... no problem with both php get data and javascipt validation.


<?php
if(isset($_REQUEST['lang']))
{
$lang=$_REQUEST['lang'];
foreach ($lang as $language)
{
echo "$language is checked <br />";
}
echo "Selected Array: ";
print_r($lang);
}
?>
<html>
<head>
<title>TEST</title>
<script language="javascript">
function chkThis()
{
var chkList = document.chkForm['lang[]'];
for(var i = 0 ; i < chkList.length ; i++)
{
if(chkList[i].checked)
alert(chkList[i].value);
}
}
</script>
</head>
<body>
<form method="post" action="#" name="chkForm" onsubmit="return chkThis();">
Please choose language:<br />
<input type="checkbox" name="lang[]" value="JAVA">JAVA><br />
<input type="checkbox" name="lang[]" value="VB">VB<br />
<input type="checkbox" name="lang[]" value="PHP">PHP<br />
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

Friday, May 16, 2008

Checkbox validation in Javascirpt

Try this:
Change checkbox values and click button.....

<form name="test">
<input type="checkbox" name="checkgroup" checked />
<input type="checkbox" name="checkgroup" />
<input type="checkbox" name="checkgroup" checked />
<input type="button" value="Button" name="B1" onclick="check_boxes();"></form>

<script type="text/javascript">
function check_boxes()
{
for (i=0; i<document.test.checkgroup.length; i++){
if (document.test.checkgroup[i].checked==true)
alert("Checkbox at index "+i+" is checked!")
}
}
</script>


From: http://www.javascriptkit.com/jsref/checkbox.shtml

Wednesday, May 7, 2008

Changing Max upload file size in php

We can change the maximum file size of uploading in 2 ways.

1) using ini_set()
if you want to set it to 8 MB try: ini_set('max_upload_filesize', 8388608);
and not : ini_set('max_upload_filesize', 8M);

2) or using .htaccess file
in .htaccess file put this line:
php_value upload_max_filesize 8M
php_value post_max_size 8M

Friday, April 25, 2008

Change the colour of scrollbars using css

This code will change the color or scroll bar of the window:

body {
scrollbar-3dlight-color:#ffd700;
scrollbar-arrow-color:#ff0;
scrollbar-base-color:#ff6347;
scrollbar-darkshadow-color:#ffa500;
scrollbar-face-color:#008080;
scrollbar-highlight-color:#ff69b4;
scrollbar-shadow-color:#f0f
}



Will work in IE, but not in FF.

From:
http://websitetips.com/articles/css/scrollbars/

Tuesday, April 22, 2008

Use of === and !== operators in PHP

=== (identical)
$a === $b : TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)

!== (Not identical)
$a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.

Example:
<?php
$a = "1"; //String
$b = 1; //Integer
if($a == $b)
echo "Equal<br />";
else
echo "Not Equal<br />";

if($a === $b)
echo "Identical<br />";
else
echo "Not Identical<br />";
?>

Output:
Equal
Not Identical


From: http://in.php.net/manual/en/language.operators.comparison.php

Wednesday, April 16, 2008

Changing / Setting Server Timezone in PHP

<?
session_start();
ob_start();
echo '1 '.date("d-m-Y H:i:s"); // Default server time zone

putenv("TZ=Asia/Calcutta"); //change your server time zone - this one is for India - works in PHP 4
// also you can try: date_default_timezone_set('Asia/Shanghai'); - since PHP 5.1.0

echo '2 '.date("d-m-Y H:i:s");

//Reference:
// http://in.php.net/manual/en/function.date.php
// http://in.php.net/manual/en/timezones.php
// http://in.php.net/manual/en/function.date-default-timezone-set.php
?>

Getting Checkbox Values in PHP

<?php
if(isset($_REQUEST['lang']))
{
$lang=$_REQUEST['lang'];
foreach ($lang as $language)
{
echo "$language is checked <br />";
}
echo "Selected Array: ";
print_r($lang);
}
?>
<html>
<head>
<title>TEST</title>
</head>
<body>
<form method="post" action="#">
Please choose language:<br />
<input type="checkbox" name="lang[]" value="JAVA">JAVA<br />
<input type="checkbox" name="lang[]" value="VB">VB<br />
<input type="checkbox" name="lang[]" value="PHP">PHP<br />
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

Tuesday, April 15, 2008

HTML - CSS - Round corner table - Curve corners

<html>
<head>
<title>Test</title>
<style>
b.rtop, b.rbottom{display:block;}
b.rtop b, b.rbottom b{display:block;height: 1px;overflow: hidden; background: #9BD1FA}
b.r1{margin: 0 5px}
b.r2{margin: 0 3px}
b.r3{margin: 0 2px}
b.rtop b.r4, b.rbottom b.r4{margin: 0 1px;height: 2px}
</style>
</head>
<body>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td><b class="rtop"><b class="r1"></b><b class="r2"></b><b class="r3"></b><b class="r4"></b></b></td>
</tr>
<tr>
<td bgcolor="#9BD1FA"> </td>
</tr>
<tr>
<td><b class="rbottom"><b class="r4"></b><b class="r3"></b><b class="r2"></b><b class="r1"></b></b></td>
</tr>
</table>
</div>
</body>
</html>

Difference between echo and print in PHP

1. Speed.  There is a difference between the two, but speed-wise it
should be irrelevant which one you use. echo is marginally faster
since
it doesn't set a return value if you really want to get down to the
nitty gritty.

2. Expression. print() behaves like a function in that you can do:
$ret = print "Hello World"; And $ret will be 1. That means that print
can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:

$b ? print "true" : print "false";

print is also part of the precedence table which it needs to be if it
is
to be used within a complex expression. It is just about at the bottom
of the precedence list though. Only "," AND, OR and XOR are lower.

3. Parameter(s). The grammar is: echo expression [, expression[,
expression] ... ] But echo ( expression, expression ) is not valid.
This would be valid: echo ("howdy"),("partner"); the same as: echo
"howdy","partner"; (Putting the brackets in that simple example
serves
no purpose since there is no operator precedence issue with a single
term like that.)

So, echo without parentheses can take multiple parameters, which get
concatenated:

echo "and a ", 1, 2, 3; // comma-separated without parentheses
echo ("and a 123"); // just one parameter with parentheses

print() can only take one parameter:

print ("and a 123");
print "and a 123";

From:
http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

Wednesday, March 12, 2008

Sending HTML Mail in PHP

To send HTML mail in php use the following code...

$to = 'some@example.com';
$subject = Testing Html Mail;
$message = '
// HTML code here
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Somebody ,' . "\r\n";
$headers .= 'From: Me ' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);


- This is not an Authentication Mail.

Sending simple text mail using PHP

To send simple text mail use the following code:

If your server supports php mail you can see Mail Sent.
If it allows only Authentication Mail then you can see Error.