Tuesday, 18 October 2011

PHP function for Auto select birth date month year


An user-defined function saves  us from  rewriting the same code again and again and helps us to make our application smaller. The given examples will help to understand how you can put them to work.
PHP has so many built-in functions, besides  that you can make your own functions as per the need. General structure of the user defined function is:
function function_name([mixed $var,.....]){ PHP code}
mixed is the datatype that means it could be of any datatype.
$var: is the name of the parameter, one function could have one,  more than one or no parameter.






1. PHP user define function to print birth Month , Date , Year .

Download Files


Html code
<tr height="20">
            <td align="right" class="normaltext">&nbsp;</td>
            <td align="right" class="normaltext"><div align="left">Date Of Birth:</div></td>
            <td align="left"><select name="month" id="month"  style="width:80px" class="whiteselectsmall">
                <?php print month_opt(get_month($uid)); ?>
              </select>
              <select name="day" id="day" style="width:40px" class="whiteselectsmall">
                <?php print set_day($uid);?>
              </select>
              <span class="normaltext">
              <select name="year" id="day" style="width:80px" class="whiteselectsmall">
              <?php print set_year($uid);?>
              </select>
              </span> </td>
          </tr>

PHP code
<?php 
 
//Database Information

$dbhost = "localhost";
$dbname = "pk";
$dbuser = "root";
$dbpass = "";

//Connect to database

mysql_connect ($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$uid =1; // or you use session id   or  database table has ids 1,100,101 please change $uid value andrefresh page too see change....

?>  

<?php 
    
function get_month($user_id)
{
$query="select  bday from user where id='$user_id'";
 $res=mysql_query($query) or die(mysql_error());
 $row=mysql_fetch_array($res);
 $str=$row[0];
 $select=explode("-",$str);
 return $select[1];

}
    function month_opt($mon)
{
 $m=array(Month, Jan, Feb, Mar, April, May, Jun, July, August, Sep, Oct, Nov, Dec);
 for($i=1;$i<13;$i++)
 {
   if($i==$mon)
   $opt .= "<option value='$i' selected>$m[$i]</option>";   
   else
   $opt .= "<option value='$i'>$m[$i]</option>";
 }
 return $opt;
}


function set_day($user_id)
{
 $query="select  bday from user where id='$user_id'";
 $res=mysql_query($query) or die(mysql_error());
 $row=mysql_fetch_array($res);
 $str=$row[0];
 $select=explode("-",$str);
 
 for($i=1;$i<32;$i++)
 {
   if($i==$select[2])
   { 
     $opt .= "<option value='$i' selected>$i</option>";
   }
   else
   {
     $opt .= "<option value='$i'>$i</option>";
  //dsfl    
   }
 }
 return $opt;
}


function set_year($user_id)
{
// $user_id;
 $query="select  bday from user where id='$user_id'";
 $res=mysql_query($query) or die(mysql_error());
 $row=mysql_fetch_array($res);
 $str=$row[0];
 $select=explode("-",$str);

 $dat=date("Y");
 $start=$dat-80;
 for($i=$start;$i<$dat;$i++)
 {
   if($i==$select[0])
   { 
     $opt .= "<option value='$i' selected>$i</option>";
   }
   else
   {
     $opt .= "<option value='$i'>$i</option>";
  //dsfl    
   }
 }
 return $opt;
}
    ?>

Mysql code



-- Table structure for table `user`
-- 

CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `uname` varchar(50) collate latin1_general_ci NOT NULL,
  `email` varchar(50) collate latin1_general_ci NOT NULL,
  `password` varchar(50) collate latin1_general_ci NOT NULL,
  `bday` date NOT NULL,
  `state` varchar(50) collate latin1_general_ci NOT NULL,
  `city` int(11) NOT NULL,
  `college` int(11) NOT NULL,
  `collstatus` int(11) NOT NULL,
  `collyear` int(11) NOT NULL,
  `time_zone` varchar(50) collate latin1_general_ci NOT NULL,
  `question` varchar(100) collate latin1_general_ci NOT NULL,
  `answar` varchar(50) collate latin1_general_ci NOT NULL,
  `blocked` enum('0','1') collate latin1_general_ci NOT NULL,
  `deactive` enum('0','1') collate latin1_general_ci NOT NULL default '0',
  `couse` text collate latin1_general_ci NOT NULL,
  `confirm` enum('0','1') collate latin1_general_ci NOT NULL default '0',
  `ccode` varchar(100) collate latin1_general_ci NOT NULL,
  `second_lastlogin` datetime NOT NULL,
  `lastlogin` datetime NOT NULL,
  `p_view` int(11) NOT NULL,
  `online` enum('0','1') collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=118 ;

-- 
-- Dumping data for table `user`
-- 

INSERT INTO `user` VALUES (1, 'user', 'user@user.com', 'user', '1982-08-22', '', 1, 1, 0, 0, '', '3', 'morning', '0', '0', 'sorry', '1', '', '2007-07-05 11:47:25', '2007-07-05 18:57:37', 19, '0');
INSERT INTO `user` VALUES (100, 'henery', 'henery@yahoomail.co.in', 'henery125', '1975-12-27', '5', 8, 0, 0, 0, '', '1', 'dog', '0', '0', '', '0', '5cdef582a2', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, '0');
INSERT INTO `user` VALUES (101, 'jax', 'jax@gamil.com', 'jax12345', '1982-07-25', '7', 19, 0, 0, 0, '', '4', 'joli', '0', '0', '', '0', '7e65a9b554', '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, '0');






No comments:

Post a Comment