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

Neeraj Nemiraj 4NM20IS086

Web Lab MSE 2 Questions

1. Execute the following:


a) Change Text Color of the Elements Using Jquery
<!DOCTYPE>
<html>
<head>
<title>Change Text Color</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("p").css("color","cyan")
});
</script>
</head>
<body>
<p>Hello</p>
</body>
</html>

Output :

b) Write a PHP script to generate the following output


1
12
123
1234
12345

<?php

for($i=1;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "$j";
echo " ";
}
echo"<br>";
}

?>

Output :

2. Execute the following:


a) Click event Double click event in jQuery
<!DOCTYPE html>
<html>
<head>
<title>Double Click Event JQuery</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("p").dblclick(function(){
alert("Double clicked");
});
});
</script>
</head>
<body>
<p>Double click me.</p>
</body>
</html>

Output :

b) Write a HTML code that takes name and email as input and display the entered
details using $_GET array.

get.html

<!DOCTYPE html>
<html>
<head>
<title>Get Form</title>
</head>
<body>
<h1>Get Form</h1>
<form action="get.php" method="GET">
Name : <input type="text" name="name"/><br><br>
Email : <input type="email" name="email"/><br><br>
<input type="submit"/>
</form>
</body>
</html>

get.php

<!DOCTYPE html>
<html>
<head>
<title>PHP Form</title>
</head>
<body>
<p>Welcome <?php echo $_GET['name']; ?></p>
<p>Email is <?php echo $_GET['email']; ?></p>
</body>
</html>
Output :

3. Execute the following:


a) Creating a simple show and hide effect in jQuery

<!DOCTYPE>
<html>
<head>
<title>Hide and Show</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$(".showbtn").click(function(){
$("p").show();
});
$(".hidebtn").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<p>This text is shown.</p>
<br>
<button class="showbtn">Show</button>
<button class="hidebtn">Hide</button>
</body>
</html>

Output :

b) Write a HTML code that takes name and email as input and display the entered
details using $_POST array.

post.html

<!DOCTYPE html>
<html>
<head>
<title>Post Form</title>
</head>
<body>
<h1>Post Form</h1>
<form action="post.php" method="POST">
Name : <input type="text" name="name"/><br><br>
Email : <input type="email" name="email"/><br><br>
<input type="submit"/>
</form>
</body>
</html>

post.php

<!DOCTYPE html>
<html>
<head>
<title>Post PHP</title>
</head>
<body>
<p>Welcome <?php echo $_POST['name']; ?></p>
<p>Your email is <?php echo $_POST['email']; ?></p>
</body>
</html>

Output :

4. Execute the following:


a) Write a PHP program to display a digital clock which displays the current time of
the server.

<!DOCTYPE html>
<html>
<head>
<title>Clock</title>
<meta http-equiv="refresh" content="1"/>
</head>
<body>
<h1><?php echo date("h : i : s A"); ?></h1>
</body>
</html>

Output :
b) Write a PHP script that accepts filename, extension, text content and writes into
file.
<!DOCTYPE html>
<html>
<head>
<title>File extentions</title>
</head>
<body>
<form method="post">
<p>File name : <input type="text" name="file"/>
<p>Extention : </p>
<select name="ext">
<option value="txt">txt</option>
<option value="doc">doc</option>
<option value="pdf">pdf</option>
<option value="docx">docx</option>
<option value="odt">odt</option>
</select>
<br><br>
<textarea name="cont"></textarea><br><br>
<input type="submit" value="submit" name="submit"\>
</form>
</body>
</html>

<?php

if(isset($_POST['submit']))
{
$filename = $_POST['file'];
$extention = $_POST['ext'];
$content = $_POST['cont'];

file_put_contents($filename.".".$extention,$content) or die("Unable to open");


}

?>

Output :

5. Execute the following:


a) Write a PHP script to do the following
i) create 2 Arrays
ii) sort Arrays
iii) find common elements from array
iv)remove duplicates from an array

<?php

$arr1 = array(45,23,98,32,44,12,45,23,22,23,86,44);
$arr2 = array(56,75,23,64,23,44,32,64,21,54,23,64);
echo "Array 1 : ";
echo "<br>";
foreach($arr1 as $val){
echo("$val ");
}

echo "<br><br>";
echo "Array 2 : ";
echo "<br>";
$s1=count($arr2);
for($i=0;$i<$s1;$i++){
echo($arr2[$i]);
echo " ";
}

sort($arr1);
sort($arr2);

echo "<br><br>";
echo "Sorted array 1 : ";
echo "<br>";
foreach($arr1 as $val){
echo("$val ");
}

echo "<br><br>";
echo "Sorted array 2 : ";
echo "<br>";
foreach($arr2 as $val){
echo("$val ");
}

$res = array_unique(array_intersect($arr1,$arr2));
echo "<br><br>";
echo "Common elements : ";
echo "<br>";
foreach($res as $val){
echo("$val ");
}

$arr_unique1 = array_unique($arr1);
$arr_unique2 = array_unique($arr2);
echo "<br><br>";
echo "Unique array 1 : ";
echo "<br>";
foreach($arr_unique1 as $val){
echo("$val ");
}

echo "<br><br>";
echo "Unique array 2 : ";
echo "<br>";
foreach($arr_unique2 as $val){
echo("$val ");
}

?>
Output :

b) To Generate Fahrenheit and Celsius table using PHP

<!DOCTYPE html>
<html>
<head>
<title>Temperature Table</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
}
</style>
</head>
<body>
<h1>Temperature Table</h1>
<table>
<tr>
<th colspan="2">Farenheit to Celcius</th>
<th colspan="2">Celcius to Farenheit</th>
</tr>
<tr>
<th>Farenheit</th>
<th>Celcius</th>
<th>Celcius</th>
<th>Farenheit</th>
</tr>
<?php for($i=32;$i<=42;$i++) { ?>
<tr>
<td><?php echo($i); ?></td>
<td><?php echo ((($i - 32)*5)/9); ?></td>
<td><?php echo($i); ?></td>
<td><?php echo (((($i)*9)/5)+32); ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
Output :

6. Execute the following:


a) Write a PHP script to perform file operations
i) display the contents from a file.

ii) write the contents into a file.

iii) append the contents to existing file

<?php

echo "-------------------";

echo file_put_contents("new_file.txt","Hello, how are you?");

echo "-------------------";

echo "<br><br>";

echo file_get_contents("new_file.txt");

echo "<br><br>";

echo "-------------------";

echo file_put_contents("new_file1.txt","Hello, how is it going?",FILE_APPEND);

echo "-------------------";

echo "<br><br>";

echo file_get_contents("new_file1.txt");

echo "<br><br>";

?>

Output :
b) Write a HTML code that takes name and email as input and display the entered
details using $_POST array.

post.html

<!DOCTYPE html>
<html>
<head>
<title>Post Form</title>
</head>
<body>
<h1>Post Form</h1>
<form action="post.php" method="POST">
Name : <input type="text" name="name"/><br><br>
Email : <input type="email" name="email"/><br><br>
<input type="submit"/>
</form>
</body>
</html>

post.php

<!DOCTYPE html>
<html>
<head>
<title>Post PHP</title>
</head>
<body>
<p>Welcome <?php echo $_POST['name']; ?></p>
<p>Your email is <?php echo $_POST['email']; ?></p>
</body>
</html>
Output :

7. Execute the following:


a) Write a PHP script to accept user input data, store it in a text file and display the
file contents.

file_display.html

<!DOCTYPE html>
<html>
<head>
<title>File Manipulation</title>
</head>
<body>
<form action="file_display.php" method="POST">
<p>Enter text : </p>
<textarea name="text"></textarea>
<br><br>
<input type="submit" name="submit" value="submit"\>
</form>
</body>
</html>

file_display.php

<?php

if(isset($_POST['submit'])){
$cont = $_POST['text'];

file_put_contents("text_file.txt",$cont) or die("Unable to open");

echo file_get_contents("text_file.txt");

}
?>

Output :

b) Write a HTML code that takes name and email as input and display the entered
details using $_GET array.

get.html
<!DOCTYPE html>
<html>
<head>
<title>Get Form</title>
</head>
<body>
<h1>Get Form</h1>
<form action="get.php" method="GET">
Name : <input type="text" name="name"/><br><br>
Email : <input type="email" name="email"/><br><br>
<input type="submit"/>
</form>
</body>
</html>

get.php

<!DOCTYPE html>
<html>
<head>
<title>PHP Form</title>
</head>
<body>
<p>Welcome <?php echo $_GET['name']; ?></p>
<p>Email is <?php echo $_GET['email']; ?></p>
</body>
</html>

Output :
8. Execute the following:
a)Write a PHP script to sort and display the array in Ascending & descending order.

<?php

$arr = array(34,76,23,12,76,32,11,54,34,76,43,23);

sort($arr);

echo "Acending order : ";


echo "<br>";
foreach($arr as $val){
echo ("$val ");
}

echo "<br><br>";
echo "Descending order : ";
echo "<br>";
$len=count($arr);
for($i=$len-1;$i>=0;$i--){
echo "$arr[$i]";
echo " ";
}

?>

Output :
b) Write a PHP program to connect to a database and retrieve data from a Student
table. Student table consists of Student Id, Student Name, USN, Branch, City, CGPA.
Show the details in a neat format, Insert Data and show the entered Data, Order Data
based on CGPA, Delete the rows with CGPA less than 7.75. Update the city as
NMAMIT Nitte

<?php

$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
echo "Connection failed! " . $conn->connect_error;
}

echo "Connection established.";

$sql1 = "Create table student3


(Student_ID int,
Student_Name varchar(50),
USN varchar(50),
Branch varchar(50),
City varchar(50),
CGPA float)";
if(mysqli_query($conn,$sql1)){
echo "Table created";
} else {
echo "Table not created";
}

$sql2 = "INSERT INTO student3 VALUES


(101,'Neeraj','IS101','IS','Kateel',9.12),
(102,'Dheeraj','IS102','IS','Kottara',7.23),
(103,'Suraj','IS103','IS','Bantwal',8.21),
(104,'Pankaj','IS104','IS','Ajmer',6.31),
(105,'Karthik','IS105','IS','Karkala',8.75),
(106,'Suchit','IS106','IS','Katpady',4.54)";

if($conn->query($sql2) === TRUE){


echo "<br>";
echo "Row inserted";
} else {
echo "Row not inserted";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql3 = mysqli_query($conn, "Select * from student3");
while($data = mysqli_fetch_array($sql3)){
echo "<br>";
echo $data["Student_ID"]." "."|"." ";
echo $data["Student_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Branch"]." "."|"." ";
echo $data["City"]." "."|"." ";
echo $data["CGPA"]." "."|"." ";
}

echo "<br><br>";
echo "Ordered by CGPA";
echo "<br>";
$sql4 = mysqli_query($conn, "Select * from student3 order by CGPA");
while($data = mysqli_fetch_array($sql4)){
echo "<br>";
echo $data["Student_ID"]." "."|"." ";
echo $data["Student_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Branch"]." "."|"." ";
echo $data["City"]." "."|"." ";
echo $data["CGPA"]." "."|"." ";
}

echo "<br><br>";
echo "Deleting rows with cgpa below 7.75";
echo "<br>";
$sql5 = "Delete from student3 where CGPA<7.75";
if(mysqli_query($conn,$sql5)){
echo "Rows deleted";
} else {
echo "Error";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql7 = mysqli_query($conn, "Select * from student3");
while($data = mysqli_fetch_array($sql7)){
echo "<br>";
echo $data["Student_ID"]." "."|"." ";
echo $data["Student_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Branch"]." "."|"." ";
echo $data["City"]." "."|"." ";
echo $data["CGPA"]." "."|"." ";
}

echo "<br><br>";
echo "Updating City to NMAMIT Nitte";
echo "<br>";
$sql6 = "Update student3 set City='NMAMIT Nitte' where Student_ID=103";
if(mysqli_query($conn,$sql6)){
echo "Rows updated";
} else {
echo "Error";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql8 = mysqli_query($conn, "Select * from student3");
while($data = mysqli_fetch_array($sql8)){
echo "<br>";
echo $data["Student_ID"]." "."|"." ";
echo $data["Student_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Branch"]." "."|"." ";
echo $data["City"]." "."|"." ";
echo $data["CGPA"]." "."|"." ";
}

?>
Output :
9. Execute the following:
a) Change Text Color of the Elements Using Jquery

<!DOCTYPE>
<html>
<head>
<title>Change Text Color</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("p").css("color","cyan")
});
</script>
</head>
<body>
<p>Hello</p>
</body>
</html>

Output :

b) Write a PHP program to connect to a database and retrieve data from a Employee
table. Employee table consists of emp id, emp name, dept, designation, salary, date
of join. Show the details in a neat format, Insert Data and show the entered Data,
Order the Data based on salary.

<?php

$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
echo "Connection failed! " . $conn->connect_error;
}

echo "Connection established.";

$sql1 = "Create table employee1


(Employee_ID int,
Employee_Name varchar(50),
Department varchar(50),
Designation varchar(50),
Salary bigint,
Date_of_Join date)";

if(mysqli_query($conn,$sql1)){
echo "Table created";
} else {
echo "Table not created";
}

