Thursday 22 March 2012

The PHP Date() Function


The PHP date() function formats a timestamp to a more readable date and time.
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.
Syntax
date(format,timestamp)

Parameter
Description
format
Required. Specifies the format of the timestamp
timestamp
Optional. Specifies a timestamp. Default is the current date and time

PHP Date() - Format the Date
The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:
  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
A list of all the characters that can be used in the format parameter, can be found in our PHP Date reference.
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d");
?>
The output of the code above could be something like this:
2009/05/11
2009.05.11
2009-05-11

PHP Date() - Adding a Timestamp
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
The mktime() function returns the Unix timestamp for a date.
The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)
To go one day in the future we simply add one to the day argument of mktime():
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>
The output of the code above could be something like this:
Tomorrow is 2009/05/12

The $_POST Variable

The predefined $_POST variable is used to collect values from a form sent with method="post".
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
Note: However, there is an 8 Mb max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
Example
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL will look like this:
http://www.w3schools.com/welcome.php
The "welcome.php" file can now use the $_POST variable to collect form data (the names of the form fields will automatically be the keys in the $_POST array):
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

When to use method="post"?
Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.
However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
The PHP $_REQUEST Variable
The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE.
The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
Example
Welcome <?php echo $_REQUEST["fname"]; ?>!<br />
You are <?php echo $_REQUEST["age"]; ?> years old.

The $_GET Variable


The predefined $_GET variable is used to collect values in a form with method="get"
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
Example
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
When the user clicks the "Submit" button, the URL sent to the server could look something like this:
http://www.w3schools.com/welcome.php?fname=Peter&age=37
The "welcome.php" file can now use the $_GET variable to collect form data (the names of the form fields will automatically be the keys in the $_GET array):
Welcome <?php echo $_GET["fname"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

When to use method="get"?
When using method="get" in HTML forms, all variable names and values are displayed in the URL.
Note: This method should not be used when sending passwords or other sensitive information!
However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.
Note: The get method is not suitable for very large variable values. It should not be used with values exceeding 2000 characters.

PHP Form

First Create "Welcome.php" file as follows
==================
<html>
<head>
<title>Welcome</title>
</head>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</form>
</body>
</html>

Create "Form.php" as follows
======================
html>
<head>
<title>Form</title>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" /><BR/>
Age: <input type="text" name="age" /><BR/>
<input type="submit" />
</form>
</body>
</html>


Output
=====
Welcome SATEESH.BAGADHI!
You are 28 years old.

Tuesday 20 March 2012

PHP Guest Book project code

Create a table guestbook in MYSQL
=========================
mysql> create databse guestbook;               
mysql> use guestbook;

CREATE TABLE IF NOT EXISTS `guestbook` (
  `name` varchar(40) DEFAULT NULL,
  `location` varchar(40) DEFAULT NULL,
  `email` varchar(40) DEFAULT NULL,
  `url` varchar(80) DEFAULT NULL,
  `comments` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
 
 INSERT COMMAND
================= 
INSERT INTO `guestbook` (`name`, `location`, `email`, `url`, `comments`) VALUES
('sateesh', 'seet', 'bagadhi.sateesh@gmail.com', 'http://bagadhicomputersciencetutorials.blogspot.com', 'hi');


====================
Create the following php files
1)dbconnect.php
2) create_entry.php
3)sign.php
4)view.php

code for the above files
------------------------------
 
1)dbconnect.php
 
<html>
<body>
<?php
mysql_connect("localhost", "sateesh","bagadhi") or
 die ("Could not connect to database");
mysql_select_db("guestbook") or
 die ("Could not select database");
?>
</body>
</html>




 
2)create_entry.php
 
<html>
<body>
<?php 
include("dbconnect.php");
$submit = "Sign!";
$name=$_POST['name'];
$location=$_POST['location'];
$email=$_POST['email'];
$url=$_POST['URL'];
$comments=$_POST['comments'];
if ($submit == "Sign!")
{
$query = "insert into guestbook
(name,location,email,url,comments) values
('$name','$location','$email','$url','$comments')";
mysql_query($query) or die (mysql_error());
?>
<h2>Thanks!</h2>
<h2> <a href="view.php">View My Guest Book!</a></h2>
<?php
}
else
{
include("sign.php");
}
?>
</body>
</html> 
 
3)sign.php
 
<html>
<body>
<h2>Sign my Guest Book!</h2>
<form method=post action="create_entry.php">
<b>Name  :<t></t></b>
<input type=text size=40 name=name>
<br>
<b>Location :</b>
<input type=text size=40 name=location>
<br>
<b>Email :<t></t></b>
<input type=text size=40 name=email>
<br>
<B>Home page URL:</B>
<INPUT TYPE=TEXT SIZE=40 NAME=URL>
<BR>
<b>comments :<t></t></b>
<textarea name=comments cols=40 rows=10 wrap=virtual></textarea>
<br>
<input type=submit name=submit value="Sign!">
<input type=reset name=reset value="Start Over">
</form>
</body>
</html> 
4)view.php
 
