PHP Record Final
PHP Record Final
AIM:
ALGORITHM:
STEP 5 : Create the form action and finish the HTML code with save as file.HTML extension.
STEP 6 : Open the PHP tag <?php and create php code
STEP 8 And create the empedded php code for display process
STEP 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
File To View An Output In Parent Directory.
STEP 11 : Stop The APACHE & MYSQL And Close The XAMPP.
1.Product Discount Application (HTML FILE)
<html >
<head>
<title>Product Discount Calculator</title>
</head>
<body>
<div id="content">
<h1>Product Discount Calculator</h1>
<form action="p1.php" method="post">
<div id="data">
<label>Product Description:</label>
<input type="text" name="product_description"/>
<br />
<label>List Price:</label>
<input type="text" name="list_price"/><br />
<label>Discount Percent:</label>
<input type="text" name="discount_percent"/>%<br />
</div>
<div id="buttons">
<label> </label>
<input type="submit" value="Calculate Discount" />
<br />
</div>
</form>
</div>
</body>
</html>
1.Product Discount Application (PHP FILE)
<?php
$product_description = $_POST['product_description'];
$list_price = $_POST['list_price'];
$discount_percent = $_POST['discount_percent'];
$discount = $list_price * $discount_percent * .01;
$discount_price = $list_price - $discount;
$list_price_formatted =
"$".number_format($list_price, 2);
$discount_percent_formatted = $discount_percent."%";
$discount_formatted = "$".number_format($discount, 2);
$discount_price_formatted =
"$".number_format($discount_price, 2);
?>
<html >
<head>
<title>Product Discount Calculator</title>
<link rel="stylesheet" type="text/css" href="main.css"/>
</head>
<body>
<div id="content">
<h1>Product Discount Calculator</h1>
<label>Product Description:</label>
<span><?php echo $product_description; ?>
</span><br />
<label>List Price:</label>
<span><?php echo $list_price_formatted; ?>
</span><br />
<label>Standard Discount:</label>
<span><?php echo $discount_percent_formatted; ?>
</span><br />
<label>Discount Amount:</label>
<span><?php echo $discount_formatted; ?>
</span><br />
<label>Discount Price:</label>
<span><?php echo $discount_price_formatted; ?>
</span><br />
</div></body></html>
OUTPUT:
Calculate Discount
RESULT:
The Program Will Be Executed, The Output Is Displayed.
2.Future Value Calculation
AIM:
Enhance the Future value Application to display the result on same page
ALGORITHM:
Step 5 : Create the form action and finish the HTML code with save as file.HTML extension.
Step 6 : Open the PHP tag <?php and create php code
Step 7 : Declare the variable $investment amount, $interest rate, $future value
Step 8 : And create the empedded php code for display process
Step 10 : To Run a Program Open Chrome and Type localhost/Folder Name. And Select The
File To View An Output In Parent Directory.
Step 11 : Stop The APACHE & MYSQL And Close The XAMPP.
2.Future Value Calculation (HTML FILE)
<html>
<head>
<title>Future Value Calculator</title>
</head>
<body>
<div id="content">
<h1>Future Value Calculator</h1>
<form action="p2.php" method="post">
<div id="data">
<label>Investment Amount:</label>
<input type="text" name="investment" value="<?php echo $investment; ?>"/><br />
<label>Yearly Interest Rate:</label>
<input type="text" name="interest_rate" value="<?php echo $interest_rate; ?>"/><br />
<label>Number of Years:</label>
<input type="text" name="years" value="<?php echo $years; ?>"/><br />
</div>
<div id="buttons">
<label> </label>
<input type="submit" value="Calculate"/><br />
</div>
</form>
</div>
</body>
</html>
2.Future value Calculation (PHP FILE)
<?php
// get the data from the form
$investment = $_POST['investment'];
$interest_rate = $_POST['interest_rate'];
$years = $_POST['years'];
// validate investment entry
if ( empty($investment) ) {
$error_message = 'Investment is a required field.'; }
else if ( !is_numeric($investment) ) {
$error_message = 'Investment must be a valid number.'; }
else if ( $investment <= 0 ) {
$error_message = 'Investment must be greater than zero.'; }
// validate interest rate entry
else if ( empty($interest_rate) ) {
$error_message = 'Interest rate is a required field.'; }
else if ( !is_numeric($interest_rate) ) {
$error_message = 'Interest rate must be a valid number.'; }
else if ( $interest_rate <= 0 ) {
$error_message = 'Interest rate must be greater than zero.'; }
// set error message to empty string if no invalid entries
}
// calculate the future value
$future_value = $investment;
for ($i = 1; $i <= $years; $i++) {
$future_value = ($future_value + ($future_value * $interest_rate *.01));
}
// apply currency and percent formatting
$investment_f = '$'.number_format($investment, 2);
$yearly_rate_f = $interest_rate.'%';
$future_value_f = '$'.number_format($future_value, 2);
?>
<html>
<head>
<title>Future Value Calculator</title>
</head>
<body>
<div id="content">
<h1>Future Value Calculator</h1>
<label>Investment Amount:</label>
<span><?php echo $investment_f; ?></span><br />
<label>Yearly Interest Rate:</label>
<span><?php echo $yearly_rate_f; ?></span><br />
<label>Number of Years:</label>
<span><?php echo $years; ?></span><br />
<label>Future Value:</label>
<span><?php echo $future_value_f; ?></span><br />
</div>
</body>
</html>
Output: Future Value Calculator
<?php echo $i
Investment Amount:
<?php echo $i
Yearly Interest Rate:
<?php echo $y
Number of Years:
Calculate
30000
Investment Amount:
2
Yearly Interest Rate:
15
Number of Years:
Calculate
RESULT:
The Program Will Be Executed, The Output Is Displayed.
3.Enhance The Product Manager Application
AIM:
ALGORITHAM:
Step 1: Set Up Your XAMPP Environment
1. Install XAMPP: If you haven't already, download and install XAMPP from the official site.
2. Start XAMPP: Launch the XAMPP Control Panel and start the Apache and MySQL services.
Import SQL File (Optional): If you have a SQL file, you can import it by:
2. Create PHP Files: Create the following files in the product_manager directory.
You can extend the application by adding forms for adding, editing, and deleting products.
Here's a simple way to add a form to insert products:
3.Enhance The Product Manager Application
<?php
require_once('database.php');
// Get category ID
if(!isset($category_id)) {
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
}
<div id="header">
<h1>Product Manager</h1>
</div>
<div id="main">
<h1>Product List</h1>
<div id="sidebar">
<!-- display a list of categories -->
<h2>Categories</h2>
<ul class="nav">
<?php foreach ($categories as $category) : ?>
<li>
<a href="?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="content">
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td class="right"><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post"
id="delete_product_form">
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
<input type="submit" value="Delete" />
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p><a href="add_product_form.php">Add Product</a></p>
</div>
</div>
<div id="footer">
<p>© <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>
</div>
AIM:
ALGORITHAM:
1. Install XAMPP: Download and install XAMPP from the official site.
2. Start XAMPP: Launch the XAMPP Control Panel and start the Apache and MySQL services.
2. Create PHP Files: Create the following files in the product_catalog directory.
Step 6: Additional Features (Optional)You can enhance the application by adding features
like:
Edit and Delete Functions: Create separate pages for editing and deleting products.
Search Functionality: Implement a search feature to filter products by name or description.
4.Enhance Guitar Shop Application
<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
$category_id = $_GET['category_id'];
if (empty($category_id)) {
$category_id = 1;
}
$categories = get_categories();
$category_name = get_category_name($category_id);
$products = get_products_by_category($category_id);
include('product_list.php');
} else if ($action == 'view_product') {
$categories = get_categories();
$product_id = $_GET['product_id'];
$product = get_product($product_id);
include('product_view.php');
}
?>
OUTPUT:
RESULT:
The Program Will Be Executed, The Output Is Displayed.
5.Fetching Category And Guitar Shop Database In Phpmyadmin
AIM:
Write a select query to fetch category and product data of guitar shopdatabase in phpmyadmin
ALGORITHM:
Step5: FROM products: Specifies that we are selecting data from the products table.
Step7:This query will return a list of all products along with their corresponding category
names. You can execute this query in the SQL tab of phpMyAdmin.
5.Fetching Category And Guitar Shop Database In Phpmyadmin
To fetch category and product data from a guitar shop database in phpMyAdmin, you would
typically have two tables: one for categories (categories) and one for products (products).
These tables are usually linked by a foreign key, where the products table has a category_id
that references the id in the categories table.
SQL SELECT query to fetch the category name and product details:
SELECT
categories.category_name,
products.product_name,
products.product_description,
products.price
FROM
products
JOIN
categories
ON
products.category_id = categories.id;
OUTPUT:
Assuming the categories and products tables are populated with data, the result set might look
something like this:
This output shows the category each product belongs to, along with the product name,
description, and price. You can execute this query in the SQL tab of phpMyAdmin to get similar
results based on your actual data.
RESULT:
The Program Will Be Executed, The Output Is Displayed.
6.Data Manipulation On The Product Table
AIM:
Perform Data Manipulation On The Product Table Of Guitar Shop Database In Php
MyAdmin
ALGORITHM:
Action: If there's a mistake in your SQL syntax or a problem executing the query.
Output: phpMyAdmin displays an error message, such as:
OUTPUT:
RESULT:
The Program Will Be Executed, The Output Is Displayed.
AIM:
Create a php program to perform future value calculation using a function with
parameters
ALGORITHM:
Step7 : Finally, it returns the future value, which is displayed using number_format to
xampp/htdocs/newfolder/p7.php
<?php
function future_value($PV, $r, $n)
{
return $FV;
// Example usage:
$PV = 1000;
$r = 0.05;
$n = 10;
echo "The future value of the investment is: $" . number_format($FV, 2);
?>
OUTPUT:
The future value of the investment is: $1,628.89
RESULT:
The Program Will Be Executed, The Output Is Displayed.
8. GET_Array Method Using PHP
AIM:
Create PHP program for using GET Array
ALGORITHM:
Step 1: Start the APACHE & MYSQL server
Step 2 : Open the notepad
Step 3 : Open the php tag use of IF condition statement,
(isset($_GET[&fnmae,$lname,$submit]
Step 4 : Use $_GET method for getting the values
Step 5 : Use echo statement for printing
Step 6 : And declare else statement to specify when IF statement is not true
Step 7 : Close the PHP tag
Step 8 : Type the HTML program and use input tag for first name, last name Email and submit
Step 9 : Save the program write extension of php
Step 10: To run the program open browser LOCALHOST/FOLDER NAME
Step 11 : The output will be displayed
8. Get_Array Method Using Php
<?php
if(isset($_GET['submit']))
{
if($_GET["fname"] == "" || $_GET["lname"] == "" ){
echo "The firstname or lastname can't be empty";
}
else
{
echo "First Name: ". $_GET['fname']."<br/>";
echo "Last Name: ". $_GET['lname']."<br/>";
echo "Email: ". $_GET['email']."<br/>";
echo "Phone: ". $_GET['phone'];
}
}
else
{
?>
<html>
<head>
<title>Use of PHP $_GET</title>
</head>
<body>
<form method="get" action="#">
<table>
<tr><td>
<label for="inputName">Enter your first name:</label>
</td><td>
<input type="text" name="fname" id="fname"><br/>
</td></tr><tr><td>
<label for="inputName">Enter your last name:</label>
</td><td>
<input type="text" name="lname" id="lname"><br/>
</td></tr><tr><td>
<label for="inputName">Enter your email:</label>
</td><td>
<input type="text" name="email" id="email"><br/>
</td></tr><tr><td>
<label for="inputName">Enter your phone:</label>
</td><td>
<input type="text" name="phone" id="phone"><br/>
</td></tr><tr><td>
<input type="submit" name="submit" value="Submit"><br/>
</td><td></td></tr>
</table>
</form>
</body>
</html>
<?php
}
?>
Output:
First Name: santha
Last Name: sorubini
Email: [email protected]
Phone: 9790197054
RESULT:
The Program Will Be Executed, The Output Is Displayed.
9. POST_Array method using PHP
AIM:
ALGORITHM:
STEP-1 Start the APACHE & MYSQL server
STEP-2 Open the notepad
STEP-3 Open the php tag use of IF condition statement, (isset($_POST[&user name;$password;]
STEP-4 Use $_POST method for Passed the values
STEP-5 Use echo statement for printing
STEP-6 And declare else statement to specify when IF statement is not true
STEP-7 Close the PHP tag
STEP-8 Type the HTML program and use input tag for user name, password and submit
STEP-9 Save the program write extension of pho
STEP-10 To run the program open browser LOCALHOST/FOLDER NAME
STEP-11 The output will be displayed
9. POST_Array method using PHP
<?php
if(isset($_POST['submit']))
{
if(trim($_POST["username"]) == "admin" && trim($_POST["pass"]) == "238967" ){
echo "Authenticated User";
}
else
{
echo "Invalid user";
}
}
else
{
?>
<!-- HTML form to take input from the user -->
<html lang="en">
<head>
<title>Use of PHP $_POST</title>
</head>
<body>
<form method="post" action="#">
<table>
<tr><td>
<label for="inputName">Username:</label>
</td><td>
<input type="text" name="username" id="uname"><br/>
</td></tr><tr><td>
<label for="inputName">Password:</label>
</td><td>
<input type="password" name="pass" id="pass"><br/>
</td></tr><tr><td>
<input type="submit" name="submit" value="Submit"><br/>
</td><td></td></tr>
</table>
</form>
</body>
</html>
<?php
}
?>
OUTPUT:
Username: min
Password: ******
Submit
Authenticated User
OUTPUT :
Username: admin
Password: ****
Submit
Invalid user
RESULT:
The Program Will Be Executed, The Output Is Displayed.
10. Relational And Logical Operators
AIM:
ALGORITHM:
. Step1: Start the APACHE & MYSQL server
Step7: Save The Program With .Php Extension In The Local Disk C >> Xampp
>>Htdocs
<html>
<head>Relational and logical operators</head>
<body>
<?php
// Example variables
$a = 10;
$b = 20;
OUTPUT:
Relational and logical operators
a is less than b and b is greater than 15.
RESULT:
The Program Will Be Executed, The Output Is Displayed.
11.Strings In PHP
AIM:
Create the PHP program for Strings
ALGORITHM:
Step8 : Use the 'Echo' statement to view the output of the 3 variables
xampp/htdocs/newfolder/p11.php
<?php
$str1='Hello text multiple line text within single quoted string';
$str2='Using double "quote" directly inside single quoted string';
$str3='Using escape sequences \n in single quoted string';
echo "$str1 <br/> $str2 <br/> $str3";
?>
OUTPUT 1:
Hello text multiple line text within single quoted string
Using double "quote" directly inside single quoted string
Using escape sequences \n in single quoted string
OUTPUT 2:
Hello text multiple line text within single quoted string
Using double "quote" directly inside single quoted string Using escape sequences \n in single
quoted string
RESULT:
The Program Will Be Executed, The Output Is Displayed.
12.Numbers In PHP
AIM:
Create the PHP program for Numbers
ALGORITHM:
xampp/htdocs/newfolder/p12.php
<html>
<body>
<?php
// Check if the variable is numeric
$x = 5985;
var_dump(is_numeric($x));
echo "<br>";
$x = "5985";
var_dump(is_numeric($x));
echo "<br>";
$x = "59.85" + 100;
var_dump(is_numeric($x));
echo "<br>";
$x = "Hello";
var_dump(is_numeric($x));
?>
</body>
</html>
OUTPUT:
bool(true)
bool(true)
bool(true)
bool(false)
RESULT:
The Program Will Be Executed, The Output Is Displayed.
13.Built In Function
AIM:
ALGORITHM:
Step 4 : Start the code with <?php tag and and end with ";".
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.
13.Built In Function
<html>
<body>
<?php
function sum(int $x, int $y)
{
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
</body>
</html>
OUTPUT:
5 + 10 = 15
7 + 13 = 20
2+4=6
RESULT:
The Program Will Be Executed, The Output Is Displayed.
14.W HILE -L OOP IN PHP
AIM
ALGORITHM:
. Step1: Start the APACHE & MYSQL server
Step7: Save the program with .php extension in the local disk c >> xampp >>htdocs
<html>
<body>
<?php
$x = 0;
while($x <= 100)
{
echo "The number is: $x <br>";
$x+=10;
}
?>
</body>
</html>
OUTPUT:
The number is: 0
The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50
The number is: 60
The number is: 70
The number is: 80
The number is: 90
The number is: 100
RESULT:
The Program Will Be Executed, The Output Is Displayed.
15. Array In PHP
AIM:
ALGORITHM:
Step7: Save The Program With .Php Extension In The Local Disk C >> Xampp >>Htdocs
<html>
<body>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++)
{
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++)
{
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
</body>
</html>
OUTPUT:
Row number 0
Volvo
22
18
Row number 1
BMW
15
13
Row number 2
Saab
5
2
Row number 3
Land Rover
17
15
RESULT:
The Program Will Be Executed, The Output Is Displayed.