Sunteți pe pagina 1din 27

DATA ADDITION

PROGRAM TO GET VALUES FROM A HTML FORM


AND ADD IT TO A DATABASE
<html>
<head>
<title>ADD A CATEGORY</title>
<body bgcolor =”#00654ee”>
<form action=”add.php” method=”POST”>
<tr> <td align=”center”>enter rollno:</td>
<td align=”center”><input type=”text”
name=”rollno”></td>
</tr><br/><br/>
<tr> <td align=”center”>enter name:</td>
<td align=”center”><input type=”text”
name=”name”></td>
</tr><br/><br/>
<tr> <td align=”center”>enter address:</td>
<td align=”center”><input type=”text”
name=”address”></td>
</tr><br/><br/>
<tr> <td align=”center”>enter phone:</td>
<td align=”center”><input type=”text”
name=”phone”></td>
</tr><br/><br/>
<tr> <td align=”center”>enter grade:</td>
<td align=”center”><input type=”text”
name=”grade”></td>
</tr><br/><br/>
<input type=”submit” name=”submit”
value=”insert”/><br/>
</body> </html>
//SHOW
<html>
<body bgcolor=”#0099cc”>
<?php
$con=mysql_connect(“localhost”,”root”,””);
if(!$con)
{
die(“could not connect”.mysql_error());
}
mysql_select_db(“ourdatabase”,$con);
$result=mysql_query(“select * from ourtable”);
echo”<b><center>college
application</center></b>”;
echo”<table border=’1’align=center>
<br><tr> <th>rollno</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>grade</th> </tr>”;
while($row=mysql_fetch_array($result))
{
echo”<tr>”;
echo”<td>”,$row[“rollno”],”</td>”;
echo”<td>”,$row[“name”],”</td>”;
echo”<td>”,$row[“address”],”</td>”;
echo”<td>”,$row[“phone”],”</td>”;
echo”<td>”,$row[“grade”],”</td>”;
echo”</tr>”;
}
echo”</table>”;
mysql_close($con);
?>
</body> </html>
DATA RETRIEVAL
PROGRAM TO READ RECORDS FROM A DATABASE
AND DISPLAY THEM
<html>
<head>
<title>student detail</title>
</head>
<body><center><h3>student details</h3><br>
<?php
$con=mysql_connect(“localhost”,”root”,””);
if(!$con)
{
die(“could not connect”.mysql_error());
}
mysql_select_db(“student details”,$con);
$result=mysql_query(“select * from student”);
echo”<table border=6 align=center>”;
echo”<br>”;
echo”<tr>”;
echo”<th>”,”sno”,”</th>”;
echo”<th>”,”student name”,”</th>”;
echo”<th>”,”regno”,”</th>”;
echo”<th>”,”grade”,”</th>”;
echo”</tr>”;
while($row=mysql_fetch_array($result))
{
echo”<tr>”;
echo”<td>”,$row[“sno”],”</td>”;
echo”<td>”,$row[“sname”],”</td>”;
echo”<td>”,$row[“sregno”],”</td>”;
echo”<td>”,$row[“sgrade”],”</td>”;
echo”</tr>”;
}
echo”</table>”;
mysql_close($con);
?>
</center>
</body>
</html>
ARITHMETIC OPERATION
PROGRAM TO GET TWO NUMBERS AND
PERFORM A SELECTED ARITHMETIC OPERATION
<html>
<head>
<title>ARITHMETIC OPERATION USING
FUNCTION</title>
</head> <body> <center>
<form action =”func.php” method=”POST”>
<hr><h3>ARITHMETIC OPERATIONS USING
FUNCTION</h3></hr>
Enter avalue between 1 to 4<br>
1.ADDITION<br> 2.SUBTRACTION<br>
3.MULTIPLICATION<br> 4.DIVISION<br>
Enter a number:&nbsp;&nbsp;&nbsp
<input type=”text” name=”t1″><br><br>
<input type=”submit”>
</form> </center> </body> </html>
———————————————————————————————
———————————
PHP PROGRAM
<html>
<body><?php
function add($a,$b)
{
$v=$a+$b;
echo(“<br>addition of 2 strings<br>”);
echo(“\$a=$a,<br>\$b=$b,<br>\$v=$v”);
}
function sub($a,$b)
{
$v1=$a-$b;
echo(“<br>difference of 2 strings<br>”);
echo(“\$a=$a,<br>\$b=$b,<br>\$v1=$v1”);
}
function mul($a,$b)
{
$v2=$a-$b;
echo(“<br>product of 2 strings<br>”);
echo(“\$a=$a,<br>\$b=$b,<br>\$v2=$v2”);
}
function div($a,$b)
{
$v3=$a/$b;
echo(“<br>division of 2 strings<br>”);
echo(“\$a=$a,<br>\$b=$b,<br>\$v3=$v3”);
}
$choice=$_POST[“t1”];
switch($choice)
{
case 1:add(8,2);
break;
case 2:sub(8,2);
break;
case 3:mul(8,2);
break;
case 4:div(8,2);
break;
default:echo(“operation unknown”);
} ?> </body> </html>
DATA REMOVAL
PROGRAM TO DELETE A RECORD FROM A
DATABASE
//DELETE.HTML
<html>
<head>
<title>delete</title>
<body bgcolor=”#FFCE9D”>
<form action=”delete.php”method=”POST”>
Enter rollno of the record to be deleted
<input type =”text” name=”rollno”/>
<br/><br/>
<input type=”submit” value=”submit”/>
</body>
</html>
//DELETE.PHP
<html>
<head>
<title>delete</title>
<body bgcolor=”#FFCE9D”>
<form action=”delete.php”method=”POST”>
Enter rollno of the record to be deleted
<input type =”text” name=”rollno”/>
<br/><br/>
<input type=”submit” value=”submit”/>
</body>
</html>
DATA UPDATION
PHP PROGRAM TO UPDATE RECORDS IN A
DATABASE
<html>
<head>
<title>change</title>
<body bgcolor=”#BBE8B0″>
<form action=”change.php” method=”POST”>
Enter rollno in which updation is to be done
<input type=”text” name=”rollno”/>
<br/><br/>
Enter new name<input type=”text”
name=”name”/><br/><br/>
Enter new address<input type=”text”
name=”address”/><br/><br/>
Enter new phone<input type=”text”
name=”phone”/><br/><br/>
Enter new grade<input type=”text”
name=”grade”/><br/><br/>
<input type=”submit” value=”submit”/>
</body>
</html>
//CHANGE.PHP
<html>
<body bgcolor=”#BBE8BO”>
<title>change</title>
<?php
$con=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“ourdatabase”,$con)or
die(“cannot select”);
$rollno=$_POST[‘rollno’];
$name=$_POST[‘name’];
$address=$_POST[‘address’];
$phone=$_POST[‘phone’];
$grade=$_POST[‘grade’];
$res=mysql_query(“UPDATE ourtable SET
name=’$name’,address=’$address’,
phone=’$phone’,grade=’$grade’ where
rollno=’$rollno'”);
if(!$res)
echo”record not changed”;
else
echo”record changed successfully”;
mysql_close($con);
?>
</body>
</html>
FIBONACCI SEQUENCE
PROGRAM TO DISPLAY FIBONACCI SERIES
<html>
<body>
<form action=”fibo.php” method=”POST”>
<center>
<h1>fibonacci series</h1><br>
Enter the number:&nbsp;&nbsp;&nbsp;&nbsp
<input type=”text” name=”t1″><br>
<input type=”submit”><br>
</center> </form> </body> </html>
———————————————————————————————
———————————
PHP PROGRAM
<html>
<body>
<center>
<form>
<?php
$num=$_POST[“t1″];
$a=0;
$b=1;
echo”<br>”;
echo”$a”;
echo”<br>”;
echo”$b”;
echo”<br>”;
function fibfn( )
{
global $a;
global $b;
global $c;
$c=$a+$b;
return $c;
}
for($i=2;$i<=$num;$i++)
{
fibfn( );
echo”<br>”;
echo “$c”;
echo”<br>”;
$a=$b;
$b=$c;
}
?>
</form> </center>
</body> </html>
FUNCTION PARSING
PROGRAM TO PARSE A FUNCTION
<html>
<head>
<title>parser function</title>
</head>
<body><center><hr>parser fuction<br><hr>
<?php
$file=”beginner.xml”;
function contents($parser,$data)
{
echo $data;
}
function startTag($parser,$data)
{
echo “<br>”;
}
function endTag($parser,$data)
{
echo “</b><br/>”;
}
$xml_parser=xml_parser_create( );
xml_set_element_handler($xml_parser,”startTag”,
”endTag”);
xml_set_character_data_handler($xml_parser,”co
ntents”);
$fp=fopen($file,”r”);
$data=fread($fp,80000);
if(!(xml_parse($xml_parser,$data,feof($fp))))
{
die(“error on
line”.xml_get_current_line_number($xml_parser))
;
}
xml_parser_free($xml_parser);
fclose($fp);
?>
</center>
</body>
</html>
<?xml version=”1.0″
?>
<numbers>
<num>1</num>
<num>2</num>
<num>3</num>
<num>4</num>
<num>5</num>
<num>6</num>
<num>7</num>
<num>8</num>
<num>9</num>
<num>10</num>
</numbers>
SEND MAIL
PROGRAM TO SEND MAIL.
<?php
$receiver=”myfriend@ourmail.co.in”;
$subject=”wish”;
$content=”hai”;
$sender=”myself@ourmail.co.in”;
$headers=”from:$sender”;
mail($receiver,$subject,$content,$headers);
echo”mail has been sent successfully”;
?>
SESSION HANDLING & COOKIES
PROGRAM TO PRINT DATE
<?php
print date(“y/m/d”);
print”<br/>”;
print date(“y.m.d”);
print”<br/>”;
print date(“d_m_y”);
?>
PROGRAM TO HANDLE SESSIONS
<?php
SESSION_start();
$_SESSION[‘greeting’]=’hello world’;
echo $_SESSION[‘greeting’];
?>
<hr/>
<a href=”session2.php”> next </a>
code sample:sessions/demos/session2.php
<a href=”session3.php”> next page</a>
code sample:sessions/demos/session3.php
<?php
SESSION_start();
echo $_SESSION[‘greeting’];
unset($_SESSION[‘greeting’]);
?>
<?php
SESSION_start();
echo $_SESSION[‘greeting’];
SESSION_unset();
SESSION_destroy();
?>
<?php
session_start( );
echo”<hr>”;
echo $_SESSION[“car”]=”NANO”;
echo $_SESSION[“bike”]=”YELLOW APACHE”;
echo $_SESSION[“cycle”]=”LADY BIRD”;
echo”<br>”;
echo”values in the session”;
echo”<br>”;
echo $_SESSION[“car”];
echo”<br>”;
echo $_SESSION[“cycle”];
echo”<br>”;
echo”<br>”;
echo”values in the session after unset”;
echo”<br>”;
echo $_SESSION[“car”];
echo”<br>”;
echo $_SESSION[“bike”];
echo”<br>”;
unset($_SESSION[“cycle”]);
?>
PROGRAM TO READ A COOKIE
<html>
<head>
<title>using cookies</title></head>
<body><center><hr>cookies<br></hr><br>
<?php
$_COOKIE[“user”]=”KALA”;
if(isset($_COOKIE[“USER”]))
echo”WELCOME”.$_COOKIE[“user”].”!<br>”;
else
echo”welcome guest!<br>”;
echo”end of cookie”;
?>
<br>
</body>
</html>
STRING HANDLING
PHP PROGRAMS FOR STRING HANDLING
<html>
<body>
<h1><font color=”red”>using string
functions</h1></font>
<?php
echo”The text string is php is easy,<br><br>”;
echo”Php is easy id”,strlen(“php is easy”),
“characters long<br><br>”;
echo”The word easy is at postition “,
strpos(“php is easy”,”easy”),”<br><br>”;
echo”uppercasting the first letter gives you:”,
ucfirst(“php is easy”),”<br><br>”;
echo”in uppercase:”,strtoupper(“php is
easy”),”<br><br>”;
echo”in lowercase:”,strtolower(“PHP IS
EASY”),”<br><br>”;
echo”reversed:”,strrev(“php is
easy”),”<br><br>”;
?>
</body>
</html>
———————————————————————————————
———————————
//3b.ARRAYS
<html>
<head>
<title>using array</title>
</head>
<body><h3>using array</h3>
<?php
echo”<br>array<br>——————-<br>”;
$ice[0]=”orange”;
$ice[1]=”vanilla”;
$ice[2]=”strawberry”;
foreach($ice as $value)
{
echo “$value <br>”;
}
extract($ice);
echo”<br>\$good=$ice[0]<br>”;
echo”<br>\$better=$ice[1]<br>”;
echo”<br>\$best=$ice[2]<br>”;
echo”<br>”;
echo”<br>sorting<br>——————-<br>”;
sort($ice);
foreach($ice as $value)
{
echo “$value <br>”;
}
echo”<br>”;
echo”<br>modify<br>——————–<br>”;
$ice[2]=”chocolate”;
echo”<br>\$good=$ice[0]<br>”;
echo”<br>\$better=$ice[1]<br>”;
echo”<br>\$best=$ice[2]<br>”;
echo”<br>deleting<br>——————-<br>”;
unset($ice[1]);
echo”<br>\$good=$ice[0]<br>”;
echo”<br>\$better=$ice[1]<br>”;
echo”<br>\$best=$ice[2]<br>”;
echo”<br>splitting<br>——————-<br>”;
$split=array_slice($ice,1,2);
foreach($split as $value)
{
echo”$value<br>”;
}
echo”<br>merging<br>——————-<br>”;
$fruit=array(“mango”,”banana”);
$merge=array_merge($ice,$fruit);
foreach($merge as $value)
{
echo”$value<br>”;
}
?>
</body>
</html>
———————————————————————————————
——————————–
TEXT SEARCH
PHP FOR SEARCHING A TEXT USING REGULAR
EXPRESSION
<html>
<head>
<title>word table_php</title>
</head>
<body><center><hr>regular expression <br></hr>
<?php
function splitter($str)
{
$freq=array();
$words=preg_split(“/[ . , ; : ? ]\s*/”,$str);
foreach($words as $word)
{
$keys=array_keys($freq);
if(in_array($word,$keys))
$freq[$word]++;
else
$freq[$word]=1;
}
return $freq;
}
#**end of splitter
$str=”apples are good for you,or dont you like
apples?
or may be you like oranges better than apples”;
$tbl=splitter($str);
print”<br/>word frequency<br/><br/>”;
$sorted_keys=array_keys($tbl);
sort($sorted_keys);
foreach($sorted_keys as $word)
print”$word $tbl[$word]<br/>”;
?>
</center>
</body>
</html>
MESSAGE PASSING
PROGRAM TO PASS A MESSAGE FROM ONE PAGE
TO ANOTHER
<html>
<head>
<title>Message Passing</title>
</head>
<body>
<h3> <center>
<br><hr><br>enter the following
details<br><hr><br>
<form action=”msg.php” method=”POST”>
Enter your name:<input type=”text”
name=”t1″><br><br>
Enter your age:<input type=”text”
name=”t2″><br><br>
Enter your color:<input type=”text”
name=”t3″><br><br>
<input type=”submit”>
</form>
</body> </html>
———————————————————————————————
———————————
PHP PROGRAM
<html>
<head>
<title>message passing</title>
</head>
<body>
<center><h2><br>
<hr><br>personal data<br><hr><hr>
<?php
$name=$_POST[“t1”];
$age=$_POST[“t2”];
$color=$_POST[“t3″];
echo”your name is:$name<br>”;
echo”your age is:$age<br>”;
echo”your color is:$color<br>”;
?>
<br><hr>
<a
href=”http://localhost/rajibca/msg.html”>GO BA
CK</a>
<br><hr>
</h2>
</center>
</body>
</html>

MARKLIST
PHP PROGRAM FOR ACCEPTING AND DISPLAYING MARKS ON A PAGE

//1a.HTML PAGE TO ACCEPT DATA

<html>
<body>
<form action=”pract.php” method=”POST”>
<h3><hr>STUDENT MARKLIST</hr></h3>
rollno:&nbsp;&nbsp;&nbsp
<input type=”text” name=”t1″><br><br>
name:&nbsp;&nbsp;&nbsp
<input type=”text” name=”t2″><br><br>
mark1:&nbsp;&nbsp;&nbsp
<input type=”text” name=”t3″><br><br>
mark2:&nbsp;&nbsp;&nbsp
<input type=”text” name=”t4″><br><br>
<input type=”submit”><br><br>
</form> </body> </html>

——————————————————————————————————————————————
———————–

//1a.PHP PROGRAM THAT DISPLAYS THE MARKS PAGE


<html>
<body><h5><center>STUDENT MARKLIST</center></h5>
<form>
<?php
$rollno=$_POST[“t1”];
$name=$_POST[“t2”];
$mark1=$_POST[“t3”];
$mark2=$_POST[“t4″];
$total=$mark1+$mark2;
$avg=$total/2;
$grade=” “;
if($avg>=70)
{
$grade=”o”;
}
elseif($avg<70&&$avg>=60)
{
$grade=”a”;
}
elseif($avg<60&&$avg>=50)
{
$grade=”b”;
}
else
{
$grade=”fail”;
}
?>
<center>
<table border=”2″>
<tr><align = “center”>
<th>rollno</th>
<th>name</th>
<th>mark1</th>
<th>mark2</th>
<th>total</th>
<th>avg</th>
<th>grade</th>
</tr>
<td><?php echo $rollno; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $mark1; ?></td>
<td><?php echo $mark2; ?></td>
<td><?php echo $total; ?></td>
<td><?php echo $avg; ?></td>
<td><?php echo $grade; ?></td>
</table>
</center>
</form>
</body>
</html>

PHP Student Marksheet Example


<center><h1>
<?php

$name=”shree”;
$regno=1;

$s1=40;
$s2=20;
$s3=30;

$t=$s1+$s2+$s3;
$p=$t/3;

if($p>=60)
{
echo ” first division “;
}
if($p>=50 and $p<60)
{
echo ” Second division “;
}
if($p>=40 and $p<50)
{
echo ” Third division “;

if($p<40)
{
echo ” fail”;

echo “<br>”;
if($s1>=35 and $s2>=35 and $s3>=35)
{
$r=”pass”;
}
else
{
$r=”fail”;
}

echo $r;
echo $t;
echo $p;

?>