Master of Php Record 2025doc NEW (1)
Master of Php Record 2025doc NEW (1)
SULUR-COIMBATORE-641402
Reg. No
Name
Class III-BCOM(CA)-
SCHOOL OF COMMERCE
________________________ ____________________
---------------------------- -----------------------------
Internal Examiner External Examiner
CONTENTS
6 Number tester
7 Date tester
Array of Task list manager
8
application
Task list manager application using
9
sessions
Product list manager application
10
using objects and methods
Product list manager application
11
using validation
12 Guitor shop application using PDO
SELECT,INSERT,UPDATE,DELETE in
13
mysqli Database
Create a PHP program for Get input and Display output using forms control
ALGORITHM:
STEP 5 : Create the form action and finish the embed php code with save as file.php extension.
STEP 6 : Use text boxes, password boxes, radio buttons, check boxes, drop-down lists, list
boxes, and text areas to get input from the user.
STEP 7 : Use hidden fields to pass data to the web application when a form is submitted.
STEP 8 : Use the htmlspecialchars and nl2br functions to display user entries the way you want
them displayed.
STEP 9 : Use echo statements to display data in a web page.
STEP 10 : Finish the Coding Function.
STEP 11 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 12 : Stop The APACHE & MYSQL And Close The XAMPP.
1.Get Input and Display Output Using Forms
<html>
<head>
<title>Account Sign Up</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
<h1>Account Sign Up</h1>
<form action="new.php" method="post">
<fieldset>
<legend>Account Information</legend>
<label>E-Mail:</label>
<input type="text" name="email" value="" class="textbox"/>
<br />
<label>Password:</label>
<input type="password" name="password" value="" class="textbox"/>
<br />
<label>Phone Number:</label>
<input type="text" name="phone" value="" class="textbox"/>
</fieldset>
<fieldset>
<legend>Settings</legend>
<p>How did you hear about us?</p>
<input type="radio" name="heard_from" value="Search Engine" />
Search engine<br />
<input type="radio" name="heard_from" value="Friend" />
Word of mouth<br />
<input type=radio name="heard_from" value="Other" />
Other<br />
<p>Would you like to receive announcements about new products
and special offers?</p>
<input type="checkbox" name="wants_updates"/>YES, I'd like to receive
information about new products and special offers.<br />
<p>Contact via:</p>
<select name="contact_via">
<option value="email">Email</option>
<option value="text">Text Message</option>
<option value="phone">Phone</option>
</select>
<p>Comments:</p>
<textarea name="comments" rows="4" cols="50"></textarea>
</fieldset>
<input type="submit" value="Submit" />
</form>
<br />
</div>
</body>
</html>
new.php file
<?php
$email = $_POST['email'];
$password = $_POST['password'];
$phone = $_POST['phone'];
if (isset($_POST['heard_from'])) {
$heard_from = $_POST['heard_from'];
} else {
$heard_from = 'Unknown';
}
if (isset($_POST['wants_updates'])) {
$wants_updates = 'Yes';
} else {
$wants_updates = 'No';
}
$contact_via = $_POST['contact_via'];
$comments = $_POST['comments'];
$comments = htmlspecialchars($comments);
$comments = nl2br($comments, false);
?>
<html>
<head>
<title>Account Information</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
<h1>Account Information</h1>
<label>Email Address:</label>
<span><?php echo htmlspecialchars($email); ?></span><br />
<label>Password:</label>
<span><?php echo htmlspecialchars($password); ?></span><br />
<label>Phone Number:</label>
<span><?php echo htmlspecialchars($phone); ?></span><br />
<label>Heard From:</label>
<span><?php echo $heard_from; ?></span><br />
<label>Send Updates:</label>
<span><?php echo $wants_updates; ?></span><br />
<label>Contact Via:</label>
<span><?php echo $contact_via; ?></span><br /><br />
<span>Comments:</span><br />
<span><?php echo $comments; ?></span><br />
<p> </p>
</div>
</body>
</html>
Output:
Result:
2. Future Value Calculation using Drop-Down List
AIM:
Develop a PHP program for Enhance the Future value application using drop-down list.
ALGORITHM:
STEP 5 : Create the form action and finish the embed php code with save as file.php extension.
STEP 6 : Use text boxes, drop-down lists to get input from the user.
STEP 7 : create a form with fields for investment amount, annual interest rate, number of years,
and a drop-down list for compounding frequency.
STEP 8: The compounding frequency is used to calculate the future value using the formula for
compound interest
STEP 8 : Use echo statements to display data in a web page.
STEP 9 : Finish the Coding Function.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
2. Future value Calculation using Drop-Down List
<html>
<head>
<title>Future Value Calculator</title>
</head>
<body>
<h2>Future Value Calculator</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="investment">Investment Amount:</label>
<input type="text" name="investment" required><br>
<label for="rate">Annual Interest Rate:</label>
<input type="text" name="rate" required><br>
<label for="years">Number of Years:</label>
<input type="text" name="years" required><br>
<label for="compounding">Compounding Frequency:</label>
<select name="compounding">
<option value="1">Annually</option>
<option value="2">Semi-annually</option>
<option value="4">Quarterly</option>
<option value="12">Monthly</option>
</select><br>
<input type="submit" value="Calculate">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$investment = $_POST["investment"];
$rate = $_POST["rate"] / 100; // convert percentage to decimal
$years = $_POST["years"];
$compounding = $_POST["compounding"];
// Calculate future value
$futureValue = $investment * pow((1 + $rate / $compounding), $years * $compounding);
// Display the result
echo "<h3>Future Value:</h3>";
echo "<p>The future value of your investment is $" . number_format($futureValue, 2) . "</p>";
}
?>
</body>
</html>
Output:
Result:
3. Invoice Total Application using if, else statement
AIM:
Create a PHP program for invoice total application using if, Else statement.
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : The calculateTotal function computes the total amount based on the given
quantity and price.
STEP 7 : The applyDiscount function checks if the total amount exceeds a certain threshold
and applies a discount accordingly.
STEP 8 : The sample data includes the quantity and price per item, and the program calculates
the total amount without discount and the final amount after applying any applicable
discount.
STEP 9 : Use echo statements to display data in a web page.
STEP 10 : Finish The Coding Function.
STEP 11 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
File To View An Output In Parent Directory.
STEP 12 : Stop The APACHE & MYSQL And Close The XAMPP.
3. Invoice total Application using if, else statements
<?php
// Function to calculate total amount
function calculateTotal($quantity, $price)
{
return $quantity * $price;
}
// Sample data
$itemQuantity = 5;
$itemPrice = 250;
// Display results
echo "Quantity: $itemQuantity\n";
echo "Price per item: $itemPrice\n";
echo "Total amount without discount: $totalAmount\n";
echo "Final amount after discount: $finalAmount\n";
?>
Output:
Result:
4. Score application using switch case statement
AIM:
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : The calculateGrade function takes a numeric score as an argument and uses a switch
statement to determine the corresponding grade.
STEP 7 : Each case in the switch statement checks if the score falls within a specific range and
returns the corresponding grade.
STEP 8 : The default case is used to handle situations where the input score is not within any of
the specified ranges.
STEP 9 : Use echo statements to display data in a web page.
STEP 10 : Finish The Coding Function.
STEP 11 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 12 : Stop The APACHE & MYSQL And Close The XAMPP.
4. Score application using switch case statement
<?php
function calculateGrade($score)
{
switch (true)
{
case ($score >= 90 && $score <= 100):
return 'A';
case ($score >= 80 && $score < 90):
return 'B';
case ($score >= 70 && $score < 80):
return 'C';
case ($score >= 60 && $score < 70):
return 'D';
case ($score >= 0 && $score < 60):
return 'F';
default:
return 'Invalid Score';
}
}
// Example usage
$score = 85;
$grade = calculateGrade($score);
echo "Score: $score\n";
echo "Grade: $grade\n";
?>
Output:
Result:
5. String tester
AIM:
ALGORITHM:
STEP 5 : Create the form action and finish the embed php code with save as file.php extension.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
5. String tester
<html>
<head>
<title>String Tester</title>
</head>
<body>
<?php
function isPalindrome($str)
{
$str = strtolower(str_replace(' ', '', $str));
return $str == strrev($str);
}
function reverseString($str)
{
return strrev($str);
}
function hasSubstring($str, $substring)
{
return strpos($str, $substring) !== false;
}
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$inputString = $_POST["inputString"];
$substring = $_POST["substring"];
echo "<h2>Results:</h2>";
echo "<p>Input String: $inputString</p>";
if (isPalindrome($inputString))
{
echo "<p>The string is a palindrome.</p>";
}
else
{
echo "<p>The string is not a palindrome.</p>";
}
echo "<p>Reversed String: " . reverseString($inputString) . "</p>";
if (hasSubstring($inputString, $substring))
{
echo "<p>The string contains the substring '$substring'.</p>";
}
else
{
echo "<p>The string does not contain the substring '$substring'.</p>";
}
}
?>
Output:
Result:
6. Number tester
AIM:
ALGORITHM:
STEP 5 : Create the form action and finish the embed php code with save as file.php extension.
STEP 6 : Use any of the functions and techniques that are presented in this program to work
with numbers.
STEP 7 : Get the number from the form and Validate if the input is a number
STEP 8 : Check if the number is even or odd
STEP 9 : Finish The Coding Function.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
6. Number Tester
<html>
<head>
<title>Number Tester</title>
</head>
<body>
<h2>Number Tester</h2>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
// Get the number from the form
$number = $_POST["number"];
// Validate if the input is a number
if (is_numeric($number))
{
// Check if the number is even or odd
$result = ($number % 2 == 0) ? "Even" : "Odd";
echo "<p>The number $number is $result.</p>";
} else
{
echo "<p>Please enter a valid number.</p>";
}
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Enter a number: <input type="text" name="number">
<input type="submit" value="Test">
</form>
</body>
</html>
Output:
Result:
7. Date tester
AIM:
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : Use a function isValidDate to check if a given date is in the 'YYYY-MM-DD' format
and is a valid date.
STEP 7 : The HTML part includes a form with an input field for entering the date. the PHP code
processes the input, validates the date.
STEP 8 : Use echo statements to display data in a web page.
STEP 9 : Finish The Coding Function.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
7. Date tester
<?php
function isValidDate($date)
{
$dateObj = DateTime::createFromFormat('Y-m-d', $date);
return $dateObj && $dateObj->format('Y-m-d') === $date;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$inputDate = $_POST['input_date'];
if (!empty($inputDate))
{
if (isValidDate($inputDate))
{
$result = "The date '$inputDate' is valid.";
}
else
{
$result = "Invalid date format. Please use 'YYYY-MM-DD'.";
}
}
else
{
$result = "Please enter a date.";
}
}
?>
<html>
<head>
<title>Date Tester</title>
</head>
<body>
<h2>Date Tester</h2>
Output:
Result:
8. Array of task list manager application
AIM:
Construct a PHP program using array of the Task List Manager Application.
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : Use an array $tasks to store tasks, and functions addTask, viewTasks, and
markTaskCompleted to perform different actions on the task list.
STEP 7 : Use echo statements to display data in a web page.
STEP 8 : Finish The Coding Function.
STEP 9 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 10 : Stop The APACHE & MYSQL And Close The XAMPP.
8. Array of task list manager application
<?php
$tasks = []; // Array to store tasks
// Function to add a task to the array
function addTask($task)
{
global $tasks;
$tasks[] = $task;
}
// Function to view tasks
function viewTasks()
{
global $tasks;
echo "Tasks:\n";
foreach ($tasks as $index => $task)
{
$status = $task['completed'] ? 'Completed' : 'Incomplete';
echo ($index + 1) . ". {$task['title']} - $status\n";
}
echo "\n";
}
// Function to mark a task as completed
function markTaskCompleted($index)
{
global $tasks;
if (isset($tasks[$index]))
{
$tasks[$index]['completed'] = true;
echo "Task marked as completed.\n\n";
}
else
{
echo "Invalid task index.\n\n";
}
}
// Example usage:
// Add tasks
addTask(['title' => 'Task 1', 'completed' => false]);
addTask(['title' => 'Task 2', 'completed' => false]);
// View tasks
viewTasks();
Result:
9. Task list manager application using sessions
AIM:
Develop a PHP program for Task List Manager Application using sessions.
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : The tasks are stored in the session variable $_SESSION['tasks'], and you can add
or remove tasks using the provided form.
STEP 7 : The tasks are displayed in an unordered list, and each task has a delete button
associated with it. When you click the delete button, the corresponding task is
removed from the list.
STEP 8 : Use echo statements to display data in a web page.
STEP 9 : Finish The Coding Function.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
9. Task list manager application using session
<?php
// Start the session
session_start();
// Check if the tasks array is already set in the session, if not, initialize it
if (!isset($_SESSION['tasks']))
{
$_SESSION['tasks'] = array();
}
// Function to add a task to the task list
function addTask($task)
{
$_SESSION['tasks'][] = $task;
}
// Function to remove a task from the task list
function removeTask($index)
{
if (isset($_SESSION['tasks'][$index]))
{
unset($_SESSION['tasks'][$index]);
}
}
// Check if the form is submitted to add a new task
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
if (isset($_POST['task']) && !empty($_POST['task']))
{
$newTask = htmlspecialchars($_POST['task']);
addTask($newTask);
}
elseif (isset($_POST['delete']) && !empty($_POST['delete']))
{
$indexToDelete = (int)$_POST['delete'];
removeTask($indexToDelete);
}
}
?>
<html>
<head>
<title>Task List Manager</title>
</head>
<body>
<h2>Task List</h2>
<ul>
<?php
// Display the list of tasks
foreach ($_SESSION['tasks'] as $index => $task)
{
echo "<li>{$task} <form method='post'><input type='hidden' name='delete'
value='{$index}'><button type='submit'>Delete</button></form></li>";
}
?>
</ul>
<h2>Add a Task</h2>
<form method="post">
<label for="task">Task:</label>
<input type="text" id="task" name="task" required>
<button type="submit">Add Task</button>
</form>
</body>
</html>
Output:
Result:
10.Product list manager application using objects and methods
AIM:
Create a PHP program modifies the Product Manager Application using objects and methods.
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : The Product class represents a product with properties like id, name, and price. The
ProductManager class is responsible for managing products, including adding,
viewing, and removing products.
STEP 7 : The example usage section demonstrates creating product instances, adding them to
the product manager, viewing products, removing a product, and viewing the updated
product list.
STEP 8 : Use echo statements to display data in a web page.
STEP 9 : Finish The Coding Function.
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
10. Product Manager Application Using Objects and methods
<?php
// Product class representing a product with properties and methods
class Product
{
private $name;
private $price;
public function __construct($name, $price)
{
$this->name = $name;
$this->price = $price;
}
public function getName()
{
return $this->name;
}
public function getPrice()
{
return $this->price;
}
}
// ProductManager class for managing products
class ProductManager
{
private $products = [];
public function addProduct(Product $product)
{
$this->products[] = $product;
}
public function displayProducts()
{
echo "Product List:\n";
foreach ($this->products as $product)
{
echo "Name: " . $product->getName() . ", Price: $" . $product->getPrice() . "\n";
}
}
}
// Creating product objects
$product1 = new Product("Laptop", 999.99);
$product2 = new Product("Smartphone", 499.99);
Result:
11.Product list manager application using validation
AIM:
Develop a PHP program modify the Product Manager Application using validation
techniques.
ALGORITHM:
STEP 5 : Create the form action and finish the php code with save as file.php extension.
STEP 6 : The addProduct method checks if the input parameters are valid before adding a
product to the list.
STEP 7 : The validation includes checking if the name is not empty and if both the price and
quantity are numeric.
STEP 8 : If the validation fails, an exception is thrown, and an error message is displayed.
STEP 9 : Use echo statements to display data in a web page.
STEP 10 : Finish The Coding Function.
STEP 11 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
STEP 12 : Stop The APACHE & MYSQL And Close The XAMPP.
11. Product manager application using validation
<?php
class ProductManager
{
private $products = [];
public function addProduct($name, $price, $quantity)
{
// Validate input
if (empty($name) || !is_numeric($price) || !is_numeric($quantity))
{
throw new Exception("Invalid input. Please provide valid information.");
}
// Add the product
$product = [
'name' => $name,
'price' => $price,
'quantity' => $quantity,
];
$this->products[] = $product;
echo "Product added successfully!\n";
}
public function listProducts()
{
echo "Product List:\n";
foreach ($this->products as $product)
{
echo "Name: {$product['name']}, Price: {$product['price']}, Quantity: {$product['quantity']}\n";
}
}
}
// Example usage
$productManager = new ProductManager();
try
{
$productManager->addProduct("Product 1", 25.99, 10);
$productManager->addProduct("Product 2", "InvalidPrice", 5); // This will throw an exception
$productManager->addProduct("", 19.99, 8); // This will throw an exception
$productManager->listProducts();
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage() . "\n";
}
?>
Output:
Result:
12.Guitar Shop Application Using PDO
AIM:
ALGORITHM:
USE guitar_shop;
Model:
Type:
Price:
Back to Home
3.Guitar Shop
Available Guitars
reabock 24 - $2000.00
5.Search Results:
Back to Home
Guitar Shop
Available Guitars
reabock 24 - $2000.00
royal 23-$1500.00
ALGORITHM:
Name:
Email:
Age:
Name:
Email: [email protected]
Age: 33
Result:
14.Product and Category model in Mysqli
AIM:
ALGORITHM:
Result:
15.Php program using method of Mysqli Class
AIM:
ALGORITHM:
name VARCHAR(100),
email VARCHAR(100)
db.php
<?php
// Database connection parameters
$servername = "localhost";
$username = "root"; // Change to your database username
$password = ""; // Change to your database password
$dbname = "model db"; // Change to your database name
// Create a new instance of the mysqli class
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
} else {
echo "0 results";
}
}
Current Users:
Updated Users:
RESULT: