PHP Practical Slips Solutions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

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 {

echo "Not a Palindrome";

?>

SLIP 1

Q. 4 Write a menu driven program to perform the following operations on an


associative array:

a. Display the elements of an array along with the keys.

b. Display the size of an array

c. Delete an element from an array from the given key/index.

d. Reverse the order of each element’s key-value pair [Hint: use array_flip()]

e. Traverse the elements in an array in random order [[Hint: use shuffle()]

Answer:
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>
<input type='submit' value='ok'> <input type='reset' value='cancel'><br>
</pre>
</form>
</body>
</html>

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

Q. 3 Write a menu driven program to perform the following operations on an

associative array:

a. Reverse the order of each element’s key-value pair [Hint: use array_flip()]

b. Traverse the elements in an array in random order [[Hint: use shuffle()].

Answer:

Use above answer and use only the applicable portion.

Q. 4 Declare a Multidimensional Array. Display specific element from a


Multidimensional array. Also delete given element from the Multidimensional array.
Answer:
<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>

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

a. to build a master table

b. to sort all entries

c. to search an entry

d. Display salary.

Answer:

<html>

<head>

<link rel="stylesheet" href="style/style.css">

</head>

<body>

<div class="container">

<div class="row py-3 " >

<div class="pt-4 col-md-5 form-group py-5 px-5" style="border:2px solid green">

<h4 class="text-center">Select following Options</h4><br>

<form action="#" method="post">

<input type="radio" name="r1" value="1" class=""> Master table<br>

<input type="radio" name="r1" id="" value="2"> Sorting By Emp_Code <br>

<div class="form-group row">

<label class="col-sm-5 col-form-label">

<input type="radio" name="r1" value="3"> Search By Name</label>

<div class="col-sm-6">

<input type="text" name="nm" class="form-control">

</div>

</div>
<input type="radio" name="r1" id="" value="4"> Display Salary <br><br>

<input type="submit" value="Submit" name="submit" class="btn btn-outline-primary">

<input type="reset" value="Clear" class=" ml-2 btn btn-outline-danger">

</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;

public function disemp(){

echo "<td>". $this->code ."</td><td>".$this->name."</td><td>".$this->des."</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 disacc(){

echo "<td>". $this->ano ."</td><td>".$this->jdate."</td>";

class emp_sal extends emp_acc{

public $bs, $earn, $ded, $total;

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;

public function dissal(){

echo "<td>".$this->bs ."</td><td>".$this->earn."</td><td>".$this->ded."</td><td>".$this-


>total."</td>";

$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){

echo "<table class='table'>

<tr><thead class='thead-dark'><th>Emp code</th>

<th>Emp Name</th><th>Designation</th>

<th>Account No</th><th>Joining Date</th>

<th>Basic Pay</th><th>Earning</th>

<th>Deduction</th><th>Total Salary</th></thead></tr>";

for($i=0; $i<5; $i++){

echo "<tr>";

$e1[$i]->disemp();

$e1[$i]->disacc();

$e1[$i]->dissal();

echo "</tr>";

echo"</table>";

switch($ch){

case 1 : mastertable($e1);

break;

case 2 : echo "Sorted details <br>";

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){

echo "<div class='alert alert-danger' role='alert'>

<em>Name Not Found</em>

</div>";

}else{

echo "<div class='alert alert-success' role='alert'>

<em>Name Found</em>

</div>";

break;

case 4 : echo "Display Salary <br>";

echo "<table class='table'>

<tr>

<thead class='thead-dark'>

<th>Emp Name</th>

<th>Basic Pay</th>

<th>Earning</th>

<th>Deduction</th>

<th>Total Salary</th>

</tr>";

for($i=0; $i<5; $i++){

echo "<tr> <td> ";

$e1[$i]->getName();

echo "</td>";
$e1[$i]->dissal();

echo "</tr></table>";

break;

?>

</div>

</div>

</div>

</body>

</html>

SLIP 4

Q. 3 Display information using superglobal variable.

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.

Q. 4 Write a PHP script to sort the following associative array :


array("Rahul"=>"31","Jui"=>"41","veena"=>"39","Rijul"=>"40") in

a. ascending order sort by value

b. ascending order sort by Key

c. descending order sorting by Value

d. descending order sorting by Key

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>

<form action="ABC.php" method="post">


<center>
<h2>Enter Students information :</h2>

<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>

<form action="ABC.php" method="post">


<center>
<h2>Enter Marks for Student:</h2>

<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> ";

echo "Name : ".$_POST['name']."<br>";


echo "Address : ".$_POST['addr']."\n";
echo "Class : ".$_POST['class']."\n";

echo "Java : ".$_POST['m1']."\n";


echo "PHP : ".$_POST['m2']."\n";
echo "ST : ".$_POST['m3']."\n";
echo "IT : ".$_POST['m3']."\n";
echo "Practical : ".$_POST['m3']."\n";
echo "Project : ".$_POST['m3']."\n";

?>

SLIP 6

Q. 3 Write a Ajax program to print a text file.

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();
?>

<form method="GET" action="new.php">


<fieldset>
<legend>Enter username and password</legend>

<p>Roll_No:&nbsp&nbsp&nbsp<input type="text" name="rno"><br>


<p>Name:&nbsp&nbsp&nbsp<input type="text" name="nm"><br>
<p>City:&nbsp&nbsp&nbsp:&nbsp&nbsp&nbsp<input type="text"
name="ct"><br><br>
<input type="submit" value=Submit>
</form>
New.php
<?php
$a=$_GET['rno'];
$b=$_GET['nm'];
$c=$_GET['ct'];
session_start();
$newt = $tm+60;
if($newt > time())
echo"time out";
else
{
echo"Roll_No=$a"."<br>";
echo"Name=$b"."<br>";
echo"City=$c"."<br>";
}
session_destroy();

?>

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>

Search<input type=text name=search size="20" onkeyup="m1(form.search.value)">

<input type=button value="submit" onclick="m1(form.search.value)">

</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;

echo "<table border=1>";


echo "<tr><th>Movie No</th><th>Movie Name</th><th>Release Year</th></tr>";

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

Q. 3 Write a program to change the color of the elements using jQuery.

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:

$("div").on( "click", "button", function( event ) {


$(event.delegateTarget ).css( "background-color", "green");
});

Q. 4 Write a program to show hide/show and fade effect using jQuery.

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>

<p>If you click on the "Hide" button, I will disappear.</p>

<button id="hide">Hide</button>
<button id="show">Show</button>

</body>
</html>

SLIP 10

Q. 3 Write a program for animation effect in jQuery.

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>

Q. 4 Write a program for fading effect in jQuery by Setting the duration.

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>

<p>Demonstrate fadeTo() with different parameters.</p>

<button>Click to fade boxes</button><br><br>

<div id="div1" style="width:80px;height:80px;background-color:red;"></div><br>


<div id="div2" style="width:80px;height:80px;background-color:green;"></div><br>
<div id="div3" style="width:80px;height:80px;background-color:blue;"></div>

</body>
</html>

You might also like