Saturday, February 28, 2009

How to check for a valid date using php

php have given the checkdate() function to validate user inputted dates.
checkdate($month, $day, $year) all parameters must be integer (numbers).


<?php

$month = $input_month; // should be a number
$day = $input_day; // should be a number
$year = $input_year; // should be a number

if(checkdate($month, $day, $year))
{
echo 'Valid';
}
else
{
echo 'Not valid';
}

?>

Sunday, February 22, 2009

Get width and height of a swf / flash file using php

You can use getimagesize() function to get width and height of a swf /flash file.
getimagesize() is also used to get size of images.
To find out supported formats and return values go to this link
http://www.php.net/getimagesize

Following code is a simple example on how to get the width and height of a swf file.


list($width, $height) = getimagesize("file_name.swf");

Sunday, February 15, 2009

Right date format to store in mysql - YYYY-MM-DD HH:MM:SS

When using the php date function you can format the date in lot of ways.
But when you store date and time in mysql it is really convenient to store date in the mysql friendly format YYYY-MM-DD HH:MM:SS
If you use the above format you can easily use mysql date functions to manipulate and do calculation on stored dates without using php functions.

Example functions
DATEDIFF() - Subtract two dates
ADDDATE() - Add dates
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Tuesday, February 10, 2009

How to create foreign key in mysql (InnoDB)

Yes you can definitely create foreign key constraints on mysql. but it can only be enforced in InnoDB storage engine. The default storage engine is MyISAM, you cannot create foreign key constraints in MyISAM. So make sure you create your tables using InnoDB storage engine.
If you are using a phpMyAdmin to work with mysql, look at the following ScreenShot.



Remember, before making a column available for referencing you should make sure that child column has a Index.





Now using Relation view link you can easily create foreign key constraints



Thursday, February 5, 2009

How to redirect a page in Dropdown (select) onchage

Sometimes you need to redirect or refresh a page when you select a new option in a dropdown box (select box) with some url parameters (query string).
Look at following code and try it yourself.



<html>
<head>

<title>DropDown Redirect OnChange</title>

<script type="text/javascript">

function chng_page()
{
/*
get the selected option index
it start with 0
*/
var select_index = document.getElementById('names').selectedIndex;
// use alert(select_index); see the value

//get the value of selected option
var option_value = document.getElementById('names').options[select_index].value;

//redirect with value as a url parameter
window.location = 'page.php?id=' + option_value;
}
</script>

</head>

<body>

we are using onchange event to
call a javascript function
<br />
<select name="names" id="names" onchange="chng_page()">
<option value="0">select</option>
<option value="1">kuppiya.com</option>
<option value="2">kusal</option>
</select>

</body>
</html>

Sunday, February 1, 2009

How to add a html
tags to newline characters in php - nl2br()

The easiest way to add a HTML break
to all new lines in a string is to use the php nl2br() function. This is really great when you want to restore new lines from a user input by a textarea where user input could get into several lines.



<?php

echo nl2br("line1\n line2");

?>