php project

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

(Season (2023-24))

PRACTICAL FILE

BCA503: Web designing with Php

Submitted to:- Submitted by:-


Mr. Neeraj Goyal SIR MADHUKAR CHAUHAN
(Assistant Professor) (BCAN1CA21048)
Exercise 1

1. Write a PHP program to find maximum between three numbers.

Solution:-
<?php

function findMax($num1, $num2, $num3) {


$max = $num1;

if ($num2 > $max) {


$max = $num2;
}

if ($num3 > $max) {


$max = $num3;
}

return $max;
}

// Example usage:
$num1 = 10;
$num2 = 20;
$num3 = 30;

$maxNumber = findMax($num1, $num2, $num3);


echo "The maximum number among $num1, $num2, and $num3 is:
$maxNumber";

?>
Output:-
2. Write a PHP program to input any alphabet and check whether it is
vowel or consonant.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vowel or Consonant Checker</title>
</head>
<body>

<form method="post" action="">


<label for="alphabetInput">Enter an alphabet:</label>
<input type="text" id="alphabetInput" name="alphabetInput"
maxlength="1">
<input type="submit" value="Check">
</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$alphabet = $_POST["alphabetInput"];

if (ctype_alpha($alphabet)) {
$vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];

if (in_array($alphabet, $vowels)) {
echo "$alphabet is a vowel.";
} else {
echo "$alphabet is a consonant.";
}
} else {
echo "Invalid input. Please enter a valid alphabet.";
}

?>

</body>
</html>

Output:-
3. Write a PHP program to input any character and check whether it is
alphabet, digit or special character.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Character Type Checker</title>
</head>
<body>

<form method="post" action="">


<label for="characterInput">Enter a character:</label>
<input type="text" id="characterInput" name="characterInput"
maxlength="1">
<input type="submit" value="Check">
</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$character = $_POST["characterInput"];

if (ctype_alpha($character)) {
echo "$character is an alphabet.";
} elseif (ctype_digit($character)) {
echo "$character is a digit.";
} else {
echo "$character is a special character.";
}
}

?>

</body>
</html>

Output:-
4. Write a PHP program to count total number of notes in given amount.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Count Notes</title>
</head>
<body>

<form method="post" action="">


<label for="amountInput">Enter the amount:</label>
<input type="number" id="amountInput" name="amountInput"
min="0">
<input type="submit" value="Count Notes">
</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$amount = $_POST["amountInput"];

$notes = [2000, 500, 200, 100, 50, 20, 10, 5, 2, 1];


$count = 0;

foreach ($notes as $note) {


$count += intval($amount / $note);
$amount %= $note;
}
echo "Total number of notes for the given amount is: $count";
}

?>

</body>
</html>

Output:-
5. Write a PHP code to check the Given Number is Palindrome or Not
using for Loop
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Palindrome Checker</title>
</head>
<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = $_POST["numberInput"];

$originalNumber = $number;
$reverseNumber = 0;

for ($temp = $number; $temp > 0; $temp = intval($temp / 10)) {


$reverseNumber = $reverseNumber * 10 + $temp % 10;
}

if ($originalNumber == $reverseNumber) {
echo "$originalNumber is a palindrome.";
} else {
echo "$originalNumber is not a palindrome.";
}
}
?>

<form method="post" action="">


<label for="numberInput">Enter a number:</label>
<input type="number" id="numberInput" name="numberInput"
min="0">
<input type="submit" value="Check Palindrome">
</form>

</body>
</html>

OUTPUT:-
6. Write a program to reverse a given indexed array. And print all the
elements using foreach loop

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Reverse Array</title>
</head>
<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$userArray = $_POST["arrayInput"];

// Assuming the user provides a comma-separated list of numbers, let's convert


it into an array
$array = explode(',', $userArray);

$reversedArray = array_reverse($array);

foreach ($reversedArray as $element) {


echo $element . " ";
}
}
?>

<form method="post" action="">


<label for="arrayInput">Enter numbers separated by commas:</label>
<input type="text" id="arrayInput" name="arrayInput">
<input type="submit" value="Reverse Array">
</form>

</body>
</html>
OUTPUT:-
7.Write a program to enter a number from the user using web form and
check it is prime or not using user defined functions.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Prime Number Checker</title>

</head>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$userNumber = $_POST["numberInput"];

function isPrime($number) {

if ($number <= 1) {
return false;

for ($i = 2; $i <= sqrt($number); $i++) {

if ($number % $i == 0) {

return false;

return true;

if (isPrime($userNumber)) {

echo "$userNumber is a prime number.";

} else {

echo "$userNumber is not a prime number.";

}
?>

<form method="post" action="">

<label for="numberInput">Enter a number:</label>

<input type="number" id="numberInput" name="numberInput" min="0">

<input type="submit" value="Check Prime">

</form>

</body>

</html>

OUTPUT:-
8.Write a program to print the addition of two 3*3 matrix and print in
tabular format.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Matrix Addition</title>

</head>

<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$userMatrix1 = $_POST["matrix1Input"];

$userMatrix2 = $_POST["matrix2Input"];

// Assuming the user provides matrices in a format like: "1,2,3;4,5,6;7,8,9"


$matrix1 = array_map(function ($row) {

return explode(',', $row);

}, explode(';', $userMatrix1));

$matrix2 = array_map(function ($row) {

return explode(',', $row);

}, explode(';', $userMatrix2));

$resultMatrix = [];

for ($i = 0; $i < 3; $i++) {

for ($j = 0; $j < 3; $j++) {

$resultMatrix[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];

// Print the result in tabular format


echo "<table border='1'>";

foreach ($resultMatrix as $row) {

echo "<tr>";

foreach ($row as $element) {

echo "<td>$element</td>";

echo "</tr>";

echo "</table>";

?>

<form method="post" action="">

<label for="matrix1Input">Enter the first matrix:</label>

<input type="text" id="matrix1Input" name="matrix1Input">

<br>
<label for="matrix2Input">Enter the second matrix:</label>

<input type="text" id="matrix2Input" name="matrix2Input">

<br>

<input type="submit" value="Add Matrices">

</form>

</body>

</html>

OUTPUT:-
9. Write a php script to calculate the age of a person enter by Date of
birth date must be pick from the user using html calander.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Age Calculator</title>
</head>
<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$dob = $_POST["dob"];

$today = new DateTime();


$birthdate = new DateTime($dob);
$age = $today->diff($birthdate)->y;

echo "Your age is: $age years.";


}

?>

<form method="post" action="">


<label for="dob">Enter your date of birth:</label>
<input type="date" id="dob" name="dob">
<input type="submit" value="Calculate Age">
</form>
</body>
</html>

OUTPUT:-
10.Write a program to print the addition of Two 3*3 matrix in tabular
format using multidimensional array.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Matrix Addition</title>
</head>
<body>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$matrix1 = $_POST["matrix1Input"];
$matrix2 = $_POST["matrix2Input"];

// Assuming the user provides matrices in a format like:


"1,2,3;4,5,6;7,8,9"
$matrix1 = array_map(function ($row) {
return explode(',', $row);
}, explode(';', $matrix1));

$matrix2 = array_map(function ($row) {


return explode(',', $row);
}, explode(';', $matrix2));

$resultMatrix = [];

for ($i = 0; $i < 3; $i++) {


for ($j = 0; $j < 3; $j++) {
$resultMatrix[$i][$j] = $matrix1[$i][$j] + $matrix2[$i][$j];
}
}

// Print the result in tabular format


echo "<table border='1'>";
foreach ($resultMatrix as $row) {
echo "<tr>";
foreach ($row as $element) {
echo "<td>$element</td>";
}
echo "</tr>";
}
echo "</table>";
}

?>

<form method="post" action="">


<label for="matrix1Input">Enter the first matrix:</label>
<input type="text" id="matrix1Input" name="matrix1Input">
<br>
<label for="matrix2Input">Enter the second matrix:</label>
<input type="text" id="matrix2Input" name="matrix2Input">
<br>
<input type="submit" value="Add Matrices">
</form>
</body>
</html>
Output:-
Exercise 2-

Write a php PHP MySQL Database connectivity program to make a


registration page and perform User registration operation by inserting user
data into database.

CODE:-
Php code

<?php
// Establishing connection to MySQL
$servername = "localhost"; // Change this to your MySQL server name if
different
$username = "root"; // Your MySQL username
$password = ""; // Your MySQL password
$dbname = "test2"; // Your MySQL database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Process form data


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$employee_id = $_POST['employee_id'];
$password = password_hash($_POST['password'],
PASSWORD_DEFAULT); // Hash the password before storing
$name = $_POST['name'];
$mobile_number = $_POST['mobile_number'];
$city = $_POST['city'];

// Prepare and bind the SQL statement


$stmt = $conn->prepare("INSERT INTO employees (employee_id,
password, name, mobile_number, city) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("issss", $employee_id, $password, $name,
$mobile_number, $city);

// Execute the prepared statement


if ($stmt->execute()) {
echo "Registration successful!";
} else {
echo "Error: " . $stmt->error;
}

// Close statement and connection


$stmt->close();
$conn->close();
}
?>
HTML CODE:-
<!DOCTYPE html>
<html>
<head>
<title>Employee Registration</title>
</head>
<body>
<h2>Employee Registration</h2>
<form method="post" action="register_process.php">
<label>Employee ID:</label><br>
<input type="text" name="employee_id"><br><br>

<label>Password:</label><br>
<input type="password" name="password"><br><br>

<label>Name:</label><br>
<input type="text" name="name"><br><br>

<label>Mobile Number:</label><br>
<input type="text" name="mobile_number"><br><br>

<label>City:</label><br>
<input type="text" name="city"><br><br>

<input type="submit" value="Register">


</form>
</body>
</html>

OUTPUT:-
Excercise3
Write a PHP Program will showcase how cookie will work. in this
program first php server page will collect data from user and
create cookies so that second php server page can access the data
by using cookies stored in client browser..

Page1.php
<!DOCTYPE html>
<html>
<head>
<title>Cookie Demo - Page 1</title>
</head>
<body>
<h2>Enter Your Information</h2>
<form method="post" action="page2.php">
<label>Name:</label><br>
<input type="text" name="username"><br><br>

<label>Email:</label><br>
<input type="email" name="useremail"><br><br>

<input type="submit" value="Submit">


</form>
</body>
</html>
Page2.php
<!DOCTYPE html>
<html>
<head>
<title>Cookie Demo - Page 2</title>
</head>
<body>
<h2>Displaying Data</h2>
<?php
// Check if the cookies are set
if (isset($_COOKIE['username']) && isset($_COOKIE['useremail'])) {
$username = $_COOKIE['username'];
$useremail = $_COOKIE['useremail'];

echo "Name: $username <br>";


echo "Email: $useremail <br>";
} else {
echo "No data found!";
}
?>
</body>
</html>
Coockies handle.php
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST['username'];
$useremail = $_POST['useremail'];

// Set cookies with user data


setcookie('username', $username, time() + (86400 * 30), "/"); // Cookie set for
30 days
setcookie('useremail', $useremail, time() + (86400 * 30), "/"); // Cookie set for
30 days

// Redirect to page2.php
header("Location: page2.php");
exit();
}
?>

OUTPUT:-
Exercise -4

Design a Login and Logout page for session handling using MYSQL
Database connectivity with following details-
1. Check the login details from database
2. Print User Name on next page when successful login.
3. Design a label for Logout, and perform logout operation on
click.

CODE:-

INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login</h2>
<form method="post" action="login.php">
<label>Username:</label><br>
<input type="text" name="username"><br><br>

<label>Password:</label><br>
<input type="password" name="password"><br><br>

<input type="submit" value="Login">


</form>
</body>
</html>
LOGIN.PHP

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tt";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$username = $_POST['username'];
$password = $_POST['password'];

// Query to check login credentials


$sql = "SELECT * FROM users WHERE username='$username' AND
password='$password'";
$result = $conn->query($sql);

if ($result->num_rows == 1) {
// Valid login, set session variables
$_SESSION['username'] = $username;
header("Location: profile.php");
} else {
echo "Invalid username or password";
}

$conn->close();
}
?>
LOGOUT.PHP
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");
exit();
?>

Profile.PHP

<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Profile Page</title>
</head>
<body>
<h2>Welcome, <?php echo $_SESSION['username']; ?></h2>
<a href="logout.php">Logout</a>
</body>
</html>

OUTPUT:-
Exercise 5
• Create a php PHP with MySQL Database connectivity program to make
a doctor search page and perform doctor search according to specialty
chosen by user and show doctors data in tabular format on next page
CODE

DOCTOR_SEARCH.PHP

<!DOCTYPE html>
<html>
<head>
<title>Doctor Search</title>
</head>
<body>
<h2>Find a Doctor by Specialty</h2>
<form method="post" action="search_result.php">
<label>Select Specialty:</label>
<select name="specialty">
<option value="Cardiologist">Cardiologist</option>
<option value="Dermatologist">Dermatologist</option>
<option value="Orthopedist">Orthopedist</option>
<!-- Add more specialties as needed -->
</select>
<input type="submit" value="Search">
</form>
</body>
</html>
Search_result.php
<!DOCTYPE html>
<html>
<head>
<title>Doctor Search Results</title>
</head>
<body>
<h2>Doctor Search Results</h2>
<?php
// Database connection details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tt";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Process form data


if ($_SERVER["REQUEST_METHOD"] == "POST") {
$specialty = $_POST['specialty'];
// Query doctors based on the selected specialty
$sql = "SELECT * FROM doctors WHERE specialty = '$specialty'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// Display doctors in tabular format
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Specialty</th>
<th>Address</th>
<!-- Add more columns as needed -->
</tr>";

while ($row = $result->fetch_assoc()) {


echo "<tr>
<td>".$row['name']."</td>
<td>".$row['specialty']."</td>
<td>".$row['address']."</td>
<!-- Display other details here -->
</tr>";
}
echo "</table>";
} else {
echo "No doctors found for this specialty.";
}
}
$conn->close();
?>
</body>
</html>

OUTPUT:-

You might also like