Professional Documents
Culture Documents
PHP Practical Slips Solutions
PHP Practical Slips Solutions
PHP Practical Slips Solutions
SLIP 1
Q. 3 Accept a string from the user and check whether it is a palindrome or not.
(Implement stack operations using array built-in functions).
Answer:
<?php
// PHP code to check for Palindrome string in PHP
// Using strrev()
function Palindrome($string){
if (strrev($string) == $string){
return 1;
}
else{
return 0;
}
}
// Driver Code
$original = "DAD";
if(Palindrome($original)){
echo "Palindrome";
else {
?>
SLIP 1
d. Reverse the order of each element’s key-value pair [Hint: use array_flip()]
Answer:
HTML File:
<html>
<form action='ass2a1.php' method='post'>
<pre> <h3>OPERATIONS</h3>
PHP Function:
NOTE: PHP function is saved as "ass2a1.php"
<?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;
?>
SLIP 2
associative array:
a. Reverse the order of each element’s key-value pair [Hint: use array_flip()]
Answer:
SLIP 3
Q. 3 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())
Answer:
<?php
interface Cyl
{
function area();
function volume();
}
class Cylinder implements Cyl
{
public $PI=3.14;
public $a;
public $r;
public $h;
function __construct($r,$h)
{
$this->r=$r;
$this->h=$h;
}
function area()
{
$this->a=2*$this->PI*($this->h*$this->r);
echo"Area of Cylinder= ".$this->a."<br>";
}
function volume()
{
$this->a=$this->PI*$this->r*$this->r*$this->h;
echo"Volume of Cylinder= ".$this->a."<br>";
}
}
$c=new Cylinder(10,7);
$c->area();
$c->volume();
?>
Q. 4 Write class declarations and member function definitions for an employee(code,
name, designation). Design derived classes as emp_account(account_no, joining_date)
from employee and emp_sal(basic_pay, earnings, deduction) from emp_account.
Write a menu driven program
c. to search an entry
d. Display salary.
Answer:
<html>
<head>
</head>
<body>
<div class="container">
<div class="col-sm-6">
</div>
</div>
<input type="radio" name="r1" id="" value="4"> Display Salary <br><br>
</form>
</div>
<div class="col-md-7">
<?php
if(isset($_POST['submit'])){
class employee{
public $code,$name,$des;
function __construct($a,$b,$c){
$this->code=$a;
$this->name=$b;
$this->des=$c;
return $this->name;
function __construct($a,$b,$c,$d,$e){
parent::__construct($a,$b,$c);
$this->ano=$d;
$this->jdate=$e;
}
function __construct($a,$b,$c,$d,$e,$f,$g,$h){
parent::__construct($a,$b,$c,$d,$e);
$this->bs=$f;
$this->earn=$g;
$this->ded=$h;
$this->total = $this->bs+$this->earn-$this->ded;
$e1[0]=new emp_sal(1,"Akash","HOD",10001,"02/02/2009",30000,1000,200);
$e1[1]=new emp_sal(2,"Akash","HOD",10002,"12/10/2012",29000,3500,400);
$e1[2]=new emp_sal(3,"Ramesh","HOD",10003,"18/11/2013",24000,2500,250);
$e1[3]=new emp_sal(4,"Swara","HOD",10004,"19/05/2015",21000,3000,650);
$e1[4]=new emp_sal(5,"Priya","HOD",10005,"26/07/2017",27000,4000,750);
$ch=$_POST['r1'];
$nm=$_POST['nm'];
$flag=0;
function mastertable($e1){
<th>Emp Name</th><th>Designation</th>
<th>Basic Pay</th><th>Earning</th>
<th>Deduction</th><th>Total Salary</th></thead></tr>";
echo "<tr>";
$e1[$i]->disemp();
$e1[$i]->disacc();
$e1[$i]->dissal();
echo "</tr>";
echo"</table>";
switch($ch){
case 1 : mastertable($e1);
break;
function srt($a,$b){
return strcmp($a->code,$b->code);
mastertable($e1);
usort($e1,"srt");
break;
case 3 :
for($i=0;$i<5; $i++){
$t=$e1[$i]->getName();
if($t==$nm){
$flag=1;
break;
if($flag==0){
</div>";
}else{
<em>Name Found</em>
</div>";
break;
<tr>
<thead class='thead-dark'>
<th>Emp Name</th>
<th>Basic Pay</th>
<th>Earning</th>
<th>Deduction</th>
<th>Total Salary</th>
</tr>";
$e1[$i]->getName();
echo "</td>";
$e1[$i]->dissal();
echo "</tr></table>";
break;
?>
</div>
</div>
</div>
</body>
</html>
SLIP 4
Answer:
Write down statements that can be used to display values of the superglobal
variables. Demo code already created in the class and shared on Teams.
Answer:
This is a very simple doable program where you have to use variety of array sort functions.
SLIP 5
Q. 3 Write a script to keep track of number of times the web page has been accessed.
Answer:
<html>
<head>
<title> Number of times the web page has been viited.</title>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['count']))
$_SESSION['count']=$_SESSION['count']+1;
else
$_SESSION['count']=1;
echo "<h3>This page is accessed</h3>".$_SESSION['count'];
?>
Q. 4 Create a form to accept student information (name, class, address). Once the
student information is accepted, accept marks in next form (Phy, Bio, Chem, Maths,
Marathi, English) .Display the mark sheet for the student in the next form containing
name, class, marks of the subject, total and percentage.
Answer:
Html file :
<html>
<body>
<table>
<tr><td>Name : </td><td><input type="text" name="name"></td><tr>
<tr><td>Address : </td><td><input type="text" name="addr"></td></tr>
<tr><td>Class : </td><td><input type="text" name="class"></td></tr>
<tr><td></td><td><input type="submit" value=Next></td></tr>
</table>
</center>
</form>
</body>
</html>
Php file :
ABC.php :
<html>
<body>
<table>
<tr><td>Java : </td><td><input type="text" name="m1"></td><tr>
<tr><td>PHP : </td><td><input type="text" name="m2"></td></tr>
<tr><td>ST : </td><td><input type="text" name="m3"></td></tr>
<tr><td>IT : </td><td><input type="text" name="m4"></td></tr>
<tr><td>Practical : </td><td><input type="text" name="m5"></td></tr>
<tr><td>Project : </td><td><input type="text" name="m6"></td></tr>
<tr><td></td><td><input type="submit" value=Next></td></tr>
</table>
</center>
</form>
</body>
</html>
ABC.php :
<?php
echo "<h3>Marksheet</h3> ";
?>
SLIP 6
Answer:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc() {
req=new XMLHttpRequest();
req.onreadystatechange=function() {
if (req.readyState==4 && req.status==200) {
document.getElementById("div1").innerHTML=req.responseText;
}
} // end of onreadstatechange function
req.open("GET","myinfo.txt",true);
req.send();
} // end of loadXMLDoc()
</script>
</head>
<body>
<div id="div1">my first ajax program</div>
<input type="button" id="button1" value="click me!" onclick="loadXMLDoc()"/>
</body>
</html>
Q. 4 Create a login form with a username and password. Once the user logs in, the
second form should be displayed to accept user details (name, city, phoneno). If the
user doesn’t enter information within a specified time limit, expire his session and
give a warning.
Answer:
Login.html
<html>
<form method="POST" action="login.php" >
<p>User Name: <input type="text" name="user"><br>
<p>Password :<input type="password"><br><br>
<input type="submit" value="Login"><br>
</form>
</html>
Login.php
<?php
$auth_yes=0;
session_start();
session_register();
#tm=time();
?>
?>
SLIP 7
Q. 3 Write a PHP program to display minimum, maximum and average salary of an
employee.(use emp and dept table from database)
Answer:
HTML file
<!DOCTYPE html>
<html>
<head><title>Min Max Salary</title></head>
<body>
<?php
$con=mysqli_connect(“localhost”,”root”,””,”mydatanase”);
mysqli_select_db($con,”mydatabase”);
$sql=”SELECT emp.emp_id, salary FROM emp, dept WHERE emp.emp_id =
dept.emp_id”;
$result=mysqli_query($con,$sql);
if(!$result){die(“can not find data”);}
$employee = array();
$i=0;
while($row=myqli_fetch_array($result,MYSQLI_ASSOC)){
$employee[$i]=$row[“salary”];
}
$min_sal=min($emploee);
$max_sal=max($employee);
$avg_sal=array_sum($employee) / count($employee);
echo “Minimum salary = “.$min_sal.”<br>”.
echo “Maximum salary = “.$max_sal.”<br>”;
echo “Average salary = “.$avg_sal;
?>
Q. 4 Write a PHP program to display the details of a Doctor as per the hospital name
is entered in textbox.(use hospital and doctor table from database)
Answer:
<!DOCTYPE html>
<html>
<head>
<script>
function showDoc(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "nothing to show!";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getresult.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h1>Using AJAX </h1>
<form>
<input type=”text” name="docname" onchange="showDoc(this.value)">
</form>
<br>
<div id="txtHint"><b>Doctor info will be listed here...</b></div>
</body>
</html>
Getresult.php
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 50%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
//for assigning categories as below, switch case is a better option!!
if($q == '1'){$qi = 'BREADS';} else {$qi = 'CAKES';}
if($q == '3'){$qi = 'COOKIES';}
$con = mysqli_connect('localhost','root','','hb');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"hb");
//category is a column in the database table masteritemlist in the database named hb
$sql="SELECT * FROM masteritemlist WHERE category = '".$qi."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Item Name</th>
<th>Price</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['item_name'] . "</td>";
echo "<td>" . $row['price'] . " / " . $row['unit'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html> <!DOCTYPE html>
<html>
<head>
<style>
table {
width: 50%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','hospitalDB');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"hospitalDB");
//category is a column in the database table masteritemlist in the database named hb
$sql="SELECT doctors.doc_name, doc_phone, hospital_name FROM doctors, hospitals
WHERE doctors.doc_id = hospitals.doc_id AND doctors.doc_name = '".$qi."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Doctor Name</th>
<th>Doctor Phone</th>
<th>Hospital Name</th>
</tr>";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo "<tr>";
echo "<td>" . $row['doc_name] . "</td>";
echo "<td>" . $row['doc_phone'] . "</td>";
echo "<td>" . $row['hospital_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
SLIP 8
Q. 3 Write Ajax program to fetch suggestions when is user is typing in a textbox. (eg
like google suggestions. Hint create array of suggestions and matching string will be
displayed )
Answer:
Html file :
<html>
<head>
<script type="text/javascript" >
function m1(str)
{
var ob=false;
ob=new XMLHttpRequest();
ob.open("GET","slip_15.php?q="+str);
ob.send();
ob.onreadystatechange=function()
{
if(ob.readyState==4 && ob.status==200)
{
document.getElementById("a").innerHTML=ob.responseText;
}
}
}
</script>
</head>
<body>
<form>
</form>
suggestions :<span id="a"></span><br>
</body>
</html>
Php file :
<?php
$a=array("pune","satara","nashik","sangli","mumbai","murud","akola","dound","dhule","ratnagiri","rajpur");
$q=$_GET['q'];
if(strlen($q)>0)
{
$match="";
for($i=0;$i<count($a);$i++)
{
if(strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if($match=="")
$match=$a[$i];
else $match=$match.",".$a[$i];
}
}
if($match=="")
echo "No Suggestios";
else echo $match;
}
?>
Q. 4 Write Ajax program to print Movie details by selecting an Actor’s name. Create
table MOVIE and ACTOR as follows with 1 : M cardinality MOVIE (mno, mname,
release_yr) and ACTOR(ano, aname)
Answer:
Html file :
<html>
<head>
<script type="text/javascript">
function display(a_name)
{
// alert("hi");
var ob=false;
ob=new XMLHttpRequest();
ob.onreadystatechange=function()
{
if(ob.readyState==4 && ob.status==200)
document.getElementById("place").innerHTML=ob.responseText;
}
ob.open("GET","slip_14.php?a_name="+a_name,true);
ob.send();
}
</script>
</head>
<body>
Select Actor Name :
<select name="a_name" onchange="display(this.value)">
<option value="">select actor</option>
<option value="Swapnil Joshi">Swapnil Joshi</option>
<option value="Sachit">Sachit</option>
</select>
<div id="place"></div>
</body>
</html>
Php file :
<?php
$a_name = $_GET['a_name'];
$con = mysql_connect("localhost","root","");
$d = mysql_select_db("bca_programs",$con);
$q = mysql_query("select movie.m_no,m_name,r_year from movie,actor_slip_14 where
a_name='$a_name' && actor_slip_14.m_no=movie.m_no"); //echo $q;
while($row = mysql_fetch_array($q))
{
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "</tr>";
}
echo "</table>";
?>
SLIP 9
Answer:
HTML Code:
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div style="background-color:yellow">
<p>Click the button to change the background color of this
paragraph.</p>
<button>Click me!</button>
</div>
</body>
</html>
Javascript code:
Answer:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
SLIP 10
Answer:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left: '250px',
opacity: '0.5',
height: '150px',
width: '150px'
});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<p>By default, all HTML elements have a static position, and cannot be moved. To manipulate
the position, remember to first set the CSS position property of the element to relative, fixed, or
absolute!</p>
<div style="background:#98bf21;height:100px;width:100px;position:absolute;"></div>
</body>
</html>
Answer:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
});
</script>
</head>
<body>
</body>
</html>