10 - WRITE A PHP PROGRAM TO SORT THE STUDENT RECORDS WHICH ARE STORED IN THE DATABASE USING SELECTION SORT.
SOLUTION 1
Requires XAMPP control panel for Solution 1
prog10.php
<?php
CREATE TABLE `student` (
`usn` varchar(20),
`name` varchar(20),
`marks` int(11)
);
INSERT INTO `student` (`usn`, `name`, `marks`) VALUES
('3GN16cs044', 'Nisha', 67),
('3GN16cs030', 'Manoj', 87),
('3GN16cs002', 'Abhishek', 97),
('3GN16cs014', 'Dhilip', 77),
('3GN16cs023', 'Deepika', 57);
SOLUTION 1
Requires XAMPP control panel for Solution 1
prog10.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "weblab";
$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>";
}
?>
weblab.sql
CREATE TABLE `student` (
`usn` varchar(20),
`name` varchar(20),
`marks` int(11)
);
INSERT INTO `student` (`usn`, `name`, `marks`) VALUES
('3GN16cs044', 'Nisha', 67),
('3GN16cs030', 'Manoj', 87),
('3GN16cs002', 'Abhishek', 97),
('3GN16cs014', 'Dhilip', 77),
('3GN16cs023', 'Deepika', 57);
OUTPUT :( click on image to zoom )
SOLUTION 2
Goto Mysql and then type
create database weblab;
use weblab;
create table student(usnvarchar(10),name varchar(20),address varchar(20));
program10.php
<!DOCTYPE html>
<html>
<body>
<style>
table, td, th
{
border: 1px solid black;
width: 33%;
text-align: center;
border-collapse:collapse;
background-color:lightblue;
}
table { margin: auto; }
</style>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "weblab";
$a=[];
// Create connection
// Opens a new connection to the MySQL server
$conn = mysqli_c onnect($servername, $username, $password, $dbname);
// Check connection and return an error description from the
last connection error, if any
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
$sql = "SELECT * FROM student";
// performs a query against the
database $result = $conn->query($sql);
echo "<br>";
echo "<center> BEFORE SORTING </center>";
echo "<table border='2'>";
echo "<tr>";
echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";
if ($result->num_rows> 0)
{
// output data of each row and fetches a result row as an
associative array
while($row = $result->fetch_assoc()){
echo "<tr>";
echo "<td>". $row["usn"]."</td>";
echo "<td>". $row["name"]."</td>";
echo "<td>". $row["addr"]."</td></tr>";
array_push($a,$row["usn"]);
}
}
else
echo "Table is Empty";
echo "</table>";
$n=count($a);
$b=$a;
for ( $i = 0 ; $i< ($n - 1) ; $i++ )
{
$pos= $i;
for ( $j = $i + 1 ; $j < $n ; $j++ ) {
if ( $a[$pos] > $a[$j] )
$pos= $j;
}
if ( $pos!= $i ) {
$temp=$a[$i];
$a[$i] = $a[$pos];
$a[$pos] = $temp;
}
}
$c=[];
$d=[];
$result = $conn->query($sql);
if ($result->num_rows> 0)// output data of each row
{
while($row = $result->fetch_assoc()) {
for($i=0;$i<$n;$i++) {
if($row["usn"]== $a[$i]) {
$c[$i]=$row["name"];
$d[$i]=$row["addr"];
}
}
}
}
echo "<br>";
echo "<center> AFTER SORTING <center>";
echo "<table border='2'>";
echo "<tr>";
echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";
for($i=0;$i<$n;$i++) {
echo "<tr>";
echo "<td>". $a[$i]."</td>";
echo "<td>". $c[$i]."</td>";
echo "<td>". $d[$i]."</td></tr>";
}
echo "</table>";
$conn->close();
?>
</body>
</html>
OUTPUT :( click on image to zoom )
Great Work Man !! Thank you so Much !
ReplyDelete