Jump to content
xisto Community
Sign in to follow this  
websaint

The bithday script!! Quite handy accually

Recommended Posts

Hi!! I just wanted to show you a nice birthday script I've created. You may use this on your site to impress your visitors and friends. This script will atomaticly write " Happy birthday ...."

It could be very nice and helpful if you're no good at remembering birthdays.

Here comes the script:

 

<?

/*

Sarah's birtday is on the date 11-28 (the 28th of november)

Connor's birthday is on the date 12-12 (the 12th of desember)

Mary's birthday is on the date 1-1 (the 1th of january)

 

The script is put wherever you what the congratulation to appear.

*/

 

//add the birthdays like this MM-DD, in the same order as $name

$day = array("11-28", "12-12", "1-1");

 

//Here you'll have to put the names in the same order as $day

$name = array("Sarah", "Connor", "Mary");

 

$today=date("m-d");

 

$number = count($day);

 

for ($k=0;$k<$number;$k++)

{

if(strcasecmp($day[$k],$today)==0)

{

echo "<p>Happy birthday $name[$k]!</p>";

}

}

?>

 

That's it! Just copy and paste the script, but you of course have to edit what's marked in red. The orange stuff is just my comments. You don't have to copy that. Hope you can make the script work!! :)

Share this post


Link to post
Share on other sites