$sql2 = "INSERT INTO employee1 VALUES


(101,'Neeraj','Information Science','Manager',24000,'2016-05-13'),
(102,'Dheeraj','Information Science','Director',32000,'2017-12-23'),
(103,'Suraj','Information Science','Human Resource Director',22000,'2018-04-03'),
(104,'Pankaj','Information Science','Developer',16000,'2019-10-23'),
(105,'Karthik','Information Science','Web Developer',21000,'2020-11-25'),
(106,'Suchit','Information Science','IOT Developer',21222,'2021-12-23')";

if($conn->query($sql2) === TRUE){


echo "<br>";
echo "Row inserted";
} else {
echo "Row not inserted";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql3 = mysqli_query($conn, "Select * from employee1");
echo "<table>";
echo
"<tr><th>Employee_ID</th><th>Employee_Name</th><th>Department</th><th>Designati
on</th><th>Salary</th><th>Date_of_Join</th></tr>";
while($data = mysqli_fetch_array($sql3)){
echo "<br>";
echo "<tr>";
echo "<td>" . $data['Employee_ID'] . "</td>";
echo "<td>" . $data['Employee_Name'] . "</td>";
echo "<td>" . $data['Department'] . "</td>";
echo "<td>" . $data['Designation'] . "</td>";
echo "<td>" . $data['Salary'] . "</td>";
echo "<td>" . $data['Date_of_Join'] . "</td>";
echo "<tr>";
}
echo "</table>";

echo "<br><br>";
echo "Ordered by Salary";
echo "<br>";
$sql4 = mysqli_query($conn, "Select * from employee1 order by Salary");
echo "<table>";
echo
"<tr><th>Employee_ID</th><th>Employee_Name</th><th>Department</th><th>Designati
on</th><th>Salary</th><th>Date_of_Join</th></tr>";
while($data = mysqli_fetch_array($sql4)){
echo "<br>";
echo "<tr>";
echo "<td>" . $data['Employee_ID'] . "</td>";
echo "<td>" . $data['Employee_Name'] . "</td>";
echo "<td>" . $data['Department'] . "</td>";
echo "<td>" . $data['Designation'] . "</td>";
echo "<td>" . $data['Salary'] . "</td>";
echo "<td>" . $data['Date_of_Join'] . "</td>";
echo "<tr>";
}
echo "</table>";
?>
Output :
10. Execute the following:
a) Write a PHP script that accepts filename, extension, text content and writes into
file.
<!DOCTYPE html>
<html>
<head>
<title>File extentions</title>
</head>
<body>
<form method="post">
<p>File name : <input type="text" name="file"/>
<p>Extention : </p>
<select name="ext">
<option value="txt">txt</option>
<option value="doc">doc</option>
<option value="pdf">pdf</option>
<option value="docx">docx</option>
<option value="odt">odt</option>
</select>
<br><br>
<textarea name="cont"></textarea><br><br>
<input type="submit" value="submit" name="submit"\>
</form>
</body>
</html>

<?php

if(isset($_POST['submit']))
{
$filename = $_POST['file'];
$extention = $_POST['ext'];
$content = $_POST['cont'];

file_put_contents($filename.".".$extention,$content) or die("Unable to open");


}

?>

Output :
b) Write a PHP program to connect to a database and retrieve data from a Library
database. The database table consists of Book Id, Book Title, Author name, ISSN,
Price, Edition. Show the details in a neat format, Insert Data and show the entered
Data, Order Data based on Book Title, Delete the rows with Price less than 600.
Update the Author name as Chetan Bhagat.

<?php

$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
echo "Connection failed! " . $conn->connect_error;
}
echo "Connection established.";

$sql1 = "Create table library1


(Book_ID int,
Book_Title varchar(50),
Author_Name varchar(50),
USN varchar(5),
Price int,
Edition int)";

if(mysqli_query($conn,$sql1)){
echo "Table created";
} else {
echo "Table not created";
}

$sql2 = "INSERT INTO library1 VALUES


(101,'Emma','Jane Austen','IS101',1200,10),
(102,'Atonement','Ian McEwan','IS102',2100,23),
(103,'Rebecca','Daphne du Maurier','IS103',120,12),
(104,'Coraline','Neil Gaiman','IS104',230,1),
(105,'Annihilation','Jeff VanderMeer','IS105',650,2),
(106,'Carrie','Stephen King','IS106',1000,3)";

