Bca PHP Lab Spiral
Bca PHP Lab Spiral
by
Ms.P.ASIYA PARVEEN,MCA
NOVEMBER-2024
DEPARTMENT OF COMPUTER SCIENCE
MARY MATHA COLLEGE OF ARTS AND SCIENCE
(Affiliated to Madurai Kamaraj University)
PERIYAKULAM EAST-625604
Name :
Reg.No :
SubjectCode :
This is to certify that the Bonafide record for practical work done in
the Mary Matha College of Arts and Science for the practical in the academic
year 2024-2025 held on ___________________
AIM:
To write a program to compute the Sum of Digits of a number.
Algorithm
Program
<!DOCTYPE html>
<html>
<head>
<title>Sum of Digits </title>
</head>
<body>
<h2>Sum of Digits of Number</h2>
<form method="post" action="<?php
echohtmlspecialchars($_SERVER["PHP_SELF"]);>">
Enter a number:
<input type="text" name="number">
<input type="submit" value="Calculate">
</form>
<?php
function sumOfDigits($number) {
$sum = 0;
while ($number != 0) {
$digit = $number % 10; // Get the last digit
$sum += $digit; // Add digit to sum
$number = (int)($number / 10);
return $sum;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["number"];
if (is_numeric($number) && $number >= 0 && is_int((int)$number)) {
$result = sumOfDigits($number);
echo "<p>Sum of digits of $number is: $result</p>";
} else {
echo "<p>Please enter a valid non-negative integer.</p>";
}
}
?>
</body>
</html>
OUTPUT:
Result:
Thus, the above program has been executed successfully and verified
the output.
Algorithm
Program
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert Item in Array</title>
</head>
<body>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$arrayInput = filter_input(INPUT_POST, 'array',
FILTER_SANITIZE_STRING);
$item = filter_input(INPUT_POST, 'item', FILTER_SANITIZE_STRING);
$position = filter_input(INPUT_POST, 'position', FILTER_VALIDATE_INT);
Output:
Result:
Thus, the above program has been executed successfully and verified
the output.
AIM:
Write a program to sort the following associative array:
array(“Sophia”=>”31”,”Jacob”=>”41”,”William”=>”39”,”Ramesh”=>
”40”)in
a)Ascending order sort by value.
b)Ascending order sort by key.
c)Descending order sort by value.
d)Descending order sort by key.
e)Transform a string to all uppercase value.
f)Transform a string to all lowercase value.
g)Make a the keys so that the first character of each word is uppercase.
Algorithm
Program
<?php
$associativeArray = array(
"Sophia" => "31",
"Jacob" => "41",
"William" => "39",
"Ramesh" => "40"
);
echo "<h2>Original Array:</h2>";
echo "<pre>";
print_r($associativeArray);
echo "</pre>";
ksort($associativeArray);
// (G) Transform the keys so that the first character of each word is uppercase
$transformedArray = array();
foreach ($associativeArray as $key => $value) {
$newKey = ucwords($key);
$transformedArray[$newKey] = $value;
}
echo "<h2>Transformed Array with Capitalized Keys:</h2>";
echo "<pre>";
print_r($transformedArray);
echo "</pre>";
?>
Output:
Result
Thus, the above program has been executed successfully and verified
the output.
AIM:
To Write a php program using nested for loop that display a chess
board.
Algorithm
Program
<?php
echo '<html><body>';
echo '<table border="1" cellspacing="0" cellpadding="0">';
for ($row = 0; $row < 8; $row++) {
echo '<tr>';
for ($col = 0; $col < 8; $col++) {
$color = ($row + $col) % 2 == 0 ? 'white' : 'black';
echo '<td style="width: 50px; height: 50px; background-color: ' . $color .
';"></td>';
}
echo '</tr>';
}
echo '</table>';
echo '</body></html>';
?>
Output
Result
Thus, the above program has been executed successfully and verified
the output.
AIM:
To Write a program to compute and return the square root of a given
number(Without default array function)(Input get using form).
Algorithm
Program
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Square Root Calculator</title>
</head>
<body>
<h1>Square Root Calculator</h1>
<form action="index.php" method="post">
<label for="number">Enter a number:</label>
<input type="text" id="number" name="number" required>
<input type="submit" value="Calculate Square Root">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = filter_input(INPUT_POST, 'number',
FILTER_VALIDATE_FLOAT);
Result
Thus, the above program has been executed successfully and verified
the output.
EX.NO:06 Fibonacci series using recursion
AIM:
To Write a php program to print Fibonacci series using recursion.
Algorithm
Program
<?php
function fibonacci($n) {
if ($n == 0) {
return 0;
} elseif ($n == 1) {
return 1;
} else {
return fibonacci($n - 1) + fibonacci($n - 2); // Recursive case
}
}
function printFibonacciSeries($n) {
for ($i = 0; $i < $n; $i++) {
echo fibonacci($i) . " ";
}
}
$n = 10;
echo "Fibonacci series up to $n terms: ";
printFibonacciSeries($n);
?>
Output
Result
Thus, the above program has been executed successfully and verified
the output.
Algorithm
Program
<!DOCTYPE html>
<html>
<head>
<title>Birthday Countdown</title>
</head>
<body>
<h2>Enter Your Birthday</h2>
<form method="post">
<label for="birthday">Birthday (YYYY-MM-DD):</label>
<input type="text" id="birthday" name="birthday" required>
<input type="submit" value="Calculate">
</form>
<?php
function validateDate($date, $format = 'Y-m-d') {
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
}
function birthdayCountdown($birthday) {
$currentDate = new DateTime();
$birthdayThisYear = new DateTime(date('Y') . '-' . date('m-d',
strtotime($birthday)));
return $interval->days;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$inputDate = $_POST['birthday'];
if (validateDate($inputDate)) {
$daysLeft = birthdayCountdown($inputDate);
echo "<p>There are $daysLeft days left until your next birthday!</p>";
} else {
echo "<p>Invalid date format. Please enter a valid date in YYYY-MM-
DD format.</p>";
}
}
?>
</body>
</html>
Output
Result
Thus, the above program has been executed successfully and verified
the output
EX.NO:08
current date-time in a COOKIE
AIM:
Write a program to store current date-time in a COOKIE and displays
the last,,Last visited on "date - time on the web page upon reopening of the
same page.
Algorithm
Program
<?php
$cookieName = "lastVisit";
if(isset($_COOKIE[$cookieName])) {
$lastVisit = $_COOKIE[$cookieName];
echo "Last visited on: " . $lastVisit . "<br>";
} else {
echo "This is your first visit!<br>";
}
Result
Thus, the above program has been executed successfully and verified
the output
EX.NO:09 Upload and display images inn particular directory
AIM:
To write a php program Upload and display images inn particular
directory
Algorithm
Program
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Upload</title>
</head>
<body>
<h2>Upload an Image</h2>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<label for="image">Choose an image:</label>
<input type="file" name="image" id="image" required>
<br><br>
<input type="submit" name="submit" value="Upload">
</form>
<?php
$target_dir = "uploads/";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
}
</body>
</html>
Output
Result
Thus, the above program has been executed successfully and verified
the output
EX.NO:10
Student details database using HTML Form and process
using PHP
AIM:
To write a php program To design an student details database using
HTML Form and process using PHP(Add,edit,delete ,view records)with login
option.
Algorithm
Step 1:Start the wamp server for the activation.put online.
Step 2:Open database.
Step 3:Create new folder to save the program that you type.
Step 4:Save the file in particular folder you have
created.(i.e)”name.php”.
Step 5:Click browser->localhost->programname.php.
Step 6:It will play the web.
Step 7:It will display the result on monitor.
Step 8:Stop the process.
Program
student_db:
USE student_db;
Users:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = md5($_POST['password']);
if (mysqli_num_rows($result) == 1) {
$_SESSION['username'] = $username;
header('Location: dashboard.php');
} else {
echo "Invalid username or password.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="index.php">
Username: <input type="text" name="username" required><br>
Password: <input type="password" name="password" required><br>
<button type="submit">Login</button>
</form>
</body>
</html>
dashboard.php:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: index.php');
exit();
}
include('db.php');
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION['username']; ?>! <a
href="logout.php">Logout</a></h2>
<h3>Students List</h3>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Email</th>
<th>Course</th>
<th>Actions</th>
</tr>
<?php
$query = "SELECT * FROM students";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<form method='post' action='dashboard.php'>";
echo "<td>{$row['id']}</td>";
echo "<td><input type='text' name='name' value='{$row['name']}'
required></td>";
echo "<td><input type='number' name='age' value='{$row['age']}'
required></td>";
echo "<td><input type='email' name='email'
value='{$row['email']}'></td>";
echo "<td><input type='text' name='course' value='{$row['course']}'
required></td>";
echo "<td>";
echo "<input type='hidden' name='id' value='{$row['id']}'>";
echo "<button type='submit' name='edit'>Edit</button>";
echo "<button type='submit' name='delete'>Delete</button>";
echo "</td>";
echo "</form>";
echo "</tr>";
}
?>
</table>
</body>
</html>
logout.php:
<?php
session_start();
session_destroy();
header('Location: index.php');
?>
db.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "student_db";
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
style.css:
/* General Styles */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
h2, h3 {
color: #333;
}
a{
text-decoration: none;
color: #fff;
padding: 8px 16px;
background-color: #333;
border-radius: 5px;
}
a:hover {
background-color: #555;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
}
button {
background-color: #28a745;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
/* Table Styling */
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
}
th {
background-color: #f8f9fa;
color: #333;
}
tr:hover {
background-color: #f1f1f1;
}
/* Logout Link */
.logout {
float: right;
background-color: #dc3545;
}
.logout:hover {
background-color: #c82333;
}
Output
Login Page:
Dashboard:
Result
Thus, the above program has been executed successfully and verified
the output
EX.NO:11
Employee details database using HTML Form and
process using PHP
AIM:
To design an employee details database using HTML Form and
process using PHP(Add,Edit,View and delete records)with login option and
some sample design.
Algorithm
Program
employee_db:
CREATE DATABASE employee_db;
USE employee_db;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
index.php:
<?php
session_start();
include('db.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$password = md5($_POST['password']);
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container">
<h2>Login</h2>
<form method="post" action="index.php">
<label for="username">Username</label>
<input type="text" name="username" required>
<label for="password">Password</label>
<input type="password" name="password" required>
<button type="submit">Login</button>
</form>
</div>
</body>
</html>
dashboard.php:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header('Location: index.php');
exit();
}
include('db.php');
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container">
<h2>Welcome, <?php echo $_SESSION['username']; ?>! <a
href="logout.php" class="logout">Logout</a></h2>
<label for="position">Position</label>
<input type="text" name="position" required>
<label for="department">Department</label>
<input type="text" name="department" required>
<label for="email">Email</label>
<input type="email" name="email" required>
<label for="phone">Phone</label>
<input type="text" name="phone" required>
<h3>Employee List</h3>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Department</th>
<th>Email</th>
<th>Phone</th>
<th>Actions</th>
</tr>
<?php
$query = "SELECT * FROM employees";
$result = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<form method='post' action='dashboard.php'>";
echo "<td>{$row['id']}</td>";
echo "<td><input type='text' name='name' value='{$row['name']}'
required></td>";
echo "<td><input type='text' name='position'
value='{$row['position']}' required></td>";
echo "<td><input type='text' name='department'
value='{$row['department']}' required></td>";
echo "<td><input type='email' name='email' value='{$row['email']}'
required></td>";
echo "<td><input type='text' name='phone' value='{$row['phone']}'
required></td>";
echo "<td>";
echo "<input type='hidden' name='id' value='{$row['id']}'>";
echo "<button type='submit' name='edit'>Edit</button>";
echo "<button type='submit' name='delete'>Delete</button>";
echo "</td>";
echo "</form>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
logout.php:
<?php
session_start();
session_destroy();
header('Location: index.php');
?>
style.css
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
h2, h3 {
color: #333;
}
.container {
width: 80%;
margin: 0 auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
margin-top: 30px;
}
form {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
}
button {
background-color: #28a745;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid #ddd;
}
th, td {
padding: 12px;
text-align: left;
}
th {
background-color: #f8f9fa;
color: #333;
}
tr:hover {
background-color: #f1f1f1;
}
.logout {
float: right;
background-color: #dc3545;
}
.logout:hover {
background-color: #c82333;
}
Output
Login Page
Dashboard:
Result
Thus, the above program has been executed successfully and verified
the output