Guest User

Untitled

a guest
Oct 26th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <style>
  5. table, td, th
  6. {
  7. border: 1px solid black;
  8. width: 33%;
  9. text-align: center;
  10. background-color:lightblue;
  11. }
  12. </style>
  13.  
  14. <?php
  15. $servername = "localhost";
  16. $username = "root";
  17. $password = "";
  18. $dbname = "UP";
  19. $a=array();
  20.  
  21. // Create connection
  22. // Opens a new connection to the MySQL server
  23.  
  24. $conn = mysqli_connect($servername, $username, $password, $dbname);
  25.  
  26. if ($conn->connect_error)
  27.  
  28. die("Connection failed: " . $conn->connect_error);
  29.  
  30. $sql = "SELECT * FROM student";
  31. // performs a query against the database
  32. $result = $conn->query($sql);
  33.  
  34. echo "<br>";
  35. echo "<center> BEFORE SORTING </center>";
  36. echo "<table align='center'>";
  37. echo "<tr>";
  38.  
  39. echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";
  40. if ($result->num_rows> 0)
  41. {
  42. while($row = $result->fetch_assoc())
  43. {
  44. echo "<tr>";
  45. echo "<td>". $row["usn"]."</td>";
  46. echo "<td>". $row["name"]."</td>";
  47. echo "<td>". $row["address"]."</td></tr>";
  48. array_push($a,$row["usn"]);
  49. }
  50. }
  51. else
  52. echo "Table is Empty";
  53. echo "</table>";
  54. $n=count($a);
  55.  
  56. $b=$a;
  57. for ( $i = 0 ; $i< ($n - 1) ; $i++ )
  58. {
  59. $pos= $i;
  60. for ( $j = $i + 1 ; $j < $n ; $j++ )
  61. {
  62. if ( $a[$pos] > $a[$j] )
  63. $pos= $j;
  64. }
  65.  
  66. if ( $pos!= $i )
  67. {
  68. $temp=$a[$i];
  69. $a[$i] = $a[$pos];
  70. $a[$pos] = $temp;
  71. }
  72. }
  73.  
  74. $c=array();
  75. $d=array();
  76. $result = $conn->query($sql);
  77.  
  78. if ($result->num_rows> 0)// output data of each row
  79. {
  80. while($row = $result->fetch_assoc()) {
  81.  
  82. for($i=0;$i<$n;$i++) {
  83. if($row["usn"]== $a[$i]) {
  84. $c[$i]=$row["name"];
  85. $d[$i]=$row["address"];
  86. }
  87. }
  88. }
  89. }
  90. echo "<br>";
  91. echo "<center> AFTER SORTING <center>";
  92. echo "<table border='2'>";
  93. echo "<tr>";
  94.  
  95. echo "<th>USN</th><th>NAME</th><th>Address</th></tr>";
  96. for($i=0;$i<$n;$i++)
  97. {
  98. echo "<tr>";
  99. echo "<td>". $a[$i]."</td>";
  100. echo "<td>". $c[$i]."</td>";
  101. echo "<td>". $d[$i]."</td></tr>";
  102. }
  103. echo "</table>";
  104. $conn->close();
  105. ?>
  106. </body>
  107. </html>
Add Comment
Please, Sign In to add comment