-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path10.SelectionSort.php
More file actions
85 lines (78 loc) · 1.84 KB
/
Copy path10.SelectionSort.php
File metadata and controls
85 lines (78 loc) · 1.84 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<!--
10. Write a PHP program to sort the student records which are stored in the database
using selection sort.
-->
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$domain = "localhost";
$username = "root";
$password = "";
$dbname = "weblab";
$a = [];
$conn = mysqli_connect($domain, $username, $password, $dbname);
if ($conn->connect_error)
die("Connection failed: " . $conn->connect_error);
$sql = "SELECT * FROM student";
$result = $conn->query($sql);
echo "<br>";
echo "<h2>BEFORE SORTING</h2>";
echo "<table border='2'>";
echo "<tr>";
echo "<th>USN</th><th>Name</th><th>Address</th></tr>";
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["usn"] . "</td>";
echo "<td>" . $row["name"] . "</td>";
echo "<td>" . $row["address"] . "</td></tr>";
array_push($a, $row["usn"]);
}
} else
echo "Table is Empty";
echo "</table>";
$n = count($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) {
while ($row = $result->fetch_assoc()) {
for ($i = 0; $i < $n; $i++) {
if ($row["usn"] == $a[$i]) {
$c[$i] = $row["name"];
$d[$i] = $row["address"];
}
}
}
}
echo "<br>";
echo "<h2>AFTER SORTING</h2>";
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>