0% found this document useful (0 votes)
68 views16 pages

Advance PHP Slip Solution 1 To 6

The document contains multiple PHP programming exercises, including creating a simple calculator class, generating XML files for student and cricket team data, demonstrating class introspection, and implementing an abstract class for shapes. It also includes scripts for displaying student details based on input and calculating areas and volumes of geometric shapes. The document provides code examples and outlines the structure of HTML forms and PHP scripts for various functionalities.

Uploaded by

rehankhan44700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views16 pages

Advance PHP Slip Solution 1 To 6

The document contains multiple PHP programming exercises, including creating a simple calculator class, generating XML files for student and cricket team data, demonstrating class introspection, and implementing an abstract class for shapes. It also includes scripts for displaying student details based on input and calculating areas and volumes of geometric shapes. The document provides code examples and outlines the structure of HTML forms and PHP scripts for various functionalities.

Uploaded by

rehankhan44700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ADVANCE PHP SLIP SOLUTION:

SLIP NO:01

A) Write a PHP script to create a simple calculator that can accept two numbers and
perform operations like add, subtract, multiplication. (Use the concept of Class)
Solution:
<?php
class MyCalculator {
private $_fval, $_sval;
public function __construct( $fval, $sval ) {
$this->_fval = $fval;
$this->_sval = $sval;
}
public function add() {
return $this->_fval + $this->_sval;
}
public function subtract() {
return $this->_fval - $this->_sval;
}
public function multiply() {
return $this->_fval * $this->_sval;
}
public function divide() {
return $this->_fval / $this->_sval;
}
}
$mycalc = new MyCalculator(12, 6);
echo $mycalc-> add()."\n"; // Displays 18
echo $mycalc-> multiply()."\n"; // Displays 72
echo $mycalc-> subtract()."\n"; // Displays 6
echo $mycalc-> divide()."\n"; // Displays 2
?>

b) Write a PHP script to create student.xml file which contains studentroll no, name,
address, college and course. Print students detail of specific course in tabular format
after accepting course as input.

XML FILE SAVE AS: slip1x.xml

<?xml version ="1.0"?>


<student>
<stud>
<rollno>111</rollno>
<name>BBBB</name>
<address>pune</address>
<college>FCC</college>
<course>FYBCA</course>
</stud>
<stud>
<rollno>222</rollno>
<name>CCC</name>
<address>pune</address>
<college>FC</college>
<course>SYBCA</course>
</stud>
</student>

File2: html file save as: slip1h.html

<html>
<body>
<center>
<table width="50%" border="2"><tr>
<form action="slip1p.php">
<tr><th>ENTER course TO SEARCH</th></tr><br/>
<td align="center"><br/>Name:<input type="text" name="course"><br/><br/>
submit:<input type="submit"><br/><br/></td>
</form></tr>
</body>
</html>

File3: php file save as: slip1p.php


<?php
$link=mysqli_connect("localhost","root","","test");
if(!$link)
{
echo "Error:Unable to connect to MYSQL.".PHP_EOL;
echo "Debugging errno:".mysqli_connect_errno().PHP_EOL;
echo "Debugging error:".mysqli_connect_errno().PHP_EOL;
exit;
}
$name=$_GET['course'];
$studs=simplexml_load_file("http://localhost/Advance%20php/slip1x.xml");
//print_r($studs);
foreach($studs as $stud)
{
$rollno=$stud->rollno;
$name=$stud->name;
$address=$stud->address;
$college=$stud->college;
$course=$stud->course;
$query="insert into stud values($rollno,'$name','$address','$college','$course')";
mysqli_query($link,$query);
}
$course=$_GET['course'];
$res=mysqli_query($link,"select*from stud where course='$course'");
if(@mysqli_num_rows($res)==0)
{
echo"$course doesn't exist in the table";
}
else
{
echo"<table border ='1'align=center width=60%>";
echo "<tr>
<td width=15% align =center >rollno</td>
<td width=15% align =center >namo</td>
<td width=15% align =center >address</td>
<td width=15% align =center >college</td>
<td width=15% align =center >course</td>
</tr>";
while ($arr=mysqli_fetch_row($res))
echo "<tr><td>$arr[0]</td>
<td>$arr[1]</td>
<td>$arr[2]</td>
<td>$arr[3]</td>
<td>$arr[4]</td>
</tr>";
echo "</table>";
}
?>
SLIP NO :02

A) Write a PHP script to demonstrate the introspection for examining classes and
objects. (use function get_declared_classes() ,get_class_methods() and get_class_vars()).

<?php
class Myclass
{
public $a;
public $b=305;
public $c='Akshay';
function Myclass()
{
//Myclass function
}
function myfun1()
{
//functin
}
function myfun2()
{
//functin
}
}
$class=get_declared_classes();
foreach($class as $cname)
{
echo"$cname<br>";
}
echo"<br>Class Methods are : <br>";
$m=get_class_methods('Myclass');
foreach($m as $mname)
{
echo"$mname<br>";
}
$cp=get_class_vars('Myclass');
echo"class variables are :<br>";
foreach($cp as $cpname => $v)
{
echo"$cpname : $v <br>";
}

?>
SLIP NO:03

A) Write a PHP script to demonstrate the introspection for examining classes and
objects. (use function get_declared_classes() ,get_class_methods() and get_class_vars()).

<?php
class Calculate
{public $a;
public $b;
function __construct($a,$b){
$this->a=$a;
$this->b=$b;
}
public function add()
{
$c=$this->a+$this->b;
echo"Addition = $c<br>";
}
public function subtract()
{
$c=$this->a-$this->b;
echo"Subtract = $c<br>";
}
public function multiply()
{
$c=$this->a*$this->b;
echo"Multiplication = $c<br>";
}
public function div()
{
$c=$this->a/$this->b;
echo"Division = $c";
}}
//$x=$_GET['a'];
//$y=$_GET['b'];
$calc=new Calculate(4,3);
$calc->add();
$calc->subtract();
$calc->multiply();
$calc->div();?>
B) Write a script to create “cricket.xm1” file with multiple elements as shown below:
<CricketTeam>
<Team country="India">
<player>ROHIT SHARMA</player>
<runs>2850</runs>
<wicket>5</wicket>
</Team>

<Team country="England">
<player>Ben Stokes</player>
<runs>2650</runs>
<wicket>25</wicket>
</Team>
<Team country="Australia">
<player>Steve Smith</player>
<runs>7000</runs>
<wicket>20</wicket>
</Team>
<Team country="West Indies">
<player>DJ BRAVO</player>
<runs>9000</runs>
<wicket>225</wicket>
</Team>
<Team country="Bangladesh">
<player>Shakib-al-Hasan</player>
<runs>4190</runs>
<wicket>135</wicket>
</Team>
</CricketTeam>
SLIP NO:04

A) Define a class Employee having private members — id, name, department, salary.
Define parameterized constructors. Create a subclass called “Manager” with private
member bonus. Create 3 objects of the Manager class and display the details of the
manager having the maximum total salary (salary + bonus).

Solution:
<?php
class Employee
{
private $eid,$ename,$edept,$sal;
function __construct($a,$b,$c,$d)
{
$this->eid=$a;
$this->ename=$b;
$this->edept=$c;
$this->sal=$d;
}
public function getdata()
{
return $this->sal;
}
public function display()
{
echo $this->eid."
";
echo $this->ename."
";
echo $this->edept."
";
}
}
class Manager extends Employee
{
private $bonus;
public static $total1=0;
function __construct($a,$b,$c,$d,$e)
{
parent::__construct($a,$b,$c,$d);
$this->bonus=$e;
}
public function max($ob)
{
$sal=$this->getdata();
$total=$sal+$this->bonus;
if($total>self::$total1)
{
self::$total1=$total;
return $this;
}
else
{
return $ob;
}
}
public function display()
{
parent::display();
echo self::$total1;
}

}
$ob=new Manager(0,"ABC","",0,0);
$ob1=new Manager(1,"ramdas","computer",28000,2000);
$ob=$ob1->max($ob);
$ob2=new Manager(2,"ramdas1","computer",30000,2500);
$ob=$ob2->max($ob);
$ob3=new Manager(3,"ramdas2","computer",32000,3000);
$ob=$ob3->max($ob);
$ob4=new Manager(4,"ramdas","computer",28000,4000);
$ob=$ob4->max($ob);
$ob5=new Manager(5,"ramdas","computer",28000,5000);
$ob=$ob5->max($ob);
$ob->display();
?>

B) Create an xml file which should comprise the following: Sachin


Tendulkar 2000 100 20 Forat least 5 players. Write a PHPscript to display the details of
players who have scored more than 1200 runs and atleast 50 wickets.
Solution:
File save as: slip4-B.php

<?php
$d=new DOMDocument();
$d->load("slip4-B.xml");

$run=$d->getElementsByTagName('runs');
$wic=$d->getElementsByTagName('wickets');
$name=$d->getElementsByTagName('player');

foreach($name as $n)
{
if($run>='12*00' && $wic>='50')
echo "<br>".$n->textContent;
else "not";
}
?>
XML File save as: slip4-B.xml

<?xml version='1.0' encoding='UTF-8'?>


<cricketinfo>
<cricket>
<player>abc</player>
<runs>1000</runs>
<wickets>50</wickets>
<noofnotout>10</noofnotout>
</cricket>

<cricket>
<player>def</player>
<runs>100</runs>
<wickets>40</wickets>
<noofnotout>10</noofnotout>
</cricket>

<cricket>
<player>pqr</player>
<runs>1020</runs>
<wickets>60</wickets>
<noofnotout>10</noofnotout>
</cricket>

<cricket>
<player>xyz</player>
<runs>9000</runs>
<wickets>90</wickets>
<noofnotout>40</noofnotout>
</cricket>

<cricket>
<player>lmn</player>
<runs>170</runs>
<wickets>80</wickets>
<noofnotout>8</noofnotout>
</cricket>
</cricketinfo>
Slip NO :5

A) Create an abstract class Shape with methods area( ) and volume( ). Derive three
classes rectangle (length, breath), Circle(radius) and Cylinder(radius, height), Calculate
area and volume of all. (Use Method overriding).
Php File save as: slip5-A.php

<?php
define('pi',3.14);
abstract class shape
{
abstract function calc_area($r,$h);
abstract function calc_vol($r,$h);
}
class circle extends shape
{
function calc_area($r,$a)
{
return pi*$r*$a;
}

class cylinder extends shape


{
function calc_area($r,$h)
{
return 2*pi*$r*($r+$h);
}

function calc_vol($r,$h)
{
return pi*$r*$r*$h;
}
}

class rectangle extends shape


{
function calc_area($r,$h)
{
return $r*$h;
}

}
$r=$_GET['r'];
$h=$_GET['h'];
$a=$r;
$ob=new rectangle();
echo "Area of rectangle ".$ob->calc_area($r,$h);
echo "</br>";
$ob=new cylinder();
echo "Area of cylinder ".$ob->calc_area($r,$h);
echo "</br>";
echo "Volume of cylinder".$ob->calc_vol($r,$h);
echo "</br>";
$ob=new circle();
echo "Area of circle ".$ob->calc_area($r,$a);
echo "</br>";
?>

HTML File save as: slip5-A.html


<html>
<body>
<form action="slip5-A.php" method=get>
<center><h2>Enter values for Cone & Cylinder</h2>
<p>Enter Radius </td><td><input type="text" name="r"><br>
<p>Enter Height</td><td> <input type="text" name="h"><br>
<p><input type="submit" value="calculate">
</form>
</body>
</html>

B) Create student table as follows: Student(sno, sname, standard, Marks, per). Write
AJAX script to select the student name and print the student’s details of particular
standard.

Html file save as :slip5-B.html


<html>
<body>
<form action=" slip5-B.php " method="get">
Enter student name Name:<input type="text" name=sname><br>
<input type="submit" value=submit>
</form>
</body>
</html>
Php file save as: slip5-B.php
<?php
$sname=$_GET['sname'];
$con=mysql_connect("localhost","root","");
$d=mysql_select_db("bca_programs",$con);
$result=mysql_query("select *from student where sname='$sname'");
while($row=mysql_fetch_array($result))
{
echo $row['sno']."--".$row['sname']."--".$row['per']."<br>";
}
?>

Other solution :

<html>
<head>
<script>
function showHint(str)
{
if(str=="")
{
document.getElementById("mydiv").innerHTML="";
returm;
}
if(window.XMLHttpRequest)
{
XHRobj=new XMLHttpRequest();
}
else
{
XHRobj=new ActiveXObject("Microsoft.XMLHTTP");
}
XHRobj.onreadystatechange=function()
{
if(XHRobj.readyState==4 && XHRobj.status ==200)
{
document.getElementById("mydiv").innerHTML=XHRobj.responseText;
}
}
XHRobj.open("GET","select.php?q=" + str,true);
XHRobj.send();
}
</script>
</head>
<body>
<p><b>Student Name:</b></p>
<form>
<?php
$database="test"; // database Name
$con=mysqli_connect("localhost","root","","test");
if(!$con)
{
die('could not connect:' .@mysqli_error());
}
$result=mysqli_query($con,"select*from student")or die (@mysqli_error());
?>
<SELECT name="sname" onchange="showHint(this.value)">
<option value ="">Select Name</option>
<?php whilw($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['sname']?>"><?php echo $row['sname']?>
</option>
<?php }?>
</select>
</form>
<div id="mydiv"><b> Student info.......</b></div>
</body>
</html>

File 2:Select.php
<?php
$database="test";
$q=$_GET['q'];
$con=@mysqli_connect("localhost","root","","test");
if(!$con)
{
die('could not connect:'/@mysql_error());
}
$result=@mysqli_query($con,"select * from student where sname =!$q")or
die(@mysqli_error());
echo "<table border='1'>";
echo "<tr><th>sno</th></th>sname</th><th>per</th></te>";
while($row=@mysqli_fetch_array($result))
{
echo "<tr><td>";
echo $row['sno'];
echo "</td><td>";
echo $row['sname'];
echo "</td><td>";
echo $row['per'];
echo "</td></tr>";
}
echo "</table>";
@mysqli_close($con);
?>
Slip N0 :06

A) Write a PHP script, which will return the following component of the URL (Using
response header) http://www.college.com/Science/CS.php List of Components:
scheme,host, path Expected output: Scheme: http Host: www.college.com Path:
/Science/CS.php

<?php
$url = 'http://www.college.com/Science/Cs.php';
$url=parse_url($url);
echo 'Scheme : '.$url['scheme']."<br>";
echo 'Host : '.$url['host']."<br>";
echo 'Path : '.$url['path']."<br>";
?>

B) Create employee table as follows EMP (eno, ename,designation, salary). Write Ajax
program to select the employees name and print the selected employee’s details.

Html file save as :slip6-B.html


<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
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","slip6-B.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="harsh">harsh</option>
<option value="lokya">lokya</option>
<option value="Aryan">Aryan</option>
<option value="Yash">Yash</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>

Ph p file save as: slip6-B.php


<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "q3";

$con = mysqli_connect($host, $user, $password,$dbname);

if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
?>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
padding: 5px;
}

th {text-align: left;}
</style>
</head>
<body>

<?php
$q = $_GET['q'];
mysqli_select_db($con,"ajax");
$sql="SELECT * FROM emp WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table>
<tr>
<th>Eid</th>
<th>Employeename</th>
<th>Designation</th>
<th>Contact Number</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['designation'] . "</td>";
echo "<td>" . $row['number'] . "</td>";
//echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>

You might also like