Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <body>
- <a href="create.php">Add new student</a>
- <?php
- $servername = "localhost:3306";
- $username = "root";
- $password = "";
- $dbname = "university";
- $conn = null;
- try {
- $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
- } catch (PDOException $e) {
- die('Connection failed: ' . $e->getMessage());
- }
- function displayStudents($groupId) {
- global $conn;
- $queryStudents = $conn->prepare("SELECT first_name, last_name, faculty_number FROM students WHERE group_id = :groupId ORDER BY first_name, last_name");
- $queryStudents->bindParam(':groupId', $groupId);
- $queryStudents->execute();
- if ($queryStudents->rowCount() > 0) {
- echo '<ol>';
- while($student_row = $queryStudents->fetch(PDO::FETCH_ASSOC)) {
- echo '<li>'.$student_row["first_name"].' '.$student_row["last_name"].': '.$student_row["faculty_number"].'</li>' ;
- }
- echo '</ol>';
- } else {
- echo "<p style='color: red;'>No students</p>";
- }
- }
- function displayGroups($majorId) {
- global $conn;
- $queryGroups = $conn->prepare("SELECT id, name FROM groups WHERE major_id = :majorId");
- $queryGroups->bindParam(':majorId', $majorId);
- $queryGroups->execute();
- if ($queryGroups->rowCount() > 0) {
- while($group_row = $queryGroups->fetch(PDO::FETCH_ASSOC)) {
- echo '<p>'.$group_row["name"].'</p>' ;
- displayStudents($group_row["id"]);
- }
- } else {
- echo "<p>No groups</p>";
- }
- }
- function displayMajors() {
- global $conn;
- $queryMajors = "SELECT id, name, academic_degree, start_year FROM majors";
- $resultMajors = $conn->query($queryMajors);
- if ($resultMajors->rowCount() > 0) {
- while($major_row = $resultMajors->fetch(PDO::FETCH_ASSOC)) {
- echo '<h1>'.$major_row["name"].': '.$major_row["start_year"].' ('.$major_row["academic_degree"].')</h1>' ;
- displayGroups($major_row["id"]);
- }
- } else {
- echo "<h1>No majors</h1>";
- }
- }
- displayMajors();
- $conn = null;
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement