Advance PHP Slip Solution 1 To 6
Advance PHP Slip Solution 1 To 6
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.
<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>
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();
?>
<?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
<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;
}
function calc_vol($r,$h)
{
return pi*$r*$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>";
?>
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.
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.
<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>
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>