0% found this document useful (0 votes)
4 views

Lab-5

This document is a lab report for a Database Management System course, detailing various PHP programming tasks. It includes assessments on report quality, submission timeliness, program specifications, and communication skills. The tasks cover type casting, function usage, loops, object-oriented programming, and building a simple image gallery website.

Uploaded by

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

Lab-5

This document is a lab report for a Database Management System course, detailing various PHP programming tasks. It includes assessments on report quality, submission timeliness, program specifications, and communication skills. The tasks cover type casting, function usage, loops, object-oriented programming, and building a simple image gallery website.

Uploaded by

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

PROGRAMMING

IN PHP

LAB # 05

Spring 2025

CSE-403L
Database Management System Lab

Submitted by: Zeeshan Ahmad


Registration No.: 22PWCSE2168
Class Section: ‘B’

“I affirm that I have completed this work with integrity”

Submitted to:
Engr. Sumayyea Salahuddin
Friday, May 16, 2025

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
Database Management Systems Lab
LAB ASSESSMENT RUBRICS

Unsatis- Stu-
Exemplary Acceptable Developing
factory dent
Scor
Dimen-
e out
sion of 10
10 8 6 4 Mark
s

Overall Report is com- Report is Report is Report is


Impres- plete, well writ- complete, mostly com- incomplete,
sion of ten, and orga- briefly writ- plete, loosely sloppy
Lab Re- nized appropri- ten, and or- written, and and/or dis-
port ately with addi- ganized. fairly orga- organized.
tional elements There are few nized. There There are
that enhance it. spellings are spellings many
There are no and/or gram- and/or gram- spellings
spellings or mar errors. mar errors. and gram-
grammar errors. mar errors
that affect
clarity.

Submis- Report is submit- Report is sub- Report is sub- Report was


sion ted on time. mitted within mitted within more than
24 hours of 72 hours of 3 days
due date. due date. overdue.

Specifica- Programs work Programs Programs Programs


tion and exceed spec- work and work and work but
ifications. meet all meet partial fail to meet
specifica- specifica- any specifi-
tions. tions. cations.

Verbal Answered clearly Answered Answered Answered


Commu- and accurately clearly and somewhat wrongly
nication with sufficient accurately clearly and and inaccu-
and knowledge. with average somewhat ac- rately with
Under- knowledge. curately with no knowl-
standing limited edge.
knowledge.
Output All the output fig- Most of the Few of the Output fig-
Figures/ ures and graph- output fig- output fig- ures and
Graphics ics are shown ures and ures and graphics
clearly and la- graphics are graphics are are shown
beled. shown clearly shown clearly clearly and
and labeled. and labeled. not labeled.

Marks: (_______+_______+_______+_______)/5= _______

Teacher Remarks and Signature:


____________________________________________________

Lab Tasks:
Task 1: In contrast to settype(), there is another method that
causes a variable’s value to be treated as a specific type. It is
known as type casting. Note that the variable itself remains un-
affected during type casting. Consider the following variable:
 $test_var = 8.23;
Type cast this variable’s value to integer, string, and Boolean
and show result using echo.

Code:
<?php
$test_var = 8.23;

// Casting to integer
$int_var = (int)$test_var;
echo "Integer: " . $int_var . "<br>";

// Casting to string
$str_var = (string)$test_var;
echo "String: " . $str_var . "<br>";

// Casting to Boolean
$bool_var = (bool)$test_var;
echo "Boolean: " . ($bool_var ? 'true' : 'false') . "<br>";
?>

Output:
Task 2: Use and specify the purpose of following functions:
 intval(value )
 floatval(value )
 strval(value )

1. intval(value)
 Purpose: Converts a given value to an integer.
 Usage: This function is commonly used when you
need to perform mathematical operations that re-
quire integer values. It's particularly useful when
dealing with data retrieved from sources where the
type might not be strictly controlled, such as user in-
put or database calls.
 Example: If you have a string '123.45' and you use
intval('123.45'), the result will be 123. This function
also handles other types like Booleans and nulls, con-
verting true to 1 and false or null to 0.
2. floatval(value)
 Purpose: Converts a given value to a floating point
number (float).
 Usage: This is useful when you need to ensure that
operations on numbers retain their decimal compo-
nents. It is particularly important when calculations
require precision, such as in financial calculations or
scientific computations.
 Example: When converting the string '123.45' with
floatval('123.45'), the output will be 123.45 as a
float.
3. strval(value)
 Purpose: Converts a given value to a string.
 Usage: This function is essential when you need to
concatenate numerical values with strings or when
you need to output data as part of a text. It ensures
that numeric values can be easily appended to text
strings without causing errors.
 Example: If you have an integer 123, using
strval(123) will convert it to the string '123'. This is
helpful for outputting numbers within text or for us-
ing numeric data in HTML or other text-based for-
mats.

Task 3: Write PHP script that shows the division table dis-
played as in Table 5.2 using different loops. For each number,
display whether that number is an odd or even number, and
also display a message if the number is a prime number. Dis-
play this information within an HTML table.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>5.3: Division Table</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 5px;
text-align: center;
}
</style>
</head>
<body>
<h1>Task 5.3: Division Table</h1>

<table>
<tr>
<th>/</th>
<?php for ($i = 1; $i <= 10; $i++): ?>
<th><?= $i ?></th>
<?php endfor; ?>
</tr>

<?php
function is_prime($num) {
if ($num < 2) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}

function classify_number($num) {
$type = fmod($num, 2) == 0 ? "even" : "odd";
$prime = is_prime($num) ? "and prime" : "";
return "{$type} {$prime}";
}

for ($row = 1; $row <= 10; $row++) {


echo "<tr>";
echo "<th>$row</th>";
for ($col = 1; $col <= 10; $col++) {
$result = $row / $col;
// Check if the result is an integer, if so, classify it
if (intval($result) == $result) {
$description = classify_number($result);
} else {
$description = ""; // Do not classify non-integers
}
$formattedResult = round($result, 3);
echo "<td>{$formattedResult} {$description}</td>";
}
echo "</tr>";
}
?>

</table>
</body>
</html>

Output:
Task 4: Explore PHP Object Oriented using examples showing
classes, objects, inheritance, and polymorphism.

Code for Class:


<?php
class Car {
// Properties
public $color;
public $model;

// Constructor
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}

// Method
public function getDetails() {
return $this->color . " " . $this->model;
}
}

// Creating an object
$myCar = new Car("red", "Toyota");
echo $myCar->getDetails(); // Outputs: red Toyota
?>
Code for Inheritance:
<?php
class Vehicle {
public $brand;
public function __construct($brand) {
$this->brand = $brand;
}

public function start() {


return "Starting the engine...";
}
}

class Motorcycle extends Vehicle {


public function wheelie() {
return "Performing a wheelie!";
}
}

$motorcycle = new Motorcycle("Harley-Davidson");


echo $motorcycle->start(); // Outputs: Starting the engine...
echo $motorcycle->wheelie(); // Outputs: Performing a wheelie!
?>

Code for Polymorphism:


<?php
interface Animal {
public function makeSound();
}

class Dog implements Animal {


public function makeSound() {
return "Bark";
}
}

class Cat implements Animal {


public function makeSound() {
return "Meow";
}
}

function animalSound(Animal $animal) {


echo $animal->makeSound();
}

$dog = new Dog();


$cat = new Cat();

animalSound($dog); // Outputs: Bark


animalSound($cat); // Outputs: Meow
?>

Task 5: Build an image gallery website using PHP without


database. Your website must show all the images in a given di-
rectory. Use bootstrap/CSS in your website. Please note that
when a given image is clicked, it enlarges it and shows in
higher resolution. Also, your website should be flexible enough
to show the images when number of images are increased or
decreased from a given directory.

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>5.5: Image Gallery</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstra
p.min.css" rel="stylesheet">
<style>
.gallery img {
width: 100%;
height: auto;
padding: 5px;
}
.modal-body img {
width: 100%;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="row">
<?php
$dir = "../images/";
$images = glob($dir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($images as $image) {
echo '<div class="col-lg-3 col-md-4 col-sm-6 mb-4">';
echo '<div class="gallery">';
echo '<a href="#" data-bs-toggle="modal" data-bs-tar-
get="#imageModal" data-bs-image="' . $image . '">';
echo '<img src="' . $image . '" class="img-thumbnail">';
echo '</a>';
echo '</div>';
echo '</div>';
}
?>
</div>
</div>

<!-- Modal -->


<div class="modal fade" id="imageModal" tabindex="-1" aria-la-
belledby="imageModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="imageModalLabel">Image
Preview</h5>
<button type="button" class="btn-close" data-bs-dis-
miss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<img src="" id="modalImage" alt="Image preview">
</div>
</div>
</div>
</div>

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.
bundle.min.js"></script>
<script>
var imageModal = document.getElementById('imageModal');
imageModal.addEventListener('show.bs.modal', function
(event) {
var button = event.relatedTarget;
var src = button.getAttribute('data-bs-image');
var modalImage = document.getElement-
ById('modalImage');
modalImage.src = src;
});
</script>
</body>
</html>
Output:
Task 7: Implement the following relationship in PHP.
 Department: depID, depName
 Emplyee: empID, empName, empJob, dID
Note that the department has at least one or many employees
working in it; while employee works exactly in one department.
Also, feed at least five records in the created tables.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>7: Company Structure</title>
</head>
<body>
<h1>Employees Information</h1>
<?php
$departments = [
['depID' => 1, 'depName' => 'Human Resources'],
['depID' => 2, 'depName' => 'Marketing'],
['depID' => 3, 'depName' => 'Engineering'],
['depID' => 4, 'depName' => 'Sales'],
['depID' => 5, 'depName' => 'Finance']
];

$employees = [
['empID' => 1, 'empName' => 'Alice', 'empJob' => 'Re-
cruiter', 'dID' => 1],
['empID' => 2, 'empName' => 'Bob', 'empJob' => 'Mar-
keter', 'dID' => 2],
['empID' => 3, 'empName' => 'Charlie', 'empJob' => 'Soft-
ware Engineer', 'dID' => 3],
['empID' => 4, 'empName' => 'David', 'empJob' => 'Sales
Representative', 'dID' => 4],
['empID' => 5, 'empName' => 'Eva', 'empJob' => 'Accoun-
tant', 'dID' => 5]
];

function getDepartmentName($departments, $depID) {


foreach ($departments as $department) {
if ($department['depID'] == $depID) {
return $department['depName'];
}
}
return "Department not found";
}
function findEmployeesByDepartment($employees, $depID) {
$deptEmployees = [];
foreach ($employees as $employee) {
if ($employee['dID'] == $depID) {
$deptEmployees[] = $employee;
}
}
return $deptEmployees;
}

function displayEmployees($employees, $departments) {


foreach ($employees as $employee) {
$departmentName = getDepartmentName($departments,
$employee['dID']);
echo "Name: " . $employee['empName'] . ", Job: " . $em-
ployee['empJob'] . ", Department: " . $departmentName . "<br>";
}
}

displayEmployees($employees, $departments);
?>
</body>
</html>

Output:

You might also like