-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg10.php
More file actions
48 lines (46 loc) · 1.13 KB
/
pg10.php
File metadata and controls
48 lines (46 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
header('Content-Type: text/plain');
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "weblab";
$usnArray=[];
$nameArray= [];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); // die() prints msg and exit the program
}
$sql = "SELECT * FROM student";
$result = $conn->query($sql);
echo "Before sorting \n";
while($row = $result->fetch_assoc()){
echo $row["usn"];
echo " ";
echo $row["name"];
echo " ";
array_push($usnArray,$row["usn"]);
$nameArray[$row["usn"]] = $row["name"];
echo "\n";
}
echo "\nafter sorting \n";
$n=count($usnArray);
for ( $i = 0 ; $i < $n ; $i++ )
{
$pos= $i;
for ( $j = $i + 1 ; $j < $n ; $j++ ) {
if ( $usnArray[$j] < $usnArray[$pos] )
$pos= $j;
}
//swap
$temp = $usnArray[$i];
$usnArray[$i] = $usnArray[$pos];
$usnArray[$pos] = $temp;
}
foreach( $usnArray as $value ){
echo($value);
echo" ";
echo $nameArray[$value];
echo"\n";
}
$conn->close();
?>