Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <a href="create.php">Add new student</a>
  6.  
  7. <?php
  8. $servername = "localhost:3306";
  9. $username = "root";
  10. $password = "";
  11. $dbname = "university";
  12.  
  13. $conn = null;
  14. try {
  15. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  16. } catch (PDOException $e) {
  17. die('Connection failed: ' . $e->getMessage());
  18. }
  19.  
  20. function displayStudents($groupId) {
  21. global $conn;
  22. $queryStudents = $conn->prepare("SELECT first_name, last_name, faculty_number FROM students WHERE group_id = :groupId ORDER BY first_name, last_name");
  23. $queryStudents->bindParam(':groupId', $groupId);
  24. $queryStudents->execute();
  25.  
  26. if ($queryStudents->rowCount() > 0) {
  27. echo '<ol>';
  28. while($student_row = $queryStudents->fetch(PDO::FETCH_ASSOC)) {
  29. echo '<li>'.$student_row["first_name"].' '.$student_row["last_name"].': '.$student_row["faculty_number"].'</li>' ;
  30. }
  31. echo '</ol>';
  32. } else {
  33. echo "<p style='color: red;'>No students</p>";
  34. }
  35. }
  36.  
  37. function displayGroups($majorId) {
  38. global $conn;
  39. $queryGroups = $conn->prepare("SELECT id, name FROM groups WHERE major_id = :majorId");
  40. $queryGroups->bindParam(':majorId', $majorId);
  41. $queryGroups->execute();
  42.  
  43. if ($queryGroups->rowCount() > 0) {
  44. while($group_row = $queryGroups->fetch(PDO::FETCH_ASSOC)) {
  45. echo '<p>'.$group_row["name"].'</p>' ;
  46. displayStudents($group_row["id"]);
  47. }
  48. } else {
  49. echo "<p>No groups</p>";
  50. }
  51. }
  52.  
  53. function displayMajors() {
  54. global $conn;
  55. $queryMajors = "SELECT id, name, academic_degree, start_year FROM majors";
  56. $resultMajors = $conn->query($queryMajors);
  57.  
  58. if ($resultMajors->rowCount() > 0) {
  59. while($major_row = $resultMajors->fetch(PDO::FETCH_ASSOC)) {
  60. echo '<h1>'.$major_row["name"].': '.$major_row["start_year"].' ('.$major_row["academic_degree"].')</h1>' ;
  61. displayGroups($major_row["id"]);
  62. }
  63. } else {
  64. echo "<h1>No majors</h1>";
  65. }
  66. }
  67.  
  68. displayMajors();
  69.  
  70. $conn = null;
  71. ?>
  72.  
  73.  
  74. </body>
  75. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement