DBMS-Lab-5_Programming-in-PHP_(Aimal-Khan)
DBMS-Lab-5_Programming-in-PHP_(Aimal-Khan)
IN PHP
LAB # 05
Spring 2024
CSE-403L
Database Management System Lab
Submitted to:
Engr. Sumayyea Salahuddin
Friday, May 16, 2025
Unsatisfac- Stu-
Exemplary Acceptable Developing
tory dent
Score
Dimension out of
10 8 6 4 10
Marks
Specification Programs work and ex- Programs work Programs work Programs work
ceed specifications. and meet all speci- and meet partial but fail to meet
fications. specifications. any specifica-
tions.
Output Fig- All the output figures Most of the output Few of the output Output figures
ures/Graphics and graphics are shown figures and graph- figures and graph- and graphics are
clearly and labeled. ics are shown ics are shown shown clearly
clearly and la- clearly and la- and not labeled.
beled. beled.
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:
1. intval(value)
Purpose: Converts a given value to an integer.
Usage: This function is commonly used when you need to perform mathemati-
cal operations that require integer values. It's particularly useful when dealing
with data retrieved from sources where the type might not be strictly con-
trolled, such as user input 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,
converting 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 re-
tain their decimal components. 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 val-
ues 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 er-
rors.
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 using nu-
meric data in HTML or other text-based formats.
Task 3: Write PHP script that shows the division table displayed as in Table 5.2 using dif-
ferent 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. Display 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}";
}
</table>
</body>
</html>
Output:
Task 4: Explore PHP Object Oriented using examples showing classes, objects, inheritance,
and polymorphism.
// 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:
<!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/bootstrap.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-target="#imageModal" data-bs-
image="' . $image . '">';
echo '<img src="' . $image . '" class="img-thumbnail">';
echo '</a>';
echo '</div>';
echo '</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.getElementById('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' => 'Recruiter', 'dID' => 1],
['empID' => 2, 'empName' => 'Bob', 'empJob' => 'Marketer', 'dID' => 2],
['empID' => 3, 'empName' => 'Charlie', 'empJob' => 'Software Engineer', 'dID' => 3],
['empID' => 4, 'empName' => 'David', 'empJob' => 'Sales Representative', 'dID' => 4],
['empID' => 5, 'empName' => 'Eva', 'empJob' => 'Accountant', 'dID' => 5]
];
displayEmployees($employees, $departments);
?>
</body>
</html>
Output: