0% found this document useful (0 votes)
66 views12 pages

Wta Module 5

The document contains 8 programming exercises in PHP: 1. A program to check student grade based on marks percentage. 2. A recursive function to calculate factorial of a number. 3. A program using nested for loops to create an 8x8 chess board table. 4. A script to display contents of enumerated and associative arrays using foreach loops. 5. Code demonstrating how to post data from HTML form to PHP page. 6. A script to search and display employee data from a database table. 7. A form to accept employee data and insert into database table. 8. A function to return an associative array which is displayed in a table in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views12 pages

Wta Module 5

The document contains 8 programming exercises in PHP: 1. A program to check student grade based on marks percentage. 2. A recursive function to calculate factorial of a number. 3. A program using nested for loops to create an 8x8 chess board table. 4. A script to display contents of enumerated and associative arrays using foreach loops. 5. Code demonstrating how to post data from HTML form to PHP page. 6. A script to search and display employee data from a database table. 7. A form to accept employee data and insert into database table. 8. A function to return an associative array which is displayed in a table in
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MODULE 5

1.Write a program to check student grade based on marks


Conditions:
● If marks are 60% or more, grade will be First Division.
● If marks between 45% to 59%, grade will be Second Division.
● If marks between 33% to 44%, grade will be Third Division.
● If marks are less than 33%, student will be Fail.

<?php
$marks = 40;

if ($marks>=60)
{
$grade = "First Division";
}
else if($marks>=45)
{
$grade = "Second Division";
}
else if($marks>=33)
{
$grade = "Third Division";
}
else
{
$grade = "Fail";
}

echo "Student grade: $grade";


?>

OUTPUT:
Student grade: Third Division

2.Write Factorial program in PHP using recursive function

<?php
function factorial($number) {
if ($number < 2) {
return 1;
} else {
return ($number * factorial($number-1));
}
}

echo factorial(4);
?>

OUTPUT:
24

3.Write a program to create Chess board in PHP using for loop

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Chess Board using Nested For Loop</h3>
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<!-- cell 270px wide (8 columns x 60px) -->
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>
</body>
</html>

OUTPUT:

4.Write a PHP script to create enumerated array and associative array and display
the contents using foreach loop.

<?php
$flexiple = array("Hire", "top", "freelance", "developers");

foreach ($flexiple as $value) {


echo "$value <br>";
}
$freelancer = array(
"name" => "Eric",
"email" => "[email protected]",
"age" => 22,
"gender" => "male"
);
foreach($freelancer as $key => $value) {
echo $key . ": " . $value . "<br>";
}
?>

OUTPUT:
Hire
top
freelance
developers
name: Eric
email: [email protected]
age: 22
gender: male

5.Demonstrate how to post data from HTML to PHP using php code.

<!DOCTYPE HTML>
<html>
<body>

<form action="M55a.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

In M5a.html

Welcome sanjana
Your email address is: [email protected]

6.Write a PHP script to display the contents of an employee table(empno, ename,


esal) based on empno accepted through html document.
//form3.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form 3</title>
<style>
table,th,td{
border: 1px solid black;
}
th,td{
padding: 10px;
}
</style>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="POST">
<h1>Search Employee form</h1>
<label>Employee Number:</label>
<input type="text" name="number" size="10" placeholder="Enter Number"><br><br>
<input type="submit" name="save" value="Submit">
<input type="reset" name="reset" value='Reset'>
</form>
</body>
</html>
<?php
$host="localhost";
$username="root";
$password="";
$dbname="sample";
$conn = new
mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
die("connection failed" .$conn->connect_error);
}
else{
if(isset($_POST["save"])){
$number=$_POST["number"];
$sql="SELECT * FROM employee WHERE number=$number";
$result = $conn->query($sql);
if($result->num_rows > 0){
$row=$result->fetch_assoc();
echo "<br><br><table><tr><th>Employee Number</th><td>".$row['number'].
"</td></tr><tr><th>Employee Name</th><td>".$row['name']. "</td></tr><tr><th>Employee
Salary</th><td>".$row['salary']. "</td></tr></table>";
}else{
echo "Error : Employee number is invalid";
}
$conn->close();
}
}?
>
7.Write a PHP script to accept employee number, name and salary from HTML
document and display them. also insert the same into employee table.
//form1.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form 1</title>
</head>
<body>
<form action="form1.php" method="POST">
<h1>Insert Employee form</h1>
<label>Number:</label>
<input type="text" name="number" size="10" placeholder="Enter Number"><br><br>
<label >Name:</label>
<input type="text" name="name" placeholder="Enter Name"> <br><br>
<label >Salary:</label>
<input type="text" name="salary" size="10" placeholder="Enter Salary"><br><br>
<input type="submit" name="save" value="Submit">
<input type="reset" name="reset" value='Reset'>
</form>
</body>
</html>
//form1.php
<!DOCTYPE html>
<html>
<head>
<title>Form 1</title>
<style>
table,th,td{
border: 1px solid black;
}
th,td{
padding: 10px;
}
</style>
</head>
<body>
<?php
$host="localhost";
$username="root";
$password="";
$dbname="sample";
$conn = new mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
die("connection failed" .$conn->connect_error);
}
else{
if(isset($_POST["save"])){
$number=$_POST["number"];
$name=$_POST["name"];
$salary=$_POST["salary"];
$sql="INSERT INTO employee(number,name,salary) values
('$number','$name','$salary')";
if($conn->query($sql)){
echo "<table><tr><th>Employee Number</th><td>".$number.
"</td></tr><tr><th>Employee Name</th><td>".$name. "</td></tr><tr><th>Employee
Salary</th><td>".$salary. "</td></tr></table>";
}else{
echo "Error" .$sql. "<br>" .$conn->error;
}
$conn->close();
}
}
?>
</body>
</html>
8 Write a PHP program to create user defined function which returns
associativearray. Display the contents in main program in table format.
//program5.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Associative Array</title>
<style type="text/css">
table,th,td{
border: 1px solid black;
} th,td{
} padding: 20px;
</style>
</head>
<body>
<?php

function associative(){
$arr=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
echo "<h2>Associative array inside function<br></h2>";
foreach($arr as $x=>$y){
echo $x." => ".$y.", ";
}
echo "<br>";
return $arr;
}

$arr1=associative();
echo "<h2>Associative array in main<br></h2>";
echo "<table><tr><th>Key</th><th>Value</th></tr>";
foreach($arr1 as $x=>$y){
echo "<tr><td>".$x."</td><td>".$y."</td></tr>";
}
echo "</table>"
?>
</body>
</html>

You might also like