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