It's a good and fast script but why do you use for?Another method is whith while-list, so you haven't to use another Varwhile(list($narr,$varr) =each ($day)){ //remember to put the same date format in the array and in the $today var...if($varr==$today) echo"<p>Happy birthday $name[$narr]!</p>";}It's shorter and without the K var, 'njoy :)

Share this post


Link to post
Share on other sites

+ i'd advise to get a better way to store the birthday dates. a seperate file or so. and then read them to a table with key = name and value birthday. so you can easily add others afterwards.or store them in a database! even better!

Share this post


Link to post
Share on other sites

Thanks all. We may record all the birthdays of friends into the database. Using similar script we will be informed if the days are coming. To have more time to prepare the gift for them, we need be noticed several days before their birthday.I will try it and post the involved script here.

Share this post


Link to post
Share on other sites

In the light of the above authors, I wrote the script with php and mysql, all the data stored in MySql database.

Structure of table:
CREATE TABLE `birthdays` (
`id` int(10) NOT NULL auto_increment,
`name` varchar(200) NOT NULL default '',
`birth` varchar(5) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=24 ;

php script:<?php
//connect to Mysql database
  $dbh=mysql_connect ("localhost", "<user>", "<password>")
  mysql_select_db ("<database>");

//fetch the data from table birthdays into array name and day.
  $query=    "SELECT name,birth FROM birthdays";
  $result = mysql_query($query);
  for($id = 1; $row = mysql_fetch_row($result); $id ++)
  {
    $name[$id] = $row[0];
    $day[$id] = $row[1];
  }
  $close = mysql_close($dbh)

//with the suggestions from Xisto.com
  $today=date("m-d");
  while(list($narr,$varr) =each ($day))
  {
    if($varr==$today) echo"<p>Happy birthday $name[$narr]!</p>";
  }
?>


Share this post


Link to post
Share on other sites

I would find a birthday script that people could enter there birthdays a god send can some one write a script like this for me  :)

<{POST_SNAPBACK}>


I've written the entry form you could use to collect birthday information, then on whatever page you want, you just query the database the day and month of the date with the dd-mm entered, I will explain this later, unless someone wants an attempt to do it. It's partially tested to make sure it works correctly, but it has not gone under extensive testing, especially in the security way of things, as I wrote it all on one page, just to speed things up, else switching through different pages, modules, functions, features just doesn't cut it.

 

So here's the code.

 

<?php$dbhost 	 = 'localhost';$dbusername = 'dbusername';$dbpasswd  = 'dbpasswd';$database  = 'database';$table    = 'table';$firstname  = 'firstname';$surname 	 = 'surname';$fullname  = 'fullname';$DOB   	 = 'birthday';$dbcon 	 = mysql_connect($dbhost, $dbusername, $dbpasswd) or die('Error: Connecting to Server failed.');$query = "CREATE DATABASE IF NOT EXISTS $database";mysql_query($query) or die('Error: Failed to create database');$db = mysql_select_db($database, $dbcon) or die('Error: Database selection failed.');$query = "CREATE TABLE IF NOT EXISTS $table ( id int(25) NOT NULL auto_increment, $firstname varchar(50) NOT NULL default '', $surname varchar(50) NOT NULL default '', $fullname varchar(100) NOT NULL default '', $DOB varchar(6) NOT NULL default '', PRIMARY KEY(id), UNIQUE KEY($fullname)) TYPE = MyISAM COMMENT = '$database $table'";mysql_query($query) or die ('Error: Failed to create table');?><html><head><title>Birthday Entry</head></head><body>	<h3>Enter Your Birthday</h3>	<form name="EntryDOB" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">	<table>  <tr> 	 <td>First Name:</td> 	 <td><input name="fname" type="text" maxlength="49" value="<?php echo $_POST['fname']; ?>"></td>  </tr>  <tr> 	 <td>Last Name:</td> 	 <td><input name="sname" type="text" maxlength="49" value="<?php echo $_POST['sname']; ?>"></td>  </tr>  <tr> 	 <td>Date of Birth (dd/mm):</td> 	 <td>    <input name="dd" type="text" size="2" maxlength="2" value="<?php echo $_POST['dd']; ?>">    <input name="mm" type="text" size="2" maxlength="2" value="<?php echo $_POST['mm']; ?>"> 	 </td>  </tr>  <tr> 	 <td> </td> 	 <td><input id="submit" name="submit" type="submit" value="Submit"></td>  </tr>	</table>	</form>	<?php	if(isset($_POST['submit']))	{  $fname  = stripslashes(trim($_POST['fname']));  $lname  = stripslashes(trim($_POST['sname']));  $day  = stripslashes(trim($_POST['dd']));   $month  = stripslashes(trim($_POST['mm']));  if((!$fname) || (!$lname) || (!$day) || (!$month))  { 	 echo "<br>\n<br>\n<p>Sorry, all fields are required, please re-enter and resubmit.</p>\n<br>\n\t</body>\n</html>"; 	 exit();  }  $month = (int)$month;  if(($month < 1) || ($month > 12))  { 	 echo "<br>\n<br>\n<p>Sorry, the month you entered is invalid, please re-enter and resubmit.</p>\n<br>\n\t</body>\n</html>"; 	 unset($month); 	 exit();  }  $howmanydays = array(1 => 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30);  $day = (int)$day;  if(($day < 1) || ($day > (int)$howmanydays[$month]))  { 	 echo "<br>\n<br>\n<p>Sorry, the day you entered is invalid, please re-enter and resubmit.</p>\n<br>\n\t</body>\n</html>"; 	 unset($day); 	 exit();  }  $completename = "$fname $lname";  $birthday	= "$day-$month";  $sql_name_check = mysql_query("SELECT $fullname FROM $table WHERE $fullname='$completename'");  $name_check = mysql_num_rows($sql_name_check);  if($name_check > 0)  { 	 echo "<br>\n<br>\n<p>Sorry, your name seems to have been already entered, only one entry needed.</p>\n<br>\n\t</body>\n</html>"; 	 unset($fname, $lname, $completename); 	 exit();  }  $query = "INSERT INTO $table (id, $firstname, $surname, $fullname, $DOB) VALUES ('','$fname', '$lname', '$completename', '$birthday')";  mysql_query($query);  mysql_close($dbcon);  echo "<br>\n<br>\n<p>Thank You for your entry $completename</p>\n";	}	?>	</body></html>

Starts off with the configuration variables, then this script connects to your MySQL server and then creates the database and table if the user has rights to do so. Else you would have to create it yourself. This part of the script is not needed, but for a one off script that can setup your database for you is what I was trying to achieve. Once you've ran the script once, you can remove those extries, just make sure you leave the database connection part of the script in there.

 

Next we go into creating our simple fill in form, what it asks from our users is firstname, lastname, day and month of birthday. When the form is submitted the action of the form uses itself to process the form. We could grab other information if needs be, for security reason like an email address, etc, but this was just to get the basics. The best way to do this would have the user log in, and we stored this information as part of their personal details we have on database.

 

So the form gets submitted and now goes through being processed, we set our posted variables into easier variables to work with, stripping out any slashes and removing any leading spaces, it might be better to use a regular expression on the type of characters that are allowed to be inserted and possibly to use a wordlist for blocking out bad words being used, if we are displaying this birthday to all to see, not just the person when they log in.

 

Next we check if anything was entered in the fields of the form, if not, we will stop processing the rest of the script and ask them to resubmit correct information.

 

We then check that the month falls within 1 or 12, why? Because there's only 12 months in a year, so it would not make sense seeing someone input 14 as a month. We don't want that happening as it will just add to the database and would not result in anything.

 

We also check that the days are valid for the months, I used an array to store the maxdays for the month, we know this so, we can automatically set it. We then test whether the day is valid by seeing that it's not less than one and not greater than the maxday of the month.

 

Note: I had to force the use of integer on the variables, due to PHP allowing all variables any type, and it had problems with knowing what type it should have used. So hopefully this fixed that problem.

 

So after those checks, we can fairly say that the date entered is valid, so we can continue with our script.

 

Next I create the fullname, because we will use it in our database as a check to make sure that the same name doesn't already exist, quite possible that these people are different and we may have to do a check on all information entered, but to make it simple, no fullnames the same can be entered twice, even if other details are different. I then create the birthday variable using the information entered seperating it with a dash (-).

 

Now to check if the name is already in the database, if not, it will add all the valid information in it, close the database and thank the user for their entry. That's it!

 

I know there's a lot of improvements that could be made, it wasn't really well thought out but it does what it's meant to do and that's to grab the birthday information.

 

The next thing would be to use that birthday information and display it on your page, when it's neccessary. I'll leave that up to you to do, as I'm sure it wouldn't be too much of a hassle, connect to database, create a variable for todays date to compare making sure it's set out like dd-mm with the dash seperator, you then compare the string with any in the database, get a match, display your message to them. Or just create a list of people's birthdays on that day, or upcoming birthdays, the ideas are unlimited.

 

Well hope this script shows some strategies in tackling some of the problems you will encounter along the way.

 

 

Cheers, MC

Share this post


Link to post
Share on other sites

Couldn't find any use of the script !!.Could anyone tell me what's the damn use of this script ??This is dead silly. I can surprize a person in better ways than this .

Share this post


Link to post
Share on other sites

I have to tell you guys, think outside the square (computer) for the moment.How about a Birthday/Anniversary Reminder, by utilising this script the variations of being informed that this day is coming up or it's now! by SMS/Pager, Email, Fax etc can be achieved. Wouldn't that be good, incase you keep missing important dates, set it up for your whole family, relations etc and let them be surprised!Other than this, it shows you a few scripting basics and ideas.Programming is used to solve problems, so if this isn't going to solve your problem, it may solve someone elses, and remember, modifications are allowed so you could do what you like with the script.Cheers,MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.