SameerShukla PHP Assignments PDF
SameerShukla PHP Assignments PDF
Assignment 1
Set A1
Q. Write a PHP script for the following: Design a form to accept a string.
Write a function to count the total number of vowels (a,e,I,o,u) from
the string. Show the occurrences of each vowel from the string. Check
whether the given string is a palindrome or not, without using built-in
function. (Use radio buttons and the concept of function. Use ‘include’
constructor require stmt.) Program:
Php program=>
<?php
$string = $_GET[‘inputStr’];
$vowels = array(“a”=>0,”e”=>0,”I”=>0,”o”=>0,”u”=>0);
$str=strrev($string);
$a=strlen($string);
$f=0;
For($j=0;$j<$a;$j++)
{
If($str[$j]==$string[$j])
{
$f=0;
}
Else
{
$f=1;
Break;
}
}
If($f==0)
{
Echo”string is palindrome”;
}
Else
{
Echo”string is not palindrome”;
}
?>
HTML file=>
<html>
<body>
<form method=”GET” action=”ass1a1.php”>
Enter the String : <input type=”text” name=”inputStr”><br>
<input type=”submit” name=”Submit”>
</form>
</body>
</html>
Output:
Set A2
Q. Write a PHP script for the following: Design a form to accept
two strings from the user. Find the first occurrence and the last
occurrence of the small string in the large string. Also, count the
total number of occurrences of small string in the large string.
Provide a text box to accept a string, which will replace the
small string in the large string. (Use built-in functions)
Program:
HTML file=>
<html>
<head><title>php program</title></head>
<body bgcolor=’#f1284e’ >
<form action=’ass1a2.php’ method=’post’>
<pre>
Enter1’ststring :<input type=’text’name=’str1’><br>
</html>
Php program=>
<?php
$str1=$_POST[‘str1’];
$str2=$_POST[‘str2’];
$str=$_POST[‘str’];
$ch=$_POST[‘ch’];
$oc=0;
Switch($ch)
{
Case 1:echo”<br>First occ is at position :”;echo
strpos($str2,$str1);
Echo”<br>Last occ is at possition :”;echo
strrpos($str2,$str1);
Break;
Case 2:$oc=substr_count($str2,$str1);
If($oc==0)
Echo”<br>string ‘$str1’ not present in string
‘$str2’.<br>”;
Else
Echo”<br>sub string ‘$str1’ present $oc times
in string ‘$str2’.<br>”;
Break;
Case 3:
$str3=str_replace($str1,$str,$str2);
Echo”<br>After relacing string::’$str3’.”;
Break;
}
}
?>
<h1><a href=’a1a2.html’>go back</a></h1>
Set B1
Q. Write a PHP script for the following: Design a form to accept
two numbers from the user. Give options to choose the arithmetic
operation (use radio buttons). Display the result on the next form.
(Use the concept of function and default parameters. Use ‘include’
construct or require stmt)
Program:
HTML FILE=>
<html>
<body bgcolor=pink >
<form action=”ass1b1.php” method= “GET” >
Enter first number : <input type=text name=a ><br>
Enter second number: <input type=text name=b ><br>
Operation::
<input type=radio name=c value=1>Addition.<br>
<input type=radio name=c value=2>Subtraction.<br>
<input type=radio name=c value=3>Multiplication.<br>
<input type=radio name=c value=4>Division.<br>
<inputtype=submitvalue=ok> <input type=reset
value=clear> </form>
</body>
</html>
Php program:
<?php
$n1 = $_GET[‘a’]; $n2
= $_GET[‘b’];
$ch = $_GET[‘c’];
If($ch==1)
{
$c = $n1 + $n2;
Echo”addition is: $c”;
}
Else if($ch==2)
{
$c = $n1 - $n2;
Echo”subtraction is: $c”;
}
Else if($ch==3)
{
$c = $n1 * $n2;
Echo”multiplication is: $c”;
}
Else if($ch==4)
{
$c = $n1 / $n2;
Echo”Division is: $c”;
}
?>
Set B2
Q. Write a PHP script for the following: Design a form to accept
two strings from the user. Find whether the small string
appears at the start of the large string. Provide a text box to
accept the string that will replace all occurrences of small string
present in the large string. Also, split the large string into
separate words. (Use regular expressions)Program:
HTML file=>
<html>
<body>
<form action=’ass1b2’ method=post>
<pre>
Enter first string :<input type=’text’ name=’str1’><br>
Enter second string :<input type=’text’ name=’str2’><br>
Enter string for replace:<input type=’text’ name=’str3’><br></h2>
</html>
Php program=>
<?php
$str1=$_POST[‘str1’];
$str2=$_POST[‘str2’];
$str3=$_POST[‘str3’];
$ch=$_POST[‘ch’];
Echo”First string=$str1.<br>”;
Echo”Second String=$str2.<br>”;
Echo”String for replace=$str3.<br>”;
If(strlen($str1)>strlen($str2))
{
Switch($ch)
{
Case 1: $pos=strpos($str1,$str2);
If($pos!=0)
Echo”String ‘$str2’ Not present at the start of
‘$str1’.<br>”;
Else
Echo”String ‘$str2’ Present at the strat of
‘$str1’.<br>”;
Break;
Case 2:
$str4=str_replace($str2,$str3,$str1);
Printf(“\nAfter replacing string::”);
Echo $str4;
Break;
Case 3: $s=preg_split(“//”,$str1);
Foreach($s as $v) echo “\t$v <br>”;
Break;
}
}
Else
{
Switch($ch)
{
Case 1:$pos=strpos($str2,$str1);
If($pos!=0)
Echo”String ‘$str1’ Not present at the start of
‘$str2’.<br>”;
Else
Echo”String ‘$str1’ Present at the start of
‘$str2’.<br>”;
Break;
Case 2: $str4=str_replace($str1,$str3,$str2);
Echo “After replacingstring::$str4<br>”;
Break;
Case 3:
Echo”After splitting the string::<br>”;
$s=preg_split(“//”,$str2);
Foreach($s as $v) echo “\t$v <br>”;
}
}
?>
Set C1
Q1. Write a PHP script for the following: Design a form to accept
the details Of 5 Different items, such as item code, item name,
units sold, rate. Display The bill in The tabular format. Use only 4
text boxes. (Hint : Use of explode Function.) Program:
Php function:
<?php
$icode1=$_POST[“t1”];
$iname1=$_POST[“t2”];
$iunits1=$_POST[“t3”];
$irate1=$_POST[“t4”];
$icode=explode(“ “,$icode1);
$iname=explode(“ “,$iname1);
$iunits=explode(“ “,$iunits1);
$irate=explode(“ “,$irate1);
Echo”<center><Table border=2>
<tr>
<th>Item Code</th>
<th>Item Name</th>
<th>Units Sold</th>
<th>Item Rate</th>
<th>Total</th>
</tr>”;
For($i=0;$i<5;$i++)
{
{
Echo”<tr>
<td>$icode[$i]</td>
<td>$iname[$i]</td>
<td>$iunits[$i]</td>
<td>$irate[$i]</td>”;
$total=(int)$irate[$i]*(int)$iunits[$i];
Echo”<td>$total</td>
</tr>”;
}
Echo”</table></center>”;
?>
HTML FILE:
<html>
<head>
<title>Details of Items </title></head>
<body>
<center>
<form input method=”POST”
Action=”ass1setc1.php”> Item Code:-<input
Type=”text” name=”t1”><br><br> Item Name:-
<input type=”text” name=”t2”><br><br> Units Sold:-<input
type=”text” name=”t3”><br><br>
SetC2
Q.Write a PHP script for the following: Design a form toaccept
two strings. Compare the two strings using both methods (= =
operator & strcmp function). Append second string to thefirst
string. Accept the position from the user; from where the
characters from the first string are reversed. (Use radio buttons)
Program:
HTML file=>
<html>
<head><title> .html</title></head>
<body bgcolor=”violet” >
<form action=”ass1c2.php” method=”post”>
<pre>
<inputtype=”submit”value=”check”> <input
type=”reset”value=”cancel”>
</pre>
</form>
</body>
</html>
Php program=>
<?php
$str1=$_POST[‘str1’];
$str2=$_POST[‘str2’];
$pos=$_POST[‘pos’];
$ch=$_POST[‘ch’];
Echo”First string :: $str1.<br><br>”;
Echo”choice is::$ch.<br><br>”;
Switch($ch)
{
Case 1:
If($str1==$str2)
Echo”Both string are equal.<br>”;
Else
?>
Assignment 2
Set A1
Q: 1) Write a menu driven program to perform the following
operations on an associative array:
HTML file=>
<html>
<form action=’ass2a1.php’ method=’post’>
<pre> <h3>OPERATIONS</h3>
1<input type=’radio’ name=’a’ value=’1’>Display.<br>
2<input type=’radio’ name=’a’ value=’2’>size.<br>
3<input type=’radio’ name=’a’ value=’3’>delete.<br>
4<input type=’radio’ name=’a’ value=’4’>reverse.<br>
5<input type=’radio’ name=’a’ value=’5’>traverse.<br>
<inputtype=’submit’value=’ok’> <inputtype=’reset’
value=’cancel’><br>
</pre>
</form>
</body>
</html>
Php program=>
<?php
$array=array(‘zero’=>0,’one’=>1,’two’=>2,’three’=>3,’four’=>4,’
five’=>5);
$ch=$_POST[‘a’];
Switch($ch)
{
Case 1:foreach($array as $key=>$value)
{
Echo”key:$key val:$value.<br>”;
}break;
Case 2:echo sizeof($array);
Break;
Case 3:
$len=sizeof($array);
Array_splice($array,$len);
Print_r( $array);
Break;
Case 4:
Print_r(array_flip($array));
Break;
Case 5:
Shuffle($array);
Print_r($array);
Break;
?>
Set A2
Q. Accept a string from the user and check whether it is a
palindrome or not (Implement stack operations using array built-in
functions).
Program:
HTML File=>
<html>
<h3 > PALLINDROM </h3>
<form action=’ass1a2.php’ method=’post’>
<inputtype=’submit’name=’OK’> <inputtype=’reset’
name=’CLEAR’>
</form>
</body>
</html>
Php program=>
<?php
$s=$_POST[‘str’];
$stack=array();
For($i=0;$i<strlen($s);$i++)
Array_push($stack,$s[$i]);
For($i=0;$i<strlen($s);$i++)
If(array_pop($stack)!=$s[$i])
Break;
If($i==strlen($s))
Echo “$s is pallindrome:”;
Else
Echo “$s is not pallindrom:”;
?>
Set B1
Q. Declare a Multidimensional Array. Display specific elementfrom
a Multidimensional array. Also delete given element from the
Multidimensional array.(After each operation display array
content [Hint : use print_r() ])
Program:
<html>
<body bgcolor=”gold”>
<?php
$stud=array(‘0’=>array(‘name’=> ‘deepak’ ,’age’=>20,’addr’=>’girim’),
‘1’=>array(‘name’=> ‘shankar’, ‘age’=>21,’addr’=>’khor’),
‘2’=>array(‘name’=> ‘ganesh’ , ‘age’=>19,’addr’=>’shirur’),
‘3’=>array(‘name’=> ‘radhu’ , ‘age’=>21,’addr’=>’khor’));
Print_r($stud);
Echo “<br>”;
Echo “<br> Element for delete is:”;
Echo “<br>stud[1][‘age’]<br>”;
Echo $stud[1][‘age’];
Echo “<br><br>”;
Unset($stud[1][‘age’]);
Print_r($stud);
?>
</body>
</html>
Set B2
Q. Define an array. Find the elements from the array that matches
the given value using appropriate search function.
Program:
HTML file=>
<html>
<head>
<meta http-equiv=”pragma” content=”no-cache”>
</head>
<body bgcolor=”gold”>
<form action=”ass2b2.php” method=”post”>
Php program=>
<?php
$no=$_POST[‘no’];
If(empty($no))
{
Echo “Enter all data::”;
Echo “<br><a href=’a2b2.html’>Back</a>”;
}
Else
{
$arr=array(1,2,3,4,5,6,7,7,8,9,10);
Print_r($arr);
Echo “<br><br><br>”; If(in_array($no,
$arr))
Echo “$no occurred in array”;
Else
Program:
HTML file=>
<html>
<body bgcolor=skyblue>
<form action=”ass2C1.php” method=”post”> Enter
choice :
</html>
Php program=>
<html>
<body bgcolor=”gold”> <?
$choice=$_POST[‘ch’];
{
$arr=array(1,2,3,4,5,6,7,8,9,10);
Switch($choice)
{
Case 1:
Array_push($arr,10);
Print_r($arr);
Break;
Case 2:
$ele=array_pop($arr);
Echo “Poped element : $ele”;
Break;
Case 3:
Print_r($arr);
Break;
Case 4:
Array_unshift($arr,”10”);
Print_r($arr);
Break;
Case5:
$ele=array_shift($arr);
Echo “Deleted element : $ele”;
Break;
Case6:
Print_r($arr);
Break;
}
}
Set C2
Q. Write a menu driven program to perform the following operations
on associative arrays:
Program:
HTML file=>
<html>
<body bgcolor=”skyred”>
<form action=”a2c2.php” method=”post”>
<h2>Enter choice :</h2>
<input type=”radio” name=”ch” value=1> Sort array by values in
ascending,descending order<br>
<input type=”radio” name=”ch” value=2> Sort array by values
without changing key values <br>
<input type=”radio” name=”ch” value=3> Filter odd elements from
array <br>
<input type=”radio” name=”ch” value=4> Sort different array at
glance using single function<br>
<input type=”radio” name=”ch” value=5> Merge given two arrays
<br>
<input type=”radio” name=”ch” value=6> Find intersection of two
array <br>
<input type=”radio” name=”ch” value=7> Find union of two array
<br>
<input type=”radio” name=”ch” value=8> Find set difference of two
array <br>
<br>
</body>
</html>
Php program=>
<html>
<body bgcolor=”gold”>
<?php
Function is_odd($var)
{
If($var%2==1)
Return $var;
}
$choice=$_POST[‘ch’];
$arr=array(‘a’=>1,’b’=>2,’c’=>3,’d’=>4,’e’=>5,’f’=>6,’g’=>7,’h’=>
8);
$arr1=array(‘l’=>11,’m’=>22,’n’=>33,’o’=>44,’p’=>55,’q’=>66,’r’
=>77,’s’=>88);
$arr2=array(‘a’=>10,’b’=>20,’c’=>30,’d’=>40,’e’=>50,’f’=>60,’g’=
>70,’h’=>80);
Switch($choice)
{
Case 1:
Sort($arr);
Echo “Array in ascending order:<br>”;
Print_r($arr);
Rsort($arr);
Echo “<br>Array in descending order:<br>”;
Print_r($arr);
Break;
Case 2:
Asort($arr);
Echo “Array in ascending order:<br>”;
Print_r($arr);
Arsort($arr);
Echo “<br>Array in descending order:<br>”;
Print_r($arr);
Break;
Case3:
Print_r(array_filter($arr,’is_odd’));
Break;
Case4:
Array_multisort($arr,0,$arr1,1,$arr2,0);
Print_r($arr);
Echo “<br>”;
Print_r($arr1);
Echo “<br>”;
Print_r($arr2);
Echo “<br>”;
Break;
Case 5:
Print_r(array_merge($arr,$arr1));
Break;
Case 6:
Print_r(array_intersect($arr,$arr1));
Break;
Case7:
$union=array_merge($arr,$arr1);
Print_r(array_unique($union));
Break;
Case8:
Print_r(array_diff($arr,$arr1));
Break;
}
Set A1
Q. Write a program to read two file names from user and append
contents of first file into second file.
Program:
HTML file=>
<html>
<body>
<form action=”a3a1.php” method=”POST”>
Enter First File:<input type=”text” name=”first”><br><br>
Enter Second File:<input type=”text” name=”second”><br><br>
<input type=”submit”>
</form>
</body>
</html>
Php program=>
<?php
$first=$_POST[‘first’];
$second=$_POST[‘second’]; If(!
file_exists($first))
{
Die(“<br>$first does not exist”);
}
If(!file_exists($second))
{
Die(“<br>$second does not exist”);
}
$fp1=fopen($first,’r’) or die(“unable to open first file”);
$fp2=fopen($second,’a’) or die(“unable to open second file”);
$data=fread($fp1,filesize($first));
Fwrite($fp2,$data);
Fclose($fp1);
Fclose($fp2);
Echo “<h2>Contents of first file is appanded to second file</h2>”;
?>
Set A2
Q. Write program to read directory name from user and display
content of the directory.
Program:
HTML file=>
<html>
<form action=”a3a2.php” method=”POST”>
<h1>Display directory contents</h1>
<br>
<h3>Enter directory name : <input type=”text” name=’d’></h3>
<pre>
<input type=”submit” name=”submit” value=”submit”>
</pre>
</form>
</body>
</html>
Php program=>
<?php
$d=$_POST[‘d’];
$h=dir(“.”);
If(is_dir($d))
{
$h=opendir($d);
Echo “<h3><B>Directory Contents”;
Echo “<br/>”;
While(($file=readdir($h))!==false)
{
Echo “<h3>$file<br/>”;
}
Closedir($h);
}
Else
{
Echo “<center><h2><center><u>Invalid Directory or
Directory does not exist!!!</u></center></h2></center>”;}
?>
Set B1
Q. Write a program to read a flat file “student.dat”, calculate the
percentage and display the data from file in tabular format.
(Student.dat file contains rollno, name, Syspro, TCS, CN, PHP,
JAVA, BA )
Program:
HTML File=>
<html><head><title>Exercise 3 Set B 1</title></head>
<body bgcolor=”green”>
<form action=”ass3b1.php” method=”POST”>
<h1>Reading flat file and display data in tabular form.</h1>
<br>
<br>
<hr>
<h3>Ente
r
directory
name(con
taining
file
student.d
at):<input
type=”tex
t”
name=’d’
></h3>
<pre>
</pre>
</form>
</body></html>
Php program=>
<?php
{
If (file_exists(‘student.dat’))
$f=fopen(‘student.dat’,’r’);
Echo “<br/><br/><br/>”;
$d= fgets($f);
$s=explode(‘ ‘,$d);
$m1=$s[2];
$m2=$s[3];
$m3=$s[4];
$total=$m1+$m2+$m3;
$p=($total/300)*100;
Echo “<tr><td align=center>$s[0]</td>”;
Echo “<td align=center>$s[1]</td>”;
Echo “<td align=center>$m1</td>”;
Echo “<td align=center>$m2</td>”;
Echo “<td align=center>$m3</td>”;
Echo “<td align=center>$total</td>”;
Echo “<td align=center>$p %</td></tr>”;
}
}
Echo “</table>”;
}
?>
Set B2
Program:
HTML file=>
<html>
<head><title>Exercise 3 Set A 1</title></head>
<body bgcolor=”grey”>
<form action=”a3b2.php” method=”POST”>
<center><h1><b><font type=”San Sarif”>Search file of given
extension.</font></b></h1></center>
<br>
<br>
<hr>
<h3>Ente
r
directory
name:
<input
type=”tex
t”
name=”d”
></h3>
<pre>
<input type=”submit” name=”submit” value=”submit”>
<input type=”reset” name=”reset’” value=”reset”>
</pre>
</form>
</body>
</html>
Php program=>
<?php
$d=$_POST[‘d’];
$ext=$_POST[‘ex’];
If(is_dir($d))
{
$h=opendir($d);
Echo “<h3>The files with extension $ext in directory $d are: </h3>”;
While(($file=readdir($h))!==false)
If(is_dir($file))
{
Continue;
}
$e=explode(‘.’,$file);
If($e[1]==$ext)
{
Echo “<h3>$file</h3><br/>”;
}
}
Closedir($h);
}
?>
Assignment 4
Set A1
Q. Define an interface which has methods area( ), volume( ).
Define constant PI. Create a class cylinder which implements this
interface and calculate area and volume. (Hint: Use define(
))
Program:
HTML file=>
<html>
<head><title>A4A1</title></head>
Php program=>
<?php
$r=$_POST[‘rad’];
$h=$_POST[‘hit’];
Define(‘PPI’,’3.1412’);
Interface fun
{
Function area($r,$h);
Function volume($r,$h);
}
Function area($r,$h)
{
$area=(2*PPI*$r*($r+$h));
Echo “<center><h3>The area of cylinder is:
$area</font></center></h3>”;
}
Function volume($r,$h)
{
$v=(PPI*$r*$r*$h);
Echo “<center><h3>The volume of cylinder is:
$v</font></center></h3>”;
}
}
$o=new cylinder;
$o->area($r,$h);
$o->volume($r,$h);
?>
Set A2
Q: 2) Write class declarations and member function definitions for
an employee(code, name, designation). Derive
emp_account(account_no, joining_date) from employee and
emp_sal(basic_pay, earnings, deduction) from emp_account. Write
a menu driven program
a) To build a mastertable
b) To sort allentries
c) To search anentry
d) Displaysalary
Program:
HTML file=>
<!DOCTYPE html>
<html lang=”en” dir=”ltr”>
<head>
<meta charset=”utf-8”>
<title></title>
</head>
<body>
<form class=”” action=”a4a2.php” method=”post”>
<input type=”radio” name=”rad” value=”1”>Build a Master Table
<br><br>
<input type=”radio” name=”rad” value=”2”>Sort all entries <br><br>
<input type=”radio” name=”rad” value=”3”>Search an entry by
Employee’s Name
<input type=”text” name=”ename”> <br><br>
<input type=”radio” name=”rad” value=”4”>Display Salary <br><br>
<input type=”submit” name=”sub” value=”Submit”>
</form>
</body>
</html>
Php program=>
<?php
Class employee
{
Public $code,$name,$desig;
Function construct($a,$b,$c)
{
$this->code=$a;
$this->name=$b;
$this->desig=$c;
}
Public function display_emp()
{
Echo’
<td>’.$this->code.’</td>
<td>’.$this->name.’</td>
<td>’.$this->desig.’</td>
‘;
}
Public function getname()
{
Return $this->name;
}
}
Class emp_acc extends employee
{
Public $ano,$jdate;
Function construct($a,$b,$c,$d,$e)
{
Parent:: construct($a,$b,$c);
$this->ano=$d;
$this->jdate=$e;
}
Public function display_acc()
{
Echo’
<td>’.$this->ano.’</td>
<td>’.$this->jdate.’</td>
‘;
}
}
Class emp_sal extends emp_acc
{
Public $bpay,$earns,$ded,$total;
Function construct($a,$b,$c,$d,$e,$f,$g,$h)
{
Parent:: construct($a,$b,$c,$d,$e);$this->bpay=$f;
$this->earns=$g;
$this->ded=$h;
$this->total=$this->bpay + $this->earns - $this->ded;
}
Public function display_sal()
{
Echo’
<td>’.$this->bpay.’</td>
<td>’.$this->earns.’</td>
<td>’.$this->ded.’</td>
<td>’.$this->total.’</td>
‘;
}
}
$emp[0]=new
emp_sal(‘A923B’,’Ramesh’,’Staff’,’10001’,’21/09/2011’,’20000’
,’5000’,’3000’); $emp[1]=new
emp_sal(‘A823B’,’Ram’,’HR’,’10002’,’22/09/2011’,’25000’,’500
0’,’3000’);
$emp[2]=new
emp_sal(‘A723B’,’Sita’,’Analyst’,’10003’,’23/09/2011’,’22000’,’
5000’,’3000’);
$emp[3]=new
emp_sal(‘A623B’,’Gita’,’Organiser’,’10004’,’24/09/2011’,’21000
’,’5000’,’3000’);
$emp[4]=new
emp_sal(‘A523B’,’Aman’,’Manager’,’10005’,’25/09/2011’,’3000
0’,’5000’,’3000’);
$emp[5]=new
emp_sal(‘A423B’,’Amar’,’Staff’,’10006’,’26/09/2011’,’20000 ’,’ 5
000’,’3000’);
$emp[6]=new
emp_sal(‘A323B’,’Tarun’,’Analyst’,’10007’,’27/09/2011’,’21000’
,’5000’,’3000’);
$emp[7]=new
emp_sal(‘A223B’,’Mahi’,’Organiser’,’10008’,’28/09/2011’,’2100
0’,’5000’,’3000’);
$emp[8]=new emp_sal(‘A123B’,’Karna’,’Staff’,’10009
’,’29/09/2011’,’20000’,’5000’,’3000’); $emp[9]=new
emp_sal(‘A023B’,’Suraj’,’Staff’,’10010’,’30/09/2011’,’20000’,’50
00’,’3000’);
$ch=$_POST[‘rad’];
$ename=$_POST[‘ename’];
$flag=0;
Function mastertable($emp)
{
Echo ‘<table border=”1” width=”100%”>
<tr>
<th>Code</th>
<th>Name</th>
<th>Designation</th>
<th>Ac No</th>
<th>Joindate</th>
<th>BasicPay</th>
<th>Earnings</th>
<th>Deduction</th>
<th>Total Salary</th>
</tr>’;
For($i=0;$i<10;$i++)
{
Echo ‘<tr>’;
$emp[$i]->display_emp();
$emp[$i]->display_acc();
$emp[$i]->display_sal();
Echo ‘</tr>’;
}
Echo ‘</table>’;
}
Switch($ch)
{
Case 1:
Mastertable($emp);
Break;
Case 2: echo ‘Sorting w.r.t Employee Code <br><br>’;
Function srt($a,$b)
{
Return strcmp($a->code,$b->code);
}
Usort($emp,”srt”);
Mastertable($emp);
Break;
Case 3: echo “Searching for employee $ename…..<br><br>”;
For($i=0;$i<10;$i++)
{
$temp=$emp[$i]->getname();
If($temp==$ename)
{
$flag=1;
Break;
}
}
If($flag==0)
{
Echo “Not Found<br>”;
}
Else
{
Echo “Found in the records<br>”; }
Break;
Case 4: echo “Displaying Salary of all Employees …. <br><br>”;
Echo ‘<table border=”1” width=”100%”>
<tr>
<th>Employee Name</th>
<th>Basic Pay</th>
<th>Earnings</th>
<th>Deduction</th>
<th>Total Salary</th>
</tr>’;
For($i=0;$i<10;$i++)
{
Echo ‘<tr>
<td>’.$emp[$i]->getname().’</td>
‘;
$emp[$i]->display_sal();
}
Break;
}
?>
Set B1
Q:1) Create class rectangle and derive a class square from class
Rectangle. Create another class circle. Create an interface with
only one method called area(). Implement this interface in all
the classes. Include appropriate data members and constructors
in all classes. Write a program to accept details of a square,
circle and rectangle and display the area.
Program:
HTML file=>
<!DOCTYPE html>
<html lang=”en” dir=”ltr”>
<head>
<meta charset=”utf-8”>
<title></title>
</head>
<body>
<form class=”” action=”a4b1.php” method=”post”>
<li>
Rectangle <br><br>
Length: <input type=”text” name=”len”> <br><br>
Width: <input type=”text” name=”width”> <br><br>
</li>
<li>
Square <br><br>
Side: <input type=”text” name=”side”> <br><br>
</li>
<li>
Circle <br><br>
Radius: <input type=”text” name=”radius”> <br><br>
</li>
<input type=”submit” name=”submit” value=”Submit”>
</form>
</body>
</html>
Php program=>
<?php
Define(“PI”,”3.142”);
Interface a
{
Function area();
}
Class rectangle implements a
{
Public $l,$b;
Function construct($x,$y)
{
$this->l=$x;
$this->b=$y;
}
Function area()
{
$area=$this->l * $this->b ;
Echo “Area of the Rectangle is $area <br>”;}
}
Class square extends rectangle implements a
{
Public $s;
Function construct($p,$q,$r)
{
Parent:: construct($p,$q);
$this->s=$r;
}
Function area()
{
Parent::area();
$area=$this->s * $this->s;
Echo “Area of the Square is $area <br>”;
}
}
Class circle implements a
{
Public $r;
Function construct($x)
{
$this->r=$x;
}
Function area()
{
$area=PI * $this->r * $this->r;
Echo “Area of the Circle is $area <br>”;
}
}
$side=$_POST[‘side’];
$length=$_POST[‘len’];
$width=$_POST[‘width’];
$radius=$_POST[‘radius’];
$obj=new square($length,$width,$side);
$obj1=new circle($radius);
$obj->area();
$obj1->area(); ?>
Set B2
Q:2) Create a class account(accno,cust_name). Derive two
classes from account as saving_acc(balance, min_amount)and
current_acc(balance,min_amount). a) Display amenu
b) SavingAccount
Program:
HTML file=>
<!DOCTYPEhtml>
<htmllang=”en”>
<head>
<meta charset=”UTF-8”>
<meta name=”viewport” content=”width=device-width,
initialscale=1.0”>
<title>Hierarchical Inheritance</title>
</head>
<body>
<form action=”ex4B_2.php” method=”post”>
<br> Enter Name : <input type=”text” name=”nm”>
<br> Enter Acc_no : <input type=”text” name=”a_n”>
<br> Account Type : <input type=”radio” name=”a_t” value=”1”>
Savings
<br> <input type=”radio” name=”a_t” value=”2”> Current
<br> Enter Bal : <input type=”text” name=”bal”>
<br> Enter Min Amt : <input type=”text” name=”m_a”>
<br> <input type=”radio” name=”op” value=”1”> Create account
<br> Enter Amt to Deposit : <input type=”text” name=”dep”>
<br> <input type=”radio” name=”op” value=”2”> Deposit
<br> Enter Amt to Withdraw : <input type=”text” name=”wit”>
<br> <input type=”radio” name=”op” value=”3”> Withdrawal
<br> <input type=”submit” value=”Submit”>
</form>
</body>
</html>
Php file=>
<?php
$a_t = $_POST[“a_t”];$nm
= $_POST[“nm”];
$a_n = $_POST[“a_n”];
$bal = $_POST[“bal”];
$m_a = $_POST[“m_a”];
$ch =$_POST[“op”];
$d =$_POST[“dep”];
$w = $_POST[“wit”];
Class acc
{
Public $accno;
Public $name;
Publicfunction construct($a,$n)
{
$this->accno = $a;
$this->name = $n;
}
}
Class saving_acc extends acc
{
Public $bal;
Public $min_amt;
Publicfunction construct($a_n, $nm, $b,$m)
{
Parent:: construct($a_n,$nm);
$this->bal = $b;
$this->min_amt = $m;
Echo “<br> Account created”;
}
Public function deposit($d)
{
$this->bal += $d;
Echo “<br> Balance : $this->bal”;
Echo “<br> deposited”;
}
Public function withdraw($w)
{
If($this->bal - $w < $this->min_amt)
{
Echo “<br> Amt cannot be withdrawed”;
}
Else
{
$this->bal -= $w;
Echo “<br> Balance : $this->bal”;
Echo “<br> withdrawned”;
}
}
}
Class current_acc extends acc
{
Public $bal;
Public $min_amt;
Publicfunction construct($a_n, $nm, $b,$m)
{
Parent:: construct($a_n,$nm);
$this->bal = $b;
$this->min_amt = $m;
Echo “<br> Accountcreated”;
}
Public functiondeposit($dep)
{
$this->bal += $dep;
Echo “<br> Balance : $this->bal “;
Echo “<br> deposited”;
}
Public function withdraw($w)
{
If($this->bal - $w < $this->min_amt)
{
Echo “<br> Amt cannot be withdrawed”;
}
Else
{
$this->bal -= $w;
Echo “<br> Balance : $this->bal “;
Echo “<br> withdrawned”;
}
}
}
If($a_t ==1)
{
$obj = new saving_acc($a_n, $nm, $bal,$m_a);
Switch($ch)
{
Case 1:
{
Echo “<br> Object created”;
}break; Case
2:
{
$obj->deposit($d);
}break;
Case3:
{
$obj->withdraw($w);
}break;
}
}
If($a_t ==2)
{
$obj = new current_acc($a_n, $nm, $bal,$m_a);
Switch($ch)
{
Case 1:
{
Echo “<br> Object created”;
}break;
Case2:
{
$obj->deposit($d);
}break; Case
3:
$obj->withdraw($w);
}break;
}
}
Set C1
Q:1) Define an interface for stack operation. Implement thisinterface
in aclass.
Program:
<?php
Interface stack_op
{
Function push($item);
Function pop();
Function top();
Function isEmpty();
Public $stack,$limit;
Function construct($limit=10)
$this->stack=array();
$this->limit=$limit;
}
If(count($this->stack)<$this->limit){
Array_unshift($this->stack,$item);
Else
{
Die(“Stack Overflow!!”);
If($this->isEmpty())
Die(“Stackisempty!!”); }
Else
{
Return array_shift($this->stack);
Return current($this->stack);
}
Public function isEmpty()
Return empty($this->stack);
}
}
$obj=newstack(); $obj->push(“ABC”);
$obj->push(“DEF”);
$obj->push(“GHI”);
$obj->push(“KLM”);
Echo $obj->top(); Echo “<br>”;
$obj->push(“NOP”);
Echo $obj->pop();
?>
Set C2
Q:2) Write necessary class and member function definitions for a
cricket player object. The program should accept details from
user (max :10) (player_code, name, runs, innings_played,
no_of_times_out). The program should contain following menu.
Enter details of players. Display average runs of a single player.
Average runs of all players. Display the list of players in sorted
order as per runs(use function overloading)
Program:
HTML FILE=>
<!DOCTYPEhtml>
<htmllang=”en”>
<head>
<meta charset=”UTF-8”>
</head>
<body>
<form action=”ex4C_2.php” method=”post”>
</form>
</body>
</html>
Php file=>
<?php
$ch = $_POST[“ch”];
$nm = $_POST[“nm”];
Class player
{
Public $code;
Public $name;
Public $runs;
Public $innings_played;
Public $no_times_out;
Public $avg_run;
Publicfunction construct($c, $nm, $r, $ip, $nto)
$this->code = $c;
$this->name = $nm;
$this->runs = $r;
$this->innings_played = $ip;
$this->no_times_out = $nto;
{
$player->avg_run = $player->runs / ($player->innings_played -
$player->no_times_out);
<th>Name</th>
<th>Avg_runs</th>
</tr>’;
Echo “<tr>”;
Echo “<td>”.$player->name.”</td>”;
Echo “<td>”.$player->avg_run.”</td>”;
Echo “</table>”;
}
}
Function display($player)
<tr>
<th>Code</th>
<th>Name</th>
<th>Runs</th>
<th>Innings_played</th> <th>No_times_out</th>
</tr>’;
For($i=0;$i<10;$i++)
Echo ‘<tr>’;
Echo “<td>”.$player[$i]->code.”</td>”;
Echo “<td>”.$player[$i]->name.”</td>”;
Echo “<td>”.$player[$i]->runs.”</td>”;
Echo ‘</table>’;
}
Switch ($ch)
Case ‘1’:
{
Echo “Details of each player”;
Display($player);
}
Break;
Case ‘2’:
{
If(strcmp($player[$i]->name,$nm) == 0)
$player[$i]>avg_runs($player[$i]);
}
}
Break;
Case ‘3’:
}
Break;
Case ‘4’:
{
If($player[$i]->runs > $player[$j]->runs)
$p = $player[$i];
$player[$i] = $player[$j];
$player[$j] = $p;
}
}
Display($player);
}
Break;
Assignment 5
Set A1
Program:
HTML file:
<html>
<body>
<form action=”a5a1.php” method=”POST”>
Enter Department name : <input type=”text” name=”dept_name”
/></br>
<input type=”submit”/>
</form>
</body>
</html>
Phpfile:
<?php
$dept_name = $_POST[‘dept_name’];
Floor’),(4,’Electronics’,’3rdFloor’)”;
$res = pg_query($con,$dept_insert);
$emp_insert = “insert into employee values( 1,
‘abc’,’Pune’,’7894356’,’20000’,1),( 2,
‘abc’,’Pune’,’7894356’,’10000’, 1),( 3,
‘abc’,’Pune’,’7894356’,’30000’,3),( 4,
‘abc’,’Pune’,’7894356’,’120000’,2)”;
$res1 = pg_query($con,$emp_insert);
$result = pg_query($con,$strQuery);
$row = pg_fetch_row($result);
?>
<html>
<body> <table>
<tr>
<th>Maximum salary</th>
<th>Minimum salary</th>
<th>Sum</th>
</tr>
<tr>
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td> <td><?php
echo $row[2];?></td>
</tr>
</table> </body>
</html>
Set A2
Program:
HTML file:
<html>
<center>
<form method=GET action=a5a2.php>
Hospital Name : <input type=text name=hname>
<input type=submit value=Submit>
</form>
</center>
</html>
Php program=>
<?php
$hname = $_GET[‘hname’];
Program:
HTML file:
<html>
<head>
<title>
A5SETAB1
</title>
</head>
<body>
</form>
</body>
</html>
Php file:
<?php
$pname=$_POST[‘pname’];
If(!$db)
Else
$result=pg_query($query); If(!
$result)
Echo ‘<table>
<tr>
<th>Eno</th>
<th>Ename</th>
<th>JoinDate</th> <th>Qualifications</th>
</tr>’;
While($row=pg_fetch_row($result))
{
Echo ‘<table>
<tr>
<td>’.$row[0].’</td>
<td>’.$row[1].’</td>
<td>’.$row[2].’</td> <td>’.$row[3].’</td>
</tr>’;
}
Pg_close($db);
?>
Set B2
Q: 2) Consider the following entities and their relationships
student (sno integer, s_name char(30), s_class char(10), s_addr
char(50)) teacher (tno integer, t_name char (20), qualification char
(15),experience integer) The relationship betweenstudentteacher:
m-m with descriptive attributesubject.
Using above database write a script in PHP to accept a teacher name
from user and display the names of students along with subjects to
whom teacher is teaching
Program:
HTMLfile
<html>
<head>
<title>
A5SETAB2
</title>
</head>
<body>
<br><br>
</form>
</body>
</html>
Phpfile:
<?php
$tname=$_POST[‘tname’]; $db=pg_connect(“host=localhost
user=test_userdbname=tybcs01”);
If(!$db)
Else
Tname=’”.$tname.”’))”;
$result=pg_query($query);
If(!$result)
{
Die(“An error occurred…”);
Echo ‘<table>
<tr>
<th>Sname</th>
<th>Subject</th>
</tr>’;
While($row=pg_fetch_row($result))
Echo’
<tr>
<td>’.$row[0].’</td>
<td>’.$row[1].’</td>
</tr>’;
}
Echo ‘</table>’;
Pg_close($db);
?>
Set C1
Q: 1) Consider the following entities and their relationships Movie
(movie_no, movie_name, release_year) Actor (actor_no, name)
Relationship between movie and actor is many – many with
attribute rate in Rs. Create a RDB in 3 NF for the above and solve
following Using above database, write PHP scripts for the
following☹Hint: Create HTML form having three radio buttons)
a) Accept actor name and display the names of the moviesin
which he hasacted.
Program:
HTML File
<html>
<head>
<title>
A5SETC1
</title>
</head>
<body>
<formaction=”a5c1.php”method=”POST”> <input type=”radio”
name=”rad”value=”1”>
<br><br><br>
<br>
<br><br>
<br><br>
</form>
</body>
</html>
Php file:
<?php
If(!$db)
{
Else
{
Echo “Successfully connected to the database <br><br>”;
$ch=$_POST[‘rad’];
Switch($ch)
$result=pg_query($query);
If(!$result)
{
break;
}
While($row=pg_fetch_row($result))
{
Echo ‘’.$row[0].’’;
Echo “<br>”;
}
Break;
Case ‘2’: $mname=$_POST[‘mname’];
$ryear=$_POST[‘ryear’];
If(!$result)
Break;
}
Echo “Inserted new record successfully <br><br>”;
Break;
$ryear=$_POST[‘ryear’];
$query=”update table movie set ryear=’”.$ryear.”’
Where mname=’”.$mname.”’”;
$result=pg_query($query);
If(!$result)
{
Break;
}
Break;
}
Pg_close($db);
?>
Set C2
Q: 2) Considerer the following entities and their relationships
Student (Stud_id,name,class)
Competition (c_no,c_name,type)
Relationship between student and competition is many-many with
attribute rank and year. Create a RDB in 3NF for the above andsolve
the following. Using above database write a script in PHP to accept
a competition name from user and display information of student
who has secured 1 st rank in thatcompetition
Program:
HTMLfile
<html>
<head>
<title>
A5SETC2
</title>
</head>
<body>
</form>
</body>
</html>
Phpfile:
<?php
If(!$db)
{
Die(“An error occurred…”);
Else
$cname=$_POST[‘cname’];
Where c_name=’”.$cname.”’))”;
$result=pg_query($query);
If(!$result)
While($row=pg_fetch_row($result))
{
Echo ‘’.$row[0].’ <br>’; //id
Pg_close($db);
?>