0% found this document useful (0 votes)
156 views

Web Technology

The document contains sample code programs for various questions related to HTML, CSS, JavaScript, PHP and SQL. The programs cover topics like: 1. Creating an HTML table from data 2. Increasing font size of text using JavaScript 3. Creating classes and objects in PHP to represent data like employees or students. The document acts as a question bank for students to practice and learn various web technologies through solved examples and code snippets. It provides full programs for questions on front-end and back-end programming.
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)
156 views

Web Technology

The document contains sample code programs for various questions related to HTML, CSS, JavaScript, PHP and SQL. The programs cover topics like: 1. Creating an HTML table from data 2. Increasing font size of text using JavaScript 3. Creating classes and objects in PHP to represent data like employees or students. The document acts as a question bank for students to practice and learn various web technologies through solved examples and code snippets. It provides full programs for questions on front-end and back-end programming.
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/ 38

PROGRAMS

I. Question Bank

Q1. Create HTML for the following table. (Module 2 Q2)


ONE TWO
THREE FOUR

Program :-
<html>
<head> </head>
<body>
<table>
<tr>
<td rowspan = 2>ONE</td>
<td colspan = 2>TWO</td>
</tr>
<tr>
<td>THREE</td>
<td>FOUR</td>
</tr>
<table>
</body>
</html>
Q2. Write a Javascript code that displays “VTU BELGAVI” with
increasing font size in the interval of 100ms in blue color, when
the font size reaches 50pt it should stop.( Module 3 Q15)

Program :-
<!DOCTYPE html>
<html>
<head>
<title>
Text Grow
</title>
</head>
<body>
<div id="txt"></div>
<script>
var font_size = 5, interval;
var divElm = document.getElementById('txt');
function growing() {
if (font_size == 5) {
divElm.innerHTML = ' VTU BELGAVI ';
divElm.style.fontSize = font_size + 'pt';
divElm.style.color = 'blue';
interval = setInterval(growing, 100);
font_size++;
} else if(font_size == 50) {
clearInterval(interval);
} else {
divElm.style.fontSize = font_size + 'pt';
font_size++;
}
}
growing();
</script>
</body>
</html>

Q3. Write a Javascript code that displays “CORONA VIRUS”


with increasing font size in the interval of 100ms in blue color,
when the font size reaches 50pt and teal color it should stop.
( Module 3 Q16)

Program :-
<!DOCTYPE html>
<html>
<head>
<title>
Text Grow
</title>
</head>
<body>
<div id="txt"></div>
<script>
var font_size = 5, interval;
var divElm = document.getElementById('txt');
function growing() {
if (font_size == 5) {
divElm.innerHTML = ' VTU BELGAVI ';
divElm.style.fontSize = font_size + 'pt';
divElm.style.color = 'blue';
interval = setInterval(growing, 100);
font_size++;
} else if(font_size == 50) {
clearInterval(interval);
divElm.style.color = 'teal';
} else {
divElm.style.fontSize = font_size + 'pt';
font_size++;
}
}
growing();
</script>
</body>
</html>

Q4. Write a PHP program to greet the user based on time.


( Module 3 Q26)

Program :-
<?php
$hourOfDay = $date('H');
if ( $hourOfDay > 6 && $hourOfDay < 12 ) {
$greeting = "Good Morning";
}
else if ($hourOfDay >= 12 && $hourOfDay < 16 ) {
$greeting = "Good Afternoon;
}
else if ($hourOfDay >= 16 && $hourOfDay < 20) {
$greeting = "Good Evening;
}
else if ($hourOfDay >= 20 && $hourOfDay < 6) {
$greeting = "Good Night;
}
?>

Q5. Write a php program to create a class employee with the


following specifications:
Data Members: Name,Id,Payment
Member functions: Read(getters) and write(setters)
Use the above specifications to read and print information of 10
students. (8 marks)
OR
Write a php program to create a class student with the following
specifications:
Data Members: Name,Roll Number,Average marks
Member functions: Read(getters) and write(setters)
( Module 4 Q11)

Program :-
1) Employee Part
<?php
class Employee {
private $id;
private $firstName;
private $lastName;
private $payment;
public function Write($ID,$firstName,$lastName,$Pay)
{
$this->id=$ID;
$this->firstName=$firstName;
$this->lastName=$lastName;
$this->payment=$Pay;
}
public function Read( )
{
echo $this->id.” “.$this->firstName.” “.$this->lastName.” “.$this-
>payment.”<br>”;
}
}
$ep1 = new Employee( );
$ep1->Write('1','Kai','G',1200);
$ep1->Read( );

$ep2 = new Employee( );


$ep2->Write('2','Arya','Khan',1500);
$ep2->Read( );
//Similarly write for 8 more,otherwise you can use a loop mechanism.
?>
2) Student Part
<?php
class Student {
private $usn;
private $firstName;
private $lastName;
private $avg;
public function Write($USN,$firstName,$lastName,$AvgMarks)
{
$this->usn=$USN;
$this->firstName=$firstName;
$this->lastName=$lastName;
$this->avg=$AvgMarks;
}
public function Write( )
{
echo $this->usn.” “.$this->firstName.” “.$this->lastName.” “.$this-
>avg.”<br>”;
}
}
$st1 = new Student( );
$st1->Write('1','Kai','G',12);
$st1->Read( );
$st2 = new Student( );
$st2->Write('2','Arya','Khan',15);
$st2->Read( );
?>
Q6. Write a php program to display the number of objects
created.( Module 4 Q12)

Program :-
<?php
class Student {
public static $count=0;
private $usn;
private $firstName;
private $lastName;
private $marks;
public function __construct($usn,$firstName,$lastName,$m)
{
$this->usn=$usn;
$this->firstName=$firstName;
$this->lastName=$lastName;
$this->marks=$m;
self::$count++;
}
public function display()
{
echo $this->usn.” “.$this->firstName.” “.$this->lastName.” “.$this-
>marks.”<br>”;
echo “No of objects = “.self::$count;
}
}
$st1 = new Student('1','Kai','G',12);
$st1->display();
$st2 = new Student('2','Arya','Khan',15);
$st2->display();
?>

Q7. Write the php code to validate phone number.


( Module 4 Q24)

Program :-
<?php
function validating($phone){
if(preg_match('/^[0-9]{10}$/', $phone)) {
echo " Valid Phone Number";
} else {
echo " Invalid Phone Number";}
}
validating(8902389109);
validating(890238910912);
validating(8902389$%9);
?>

Or
<?php
function validating($phone){
if(is_numeric($phone) && strlen($phone)==10) {
echo " Valid Phone Number";
} else {
echo " Invalid Phone Number";}
}

validating(8902389109);
validating(890238910912);
validating(8902389$%9);
?>

II. TIE Important Questions

Program :-
<html>
<head> </head>
<body>
<table>
<tr>
<th rowspan = 2></th>
<th colspan = 2>Average</th>
<th rowspan =2 colspan = 2>Red eyes</th>
</tr>
<tr>
<th>height</th>
<th>weight</th>
</tr>
<tr>
<th>Males</th>
<td>1.9</td>
<td>0.003</td>
<td>40%</td>
</tr>
<tr>
<th>Females</th>
<td>1.7</td>
<td>0.002</td>
<td>43%</td>
</tr>
<table>
</body>
</html>
Program :-
<html>
<head></head>
<body>
<form method = ‘GET’ action =”/action.php”>
<fieldset>
<legend>Personal Details</legend>
<label for = “sal”>Salutation</label>
<select name = “sal” id = “sal”>
<option value=”None”>--None--</option>
<option value=”Mr”>Mr</option>
<option value=”Mrs”>Mrs</option>
</select>
<label for = “fname”>First Name</label>
<input type = “text” name = “fname” id =”fname” />
<label for = “lname”>Last Name</label>
<input type = “text” name = “lname” id =”lname” />
<label for = “gender”>Gender</label>
<input type = “radio” name = “gender” value =”Male”>Male
</input>
<input type = “radio” name = “gender” value =”Female”>Female
</input>
<label for = “mail”>Email</label>
<input type = “email” name = “mail” id =”mail” />
<label for = “DOB”>Date of Birth</label>
<input type = “date” name = “DOB” id =”DOB” />
<label for = “addr”>Address</label>
<textarea name = “addr” id =”addr” rows=”4” cols=”50” />
<input type = “submit” value = “Submit” />
</fieldset>
</form>
</body>
</html>

Program :-
<!DOCTYPE html>
<html>
<head>
<title>
Text Grow
</title>
</head>
<body>
<div id="txt"></div>
<script>
var font_size = 5, interval;
var divElm = document.getElementById('txt');
function growing() {
if (font_size == 5) {
divElm.innerHTML = ' TAKEITEASY ENGINEERS';
divElm.style.fontSize = font_size + 'pt';
divElm.style.color = 'red';
interval = setInterval(growing, 100);
font_size++;
} else if(font_size == 50) {
clearInterval(interval);
} else {
divElm.style.fontSize = font_size + 'pt';
font_size++;
}
}
growing();
</script>
</body>
</html>

Program :-
<html>
<head>
<script>
function Tables()
{
var table = document.getElementById('table1');
var i=15;
while(i!=-1){
var row = table.insertRow(1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = i;
cell2.innerHTML = i*i;
cell3.innerHTML = i*i*i;
i=i-1;
}
}
</script>
</head>
<body onload = "Tables()">
<table id = "table1" border="2px">
<tr>
<th>NUMBER</th>
<th>SQUARE</th>
<th>CUBE</th>
</tr>
</table>
</body>
</html>

Program :-
1)
<?php
session_start();
if(isset($_SESSION['count'])) {
echo "Pageviews: " .$_SESSION['count'];
$_SESSION['count']++;
}
else {
$_SESSION['count']=1;
echo "Pageviews" .$_SESSION['count'];
}
?>

2)
<?php
$hourOfDay = $date('H');
if ( $hourOfDay > 6 && $hourOfDay < 12 ) {
$greeting = "Good Morning";
}
else if ($hourOfDay >= 12 && $hourOfDay < 16 ) {
$greeting = "Good Afternoon;
}
else if ($hourOfDay >= 16 && $hourOfDay < 20) {
$greeting = "Good Evening;
}
else if ($hourOfDay >= 20 && $hourOfDay < 6) {
$greeting = "Good Night;
}
?>

Program :-
JAVA SCRIPT part
const input1 = document.getElementById('item1');
const input2 = document.getElementById('item2');
const button = document.querySelector('button');
const results = document.querySelector('.results');
button.onclick = (e) => {
const num1 = parseInt(input1.value, 10);
input1.value = ' ';
const num2 = parseInt(input2.value, 10);
input2.value = ' ';
const element = document.createElement('li');
element.textContent = `${num1} + ${num2} = ${num1 + num2}`;
results.append(element);
input1.focus();
}

HTML part
<html>
<head></head>
<body>
<h1>Calculator</h1>
<div>
<label for="item">Enter a number in each field:</label>
<input type="text" id="item1">
<input type="text" id="item2">
<button>Add numbers</button>
</div>
<h2>Results</h2>
<ul class="results"></ul>
</body>
</html>

Program :-
<?php
class Student {
private $usn;
private $firstName;
private $lastName;
private $avg;
public function Write($USN,$firstName,$lastName,$AvgMarks)
{
$this->usn=$USN;
$this->firstName=$firstName;
$this->lastName=$lastName;
$this->avg=$AvgMarks;
}
public function Write( )
{
echo $this->usn.” “.$this->firstName.” “.$this->lastName.” “.$this-
>avg.”<br>”;
}
}
$st1 = new Student( );
$st1->Write('1','Kai','G',12);
$st1->Read( );
$st2 = new Student( );
$st2->Write('2','Arya','Khan',15);
$st2->Read( );
?>
III)
Web Assignment
Q1. Write a JavaScript to design a simple calculator to perform the following operations: sum,
product, difference and quotient.
<html>
<head>
<script>
function insert(value)
{
document.getElementById('txt1').value += value;
}
function ans()
{
result =
eval(document.getElementById('txt1').value);
document.getElementById('txt1').value = result;
}
function clr(value)
{
document.getElementById('txt1').value = "";
}
</script>
</head>
<body>
<input type = "text" id = "txt1" />
<br>
<table>
<tr>
<td><input type = "button" value = "1"
id="btn
" onclick="insert(value)" /></td>
<td><input type = "button" value = "2"
id="btn"onclick="insert(value)"/></td>
<td><input type = "button" value = "3"
id="btn"onclick="insert(value)"/></td>
</tr>
<tr>
<td><input type = "button" value = "4"
id="btn"onclick="insert(value)"/></td>
<td><input type = "button" value = "6" id="btn"
onclick="insert(value)"/></td>
</tr>
<tr>
<td><input type = "button" value = "7" id="btn"
onclick="insert(value)"/></td>
<td><input type = "button" value = "8" id="btn"
onclick="insert(value)"/></td>
<td><input type = "button" value = "9" id="btn"
onclick="insert(value)"/></td>
</tr>
<tr>
<td><input type = "button" value = "+" id="btn"
onclick="insert(value)"/></td>
<td><input type = "button" value = "0" id="btn"
onclick="insert(value)"/></td>
<td><input type = "button" value = "-" id="btn"
onclick="insert(value)"/></td>
</tr>
<tr>
<td><input type = "button" value = "*" id="btn"
onclick="insert(value)"/></td>
<td><input type = "button" value = "="
onclick="ans()"/></td>
<td><input type = "button" value = "/" id="btn"
onclick="insert(value)"/></td>
</tr>
</table>
<input type = "button" value = "C" onclick="clr()"/>
</body>
</html>
Q2. Write a JavaScript that calculates the squares and cubes of the numbers from 0 to 10 and
outputs HTML text that displays the resulting values in an HTML table format.

<html>
<head>
<script>
function Tables()
{
var table =
document.getElementById('table1'); var i=10;
while(i!=0){
var row =
table.insertRow(1); var
cell1 = row.insertCell(0);
var cell2 =
row.insertCell(1);var cell3
= row.insertCell(2);
cell1.innerHTML = i;
cell2.innerHTML = i*i;
cell3.innerHTML = i*i*i;
i=i-1;
}
}
</script>
</head>
<body onload = "Tables()">
<table id = "table1" border="2px">
<tr>
<th>NUMBER</th>
<th>SQUARE</th>
<th>CUBE</th>
</tr>
</table>
</body>
</html>

Q3. Write a JavaScript code that displays text “TEXT-GROWING” with increasing font size in the
interval of 100ms in RED COLOR, when the font size reaches 50pt it displays
“TEXT-SHRINKING” in BLUE color. Then the font size decreases to 5pt.

<!DOCTYPE html>
<html>
<head>
<title>
Text Grow
</title>
<style>

</style>
</head>
<body>

<div id="txt"></div>
<script>
var font_size = 5, interval;
var divElm = document.getElementById('txt');
function growing() {
if (font_size == 5) {
divElm.innerHTML = 'TEXT-GROWING';
divElm.style.fontSize = font_size + 'pt';
divElm.style.color = 'red';
interval = setInterval(growing, 100);
font_size++;
} else if(font_size == 50) {
clearInterval(interval);
shrink();
} else {
divElm.style.fontSize = font_size + 'pt';
font_size++;
}
}

function shrink() {
if (font_size == 50) {
divElm.innerHTML = 'TEXT-SHRINKING';
divElm.style.fontSize = font_size + 'pt';
divElm.style.color = 'blue';
interval = setInterval(shrink, 100);
font_size--;
} else if(font_size == 5) {
clearInterval(interval);
growing();
} else {
divElm.style.fontSize = font_size + 'pt';
font_size--;
}
}

growing();
</script>
</body>
</html>
Q4. Develop and demonstrate a HTML5 file that includes JavaScript script that
uses functions for the following problems:
1. Parameter: A string
2. Output: The position in the string of the left-most vowel
3. Parameter: A number
4. Output: The number with its digits in the reverse order

<html>
<body>
<script type="text/javascript">
var str = prompt("Enter the
Input",""); if(!(isNaN(str)))
{
var
num,rev=0,remainder; num
= parseInt(str);
while(num!=0) {
remainder = num%10;
num = parseInt(num/10);
rev = rev * 10 + remainder;
}
alert("Reverse of "+str+" is "+rev);
}
else
{
str = str.toUpperCase();
for(var i = 0; i < str.length; i++)
{ var chr = str.charAt(i);
if(chr == 'A' || chr == 'E' || chr == 'I' || chr ==
'O'
|| chr == 'U')break;
}
if( i < str.length )
alert("The position of the left most vowel in the
given word "+ str + " is "+(i+1));
else
alert("No vowel found in the entered string "+str);
}
</script>
</body>
</html
>Q6. Write a PHP program to keep track of the number of visitors visiting the
web page and to display this count of visitors, with proper headings.
<?php

if(!file_exists("counter.txt"))
{
$f=fopen("counter.txt","w"
);fputs($f,0);
fclose($f);
}

$f1 = fopen("counter.txt","r");
$count =
fgets($f1);
fclose($f1);

$count = $count + 1;

$f2 =
fopen("counter.txt","w");
fputs($f2,$count);
fclose($f2);

echo "No.of vistors: ".$count;


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

<?php
$date = date('h:i:s A');
echo "Current time on Server: ".$date;
?>

Q8.Write the PHP programs to do the following:

a. Implement simple calculator operations.

b. Find the transpose of a matrix.

c. Multiplication of two matrices.

d. Addition of two matrices.


<html>
<head>
</head>
<body>

a. Implement simple calculator operations.


<form method = "post">
<input type = 'text' name = 'txt1' />
<br>
<input type = 'submit' value ='+' name = 'add'/>
<input type = 'submit' value ='-' name = 'sub'/>
<input type = 'submit' value ='*' name = 'mul'/>
<input type = 'submit' value ='/' name = 'div'/>
<br>
<input type = 'text' name = 'txt2' />
</form>

<?php
if(isset($_POST["add"]))
{
$ans =
$_POST["txt1"]+$_POST["txt2"];echo
"Ans = ".$ans;
}
if(isset($_POST["sub"]))
{
$ans = $_POST["txt1"]-
$_POST["txt2"];echo "Ans = ".$ans;
}
if(isset($_POST["mul"]))
{
$ans = $_POST["txt1"]*$_POST["txt2"];
echo "Ans = ".$ans;
}
if(isset($_POST["div"]))
{
$ans = $_POST["txt1"]/$_POST["txt2"];
echo "Ans = ".$ans;
}

?>
<br>
b. Find the transpose of a matrix.
<?php
$m1 = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
echo "Matrix 1: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m1[$i][$j]." ";
}
echo "<br>";
}
echo "<br> Transpose is: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m1[$j][$i]." ";
}
echo "<br>";
}
?>
c. Multiplication of two matrices.
<?php
$m2 = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
$m2 = array(
array(1,4,7),
array(2,5,8),
array(3,6,9)
);
echo "Matrix 1: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m1[$i][$j]." ";
}
echo "<br>";
}
echo "Matrix 2: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m2[$i][$j]." ";
}
echo "<br>";
}
echo "Multiplication matrix is: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
$c[$i][$j]=0;
for($k=0;$k<3;$k++)
{
$c[$i][$j] += $m1[$i][$k] * $m2[$k][$j];
}
echo $c[$i][$j]." ";
}
echo "<br>";
}
?>
d. Addition of two matrices.
<?php
$m2 = array(
array(1,2,3),
array(4,5,6),
array(7,8,9)
);
$m2 = array(
array(1,4,7),
array(2,5,8),
array(3,6,9)
);
echo "Matrix 1: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m1[$i][$j]." ";
}
echo "<br>";
}
echo "Matrix 2: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
echo $m2[$i][$j]." ";
}
echo "<br>";
}
echo "Addition matrix is: <br>";
for($i=0;$i<3;$i++)
{
for($j=0;$j<3;$j++)
{
$c[$i][$j]=0;

$c[$i][$j] += $m1[$i][$j] + $m2[$i][$j];


echo $c[$i][$j]." ";
}
echo "<br>";
}

?>

</body>
</html>

Q9. Write a PHP program named states.py that declares a variable states with value
"Mississippi Alabama Texas Massachusetts Kansas". write a PHP program that does
the following:
a. Search for a word in variable states that ends in xas. Store this word in element 0 of a
list named statesList.

b. Search for a word in states that begins with k and ends in s. Perform a case-
insensitive comparison. [Note: Passing re.Ias a second parameter to method compile
performs a case-insensitive comparison.] Store this word in element1 of states List.

c. Search for a word in states that begins with M and ends in s. Store this word in
element 2 of the list.

d. Search for a word in states that ends in a. Store this word in element 3 of the list
<?php
$states_string = "Mississippi Alabama Texas Massachusetts Kansas";
$states = explode(" ", $states_string);
$patterns = array('/xas$/', '/^k.*s$/i', '/^M.*s$/', '/a$/');
$statesList = [];

function getState ($pattern, $states)


{ foreach ($states as $state) {
if (preg_match($pattern, $state)) {
return $state;
}
}
}