if($conn->query($sql2) === TRUE){


echo "<br>";
echo "Row inserted";
} else {
echo "Row not inserted";
}
echo "<br><br>";
echo "Display table";
echo "<br>";
$sql3 = mysqli_query($conn, "Select * from library1");
while($data = mysqli_fetch_array($sql3)){
echo "<br>";
echo $data["Book_ID"]." "."|"." ";
echo $data["Book_Title"]." "."|"." ";
echo $data["Author_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Price"]." "."|"." ";
echo $data["Edition"]." "."|"." ";
}

echo "<br><br>";
echo "Ordered by Book_Title";
echo "<br>";
$sql4 = mysqli_query($conn, "Select * from library1 order by Book_Title");
while($data = mysqli_fetch_array($sql4)){
echo "<br>";
echo $data["Book_ID"]." "."|"." ";
echo $data["Book_Title"]." "."|"." ";
echo $data["Author_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Price"]." "."|"." ";
echo $data["Edition"]." "."|"." ";
}

echo "<br><br>";
echo "Deleting rows";
echo "<br>";
$sql5 = "Delete from library1 where Price<600";
if(mysqli_query($conn,$sql5)){
echo "Rows deleted";
} else {
echo "Error";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql7 = mysqli_query($conn, "Select * from library1");
while($data = mysqli_fetch_array($sql7)){
echo "<br>";
echo $data["Book_ID"]." "."|"." ";
echo $data["Book_Title"]." "."|"." ";
echo $data["Author_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Price"]." "."|"." ";
echo $data["Edition"]." "."|"." ";
}

echo "<br><br>";
echo "Updating rows";
echo "<br>";
$sql6 = "Update library1 set Author_Name='Chetan Bhagat' where Book_ID=101";
if(mysqli_query($conn,$sql6)){
echo "Rows updated";
} else {
echo "Error";
}
echo "<br><br>";
echo "Display table";
echo "<br>";
$sql8 = mysqli_query($conn, "Select * from library1");
while($data = mysqli_fetch_array($sql8)){
echo "<br>";
echo $data["Book_ID"]." "."|"." ";
echo $data["Book_Title"]." "."|"." ";
echo $data["Author_Name"]." "."|"." ";
echo $data["USN"]." "."|"." ";
echo $data["Price"]." "."|"." ";
echo $data["Edition"]." "."|"." ";
}

?>

Output :
11. Execute the following:
a) Creating a simple show and hide effect in jQuery

<!DOCTYPE>
<html>
<head>
<title>Hide and Show</title>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$(".showbtn").click(function(){
$("p").show();
});
$(".hidebtn").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<p>This text is shown.</p>
<br>
<button class="showbtn">Show</button>
<button class="hidebtn">Hide</button>
</body>
</html>

Output :

b) Write a PHP program to connect to a database and retrieve data from a Car
Database table. Car Database table consists of Reg.No, Reg Owner, Address, Fuel
Type used, Car Model, Price. Show the details in a neat format, Insert Data and show
the entered Data, Order the Data based on Car Price.

<?php

$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
echo "Connection failed! " . $conn->connect_error;
}

echo "Connection established.";

$sql1 = "Create table car2


(RegNo int,
Reg_Owner varchar(50),
Address varchar(50),
Fuel_Type varchar(50),
Car_Model varchar(50),
Price bigint)";

if(mysqli_query($conn,$sql1)){
echo "Table created";
} else {
echo "Table not created";
}

$sql2 = "INSERT INTO car2 VALUES


(101,'Neeraj','Kateel','Petrol','C101',2300000),
(102,'Dheeraj','Kankanady','CNG','C102',2100000),
(103,'Suraj','Karkala','Diesel','C103',1200000),
(104,'Pankaj','Bantwal','Diesel','C104',1400000),
(105,'Karthik','Kateel','Petrol','C105',3300000),
(106,'Suchit','Karkala','CNG','C106',1100000)";
if($conn->query($sql2) === TRUE){
echo "<br>";
echo "Row inserted";
} else {
echo "Row not inserted";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql3 = mysqli_query($conn, "Select * from car2");
echo "<table>";
echo
"<tr><th>Reg.No</th><th>Reg_Owner</th><th>Address</th><th>Fuel_Type</th><th>Car
_Model</th><th>Price</th></tr>";
while($data = mysqli_fetch_array($sql3)){
echo "<tr>";
echo "<td>" . $data['RegNo'] . "</td>";
echo "<td>" . $data['Reg_Owner'] . "</td>";
echo "<td>" . $data['Address'] . "</td>";
echo "<td>" . $data['Fuel_Type'] . "</td>";
echo "<td>" . $data['Car_Model'] . "</td>";
echo "<td>" . $data['Price'] . "</td>";
echo "<tr>";
}
echo "</table>";

echo "<br><br>";
echo "Ordered by Salary";
echo "<br>";
$sql4 = mysqli_query($conn, "Select * from car2 order by Price");
echo "<table>";
echo
"<tr><th>Reg.No</th><th>Reg_Owner</th><th>Address</th><th>Fuel_Type</th><th>Car
_Model</th><th>Price</th></tr>";
while($data = mysqli_fetch_array($sql4)){
echo "<tr>";
echo "<td>" . $data['RegNo'] . "</td>";
echo "<td>" . $data['Reg_Owner'] . "</td>";
echo "<td>" . $data['Address'] . "</td>";
echo "<td>" . $data['Fuel_Type'] . "</td>";
echo "<td>" . $data['Car_Model'] . "</td>";
echo "<td>" . $data['Price'] . "</td>";
echo "<tr>";
}
echo "</table>";

?>

Output :
12. Execute the following:
a) Write a PHP script to generate the following output
*
**
***
****
*****
<?php

for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}

?>
Output :

b) Write a PHP program to connect to a database and retrieve data from a Movie
Database table. Movie Database table consists of Movie Name, Director Name, Actor
Name, Ratings, Role, Language, Genre. Show the details in a neat format, Insert
Data and show the entered Data, Order the Data based on Movie Ratings.

<?php

$servername = "172.16.2.3";
$database = "student";
$username = "student";
$password = "student";

$conn = mysqli_connect($servername, $username, $password, $database);

if(!$conn){
echo "Connection failed! " . $conn->connect_error;
}

echo "Connection established.";

$sql1 = "Create table movie1


(Movie_Name varchar(50),
Director_Name varchar(50),
Actor_Name varchar(50),
Ratings float,
Role varchar(50),
Language varchar(50),
Genre varchar(50))";

if(mysqli_query($conn,$sql1)){
echo "Table created";
} else {
echo "Table not created";
}

$sql2 = "INSERT INTO movie1 VALUES


('Haven','John Gray','Natasha Richardson',6.4,'Jane','English','Drama'),
('Limelight','Charles Chaplin','Claire Bloom',8.0,'Nigel','English','Thriller'),
('Fear','Roberto Rossellini','Ingrid Bergman',6.7,'Kurt','Hindi','Comedy')";

if($conn->query($sql2) === TRUE){


echo "<br>";
echo "Row inserted";
} else {
echo "Row not inserted";
}

echo "<br><br>";
echo "Display table";
echo "<br>";
$sql3 = mysqli_query($conn, "Select * from movie1");
echo "<table>";
echo
"<tr><th>Movie_Name</th><th>Director_Name</th><th>Actor_Name</th><th>Ratings</t
h><th>Role</th><th>Language</th><th>Genre</th></tr>";
while($data = mysqli_fetch_array($sql3)){
echo "<tr>";
echo "<td>" . $data['Movie_Name'] . "</td>";
echo "<td>" . $data['Director_Name'] . "</td>";
echo "<td>" . $data['Actor_Name'] . "</td>";
echo "<td>" . $data['Ratings'] . "</td>";
echo "<td>" . $data['Role'] . "</td>";
echo "<td>" . $data['Language'] . "</td>";
echo "<td>" . $data['Genre'] . "</td>";
echo "<tr>";
}
echo "</table>";

echo "<br><br>";
echo "Ordered by Salary";
echo "<br>";
$sql4 = mysqli_query($conn, "Select * from movie1 order by Ratings");
echo "<table>";
echo
"<tr><th>Movie_Name</th><th>Director_Name</th><th>Actor_Name</th><th>Ratings</t
h><th>Role</th><th>Language</th><th>Genre</th></tr>";
while($data = mysqli_fetch_array($sql4)){
echo "<tr>";
echo "<td>" . $data['Movie_Name'] . "</td>";
echo "<td>" . $data['Director_Name'] . "</td>";
echo "<td>" . $data['Actor_Name'] . "</td>";
echo "<td>" . $data['Ratings'] . "</td>";
echo "<td>" . $data['Role'] . "</td>";
echo "<td>" . $data['Language'] . "</td>";
echo "<td>" . $data['Genre'] . "</td>";
echo "<tr>";
}
echo "</table>";
?>
Output :

You might also like