<html>
<body>
<?php include("dbconnect.php"); ?>
<h2>View My Guest Book!</h2>
<?php
$result = mysql_query ("select * from guestbook") or
 die (mysql_error());
while ($row = mysql_fetch_array($result))
{
echo "<b>Name:</b>";
echo $row["name"];
echo "<br>\n";
echo "<b>Location:</b>";
echo $row["location"];
echo "<br>\n";
echo "<b>Email:</b>";
echo $row["email"];
echo "<br>\n"; 
echo "<b>URL:</b>";
echo $row["url"];
echo "<br>\n"; 
echo "<b>Comments:</b>";
echo $row["comments"];
echo "<br>\n";
echo "<br>\n";
echo "<br>\n";
}
mysql_free_result($result);
?>
<h2> <a href="sign.php">Sign My Guest Book!</a></h2>
<body>
</html>

 Save all files and run in localhost
sign.php 





INHERITANCE

<html>
<head>
<title>Example:1</title>
</head>
<body>
<?php
class first_class
{
var $name = "harry";
function first_class($n)
{
$this->name =$n;
}
function sayHello()
{
print "Hello my name is $this->name<br>";
}
}
class second_class extends first_class
{
}
$test = new second_class("son of harry");
$test->sayHello();
 ?>
</body>
</html>

OUTPUT
Hello my name is son of harry

CONSTRUCTOR

<html>
<head>
<title>Example:1</title>
</head>
<body>
<?php
class first_class
{
var $name;
function first_class($n ="anon")
{
$this->name = $n;
}
function sayHello()
{
print " Hello my name is $this->name <BR>";
}
}
$obj1 = new first_class("sateesh");
$obj2 = new first_class("bagadhi");
$obj1->sayHello();
$obj2->sayHello();
 ?>
</body>
</html>

OUTPUT
Hello my name is sateesh
Hello my name is bagadhi 

CHANGING THE VALUE OF A METHOD

<html>
<head>
<title>Example:2</title>
</head>
<body>
<?php
class first_class
{
var $name="Harry";
function setName($n)
{
$this ->name =$n;
}
function sayHello()
{
print"Hello my name is $this->name<BR>";
}
}
$obj1 = new first_class();
$obj1->setName("SATEESH.BAGADHI");
$obj1 -> sayHello();
?>
</body>
</html>
OUTPUT
Hello my name is SATEESH.BAGADHI

CLASS WITH METHOD

<html>
<head>
<title>Class with Method</title>
</head>
<body>
<?php
class first_class
{
var $name;
function hello()
{
print "Hello";
}
}
$obj1 = new first_class();
$obj1->hello();
?>
</body>
</html>

OUTPUT
Hello

OBJECT

<html>
<head>
<title>Example:2</title>
</head>
<body>
<?php
class first_class
{
var $name="sateesh";
var $class ="M.Sc Computer Sci.";
}
$obj1 = new first_class();
$obj2 = new first_class();
$obj1->name ="bagadhi";
print "$obj1->name<BR>";
echo "$obj2->name<BR>";
echo "$obj2->class<BR>";
?>
</body>
</html>

OUTPUT
bagadhi
sateesh
M.Sc Computer Sci.

FUNCTION WITH ARGUMENTS

<html>
<body>
<?php
 function fontWrap($txt, $size )
 {
 print "<font size=\"$size\" face=\"Helvetica,Arial,Sans-Serif\">$txt</font>";
 }
fontWrap("A heading <br>",5);
fontWrap("some body text <br>",3);
fontWrap("some more body text <BR>",3);
fontWrap("YET MORE BODY TEXT <BR>",4);
 ?>
</body>
</html>

OUTPUT
A heading some body text some more body text YET MORE BODY TEXT 

FUNCTION WITH ARGUMENTS

<html>
 <head>
 <title>Listing 6.4</title>
 </head>
 <body>
 <?php
 function addNums( $firstnum, $secondnum)
 {
 $result = $firstnum + $secondnum ;
 return $result;
 }
 print addNums(3,5);
 // will print "8"
 ?>
 </body>
 </html>
OUTPUT
8

FUNCTION

<html>
 <head>
 <title>Listing 6.3</title>
 </head>
 <body>
 <?php
 function printBR( $txt )
 {
 print ("$txt<br>\n");
 }
 printBR("This is a line");
 printBR("This is a new line");
 printBR("This is yet another line");
 ?>
 </body>
 </html>

OUTPUT
This is a line
This is a new line
This is yet another line

FUNCTION

<html>
<body>
<?php
 function bighello()
 {
 print "<h1>HELLO!</h1>";
 }
 bighello();
 ?>
</body>
</html>
 
OUTPUT

HELLO!

 
 

STUDENT MARKS

<html>
<head>
<title>Example:</title>
</head>
<body>
<?php echo"Student details "; ?><br</br>
<?php
$sno = 21;
$sname ='sateesh.bagadhi';
$class ='M.Sc Computer Science';
$mark =67;
echo "Given student details are: "; ?> <br></br>
<?php
echo"student name:";echo $sname; ?> <br></br>
<?php
echo"student number is:";echo $sno; ?> <br></br>
<?php
echo"class is:";echo $class; ?> <br></br>
<?php
echo"result is:"; ?> <br></br>
<?php
if( $mark > 34 && $mark < 99 )
{
if ($mark>59 && $mark<=100)
echo"first class";
else
echo"second class";
}
else
echo"fail";
?>
<br</br>
</body>
</html>

OUTPUT
Student details

Given student details are:
student name:sateesh.bagadhi
student number is:21
class is:M.Sc Computer Science
result is:
first class

MULTI DIMESIONAL ARRAY

<html>
<head>
<title>****  multidimensional array  ****</title>
</head>
<body>

<?php
$families = array
(
  "Griffin"=>array
  (
  "Peter",
  "Lois",
  "Megan"
  ),
  "Quagmire"=>array
  (
  "Glenn"
  ),
  "Brown"=>array
  (
  "Cleveland",
  "Loretta",
  "Junior"
  )
);

echo "Is " . $families['Griffin'][2] . 
" a part of the Griffin family?";
?>
 
OUTPUT
Is Megan a part of the Griffin family?

 





ASSOCIATIVE ARRAY

<html>
<head>
<title>****   associative array  ****</title>
</head>
<body>
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>
</body>
</html>
 
OUTPUT
Peter is 32 years old.

 

ARRAY

<html>
<head>
<title>****   array  ****</title>
</head>
<body>
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] . 
" are ". $names[0] . "'s neighbors";
?>
</body>
</html>

OUTPUT
Quagmire and Joe are Peter's neighbors

electric bill

<html>
<head>
<title>****   Electric Bill  ****</title>
</head>
<body>
<?php echo"@@@@@@@@@@@@ Electric Bill @@@@@@@@@@@@ "; ?><br</br>
<?php
$name ='sateesh.bagadhi';
$units = 21;
$rate = 5;
$amount = $rate * $units;
echo "Given BILL details are: "; ?> <br></br>
<?php
echo" NAME OF THE CUSTOMER IS:";echo $name; ?> <br></br>
<?php
echo"UNITS ARE:";echo $units; ?> <br></br>
<?php
echo"BILL AMOUNT IS:";echo $amount; ?> <br></br>
<?php
?>
<br</br>
 
output
@@@@@@@@@@@@ Electric Bill @@@@@@@@@@@@ 
Given BILL details are:  
 NAME OF THE CUSTOMER IS:sateesh.bagadhi 
UNITS ARE:21 
BILL AMOUNT IS:105  




while

<html>
<body>
<?php 
$i=1;
while($i<=5)
  {
  echo "The number is " . $i . "<br />";
  $i++;
  }
?>
</body>
</html>

output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

do while

<html>
<body>
<?php 
$i=0;
do
  {
  $i++;
  echo "The number is " . $i . "<br />";
  }
while ($i<5);
?>
</body>
</html>

output
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5 


for each

<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
  echo "Value: " . $value . "<br />";
}
?>
</body>
</html>

output
Value: one
Value: two
Value: three

for loop

<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
  echo "Hello World!<br />";
}
?>
</body>
</html>

output
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

if timings

<html>
<title>if date function </title>
<body>
<?php
echo "hi, mom. ";
$var = date("H");
if ($var <=11)
{
    echo "GOOD MORNING!";
}
elseif ($var >11 and $var<18)
{
    echo "GOOD AFTERNOON!";
}
else
{
    echo "GOOD EVENING!";
}
?>
</body>
</html>

output
hi, mom. GOOD AFTERNOON!

switch case

<html>
<body>
<?php
$x=3;

switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>
</body>
</html>

output
Number 3

ternary operator ?:

<html>
 <head>
 <title>Listing 5.5</title>
 </head>
 <body>
 <?php
 $mood = "sad";
 $text = ( $mood=="happy" )?"Hooray, I'm in a good mood":"Not happy but
$mood";
 print "$text";
 ?>
 </body>
 </html>
output
Not happy but sad

if .. else

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d== "Sat")
  echo "Have a nice Saturday!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>
</body>
</html>
output:
Have a nice day!

if

<html>
<title>if date function </title>
<body>
<?php
$d=date("D");
if ($d=="Sat"||$d=="Sun")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>
</body>
</html>

output
Have a nice day!


Php arithmetic operators

<html>
<head>
<title>Example:2</title>
</head>
<body>
<?php echo"php arithmetic "; ?><br</br>
<?php echo"addition 4+5=", 4+5; ?><br</br>
<?php echo "Substraction of 5-4=", 5-4; ?><br</br>
<?php echo "Division of 8/4=", 8/4; ?><br</br>
<?php echo "Multiplication of 4*2=", 4*2; ?><br</br>
</body>
</html>

Output:
php arithmetic
addition 4+5=9
Substraction of 5-4=1
Division of 8/4=2
Multiplication of 4*2=8

welcome to php code

<html>
<head>
<title>Example:1</title>
</head>
<body>
<?php echo"Hi, I'm PHP Script"; ?>
</body>
</html>

output:
Hi, I'm PHP Script