for ($i = 0; $i < count($patterns); $i++) {


$statesList[$i] = getState($patterns[$i], $states);
}
?>
<html>
<head>
</head>
<body>
<p>Statement Is : <b><?=$states_string;?></b></p>
<p>Name Ends With xas Is : <b><?=$statesList[0];?></b></p>
<p>Name Starts With k And Ends With s Is :
<b><?=$statesList[1];?></b></p>
<p>Name Starts With M And Ends With s Is :
<b><?=$statesList[2];?></b></p>
<p>Name Ends With a Is : <b><?=$statesList[3];?></b></p>
</body>
</html>
Q5- Design an XML document to store information about a student in an engineering college
affiliated to VTU. The information must include USN, Name, and Name of the College,
Programme, Year of Joining, and email id. Make up sample data for 3 students. Create a CSS style
sheet and use it to display the document.

XML Code

<?xml version="1.0" encoding="UTF-8"?>


<?xml-stylesheet type="text/css" href="lab5.css"?>
<VTUData>
<Stud>
<USN>USN: 4SO19CS121</USN>
<NAME>Name: Quenton Rebello</NAME>
<College>College: St Joseph Engineering College</College>
<Programme>Programme: B.E</Programme>
<YOJ>Year Joined: 2019</YOJ>
<Email>Email: [email protected]</Email>
</Stud>
<Stud>
<USN>USN: 4SO19CS177</USN>
<NAME>Name: Adam</NAME>
<College>College: St Joseph Engineering College</College>
<Programme>Programme: B.E</Programme>
<YOJ>Year Joined: 2019</YOJ>
<Email>Email: [email protected]</Email>
</Stud>
<Stud>
<USN>USN: 4SO19CS100</USN>
<NAME>Name: Noel</NAME>
<College>College: St Joseph Engineering College</College>
<Programme>Programme: B.E</Programme>
<YOJ>Year Joined: 2019</YOJ>
<Email>Email: [email protected]</Email>
</Stud>
<Stud>
<USN>USN: 4SO19CS155</USN>
<NAME>Name: Saul</NAME>
<College>College: St Joseph Engineering College</College>
<Programme>Programme: B.E</Programme>
<YOJ>Year Joined: 2019</YOJ>
<Email>Email: [email protected]</Email>
</Stud>
</VTUData>

CSS Code

*
{
display: block;
font-size:
15px;
}
USN
{
color:darkslategrey
; font-size: 25px;
margin-top: 10px;
}
Q10- Wíite a PHP píogíam to soít the student íecoíds which aíe stoíed in the database using
selection soít.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "data";
$conn = mysqli_connect($servername, $username, $password,
$dbname);if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM student";
$result = $conn->query($sql);
$usn = array() ;
echo "<table border='2'><caption>Before Sorting </caption><br>";
echo "<tr><th>USN</th><th>NAME</th><th>Marks</th></tr>";
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<tr><td>". $row["usn"]."</td>";
echo "<td>". $row["name"]."</td>";
echo "<td>". $row["marks"]."</td></tr>";
$usn[] = $row["usn"] ;
}
}
$n = sizeof($usn) ;
for($i = 0 ; $i < $n-1 ; $i++ )
{
$pos = $i ;
for($j = $i + 1 ; $j < $n ; $j++ )
{
if( $usn[$pos] < $usn[$j])
{
$pos = $j ;
}
}
if( $pos != $i)
{
$temp = $usn[$i] ;
$usn[$i] = $usn[$pos] ;
$usn[$pos] = $temp ;
}
}
$name = [] ;
$marks = [] ;
$result = $conn->query($sql);
if ($result->num_rows> 0)
{
while($row = $result->fetch_assoc())
{
for($i=0;$i<$n;$i++)
{
if($row["usn"] == $usn[$i])
{
$name[$i]=$row["name"];
$marks[$i]=$row["marks"];
}
}
}
}
echo "<br><table border='2'><caption>After Sorting
</caption><br>"; echo
"<tr><th>USN</th><th>NAME</th><th>Marks</th></tr>";
for($i = 0 ; $i < sizeof($usn) ; $i++)
{
echo "<tr><td>". $usn[$i]."</td>";
echo "<td>". $name[$i]."</td>";
echo "<td>". $marks[$i]."</td></tr>";
}
?>

You might also like