Why to study Web Technology ?

Learning Web Technology is crucial for engineering students because it equips them with essential skills to design and develop modern, user-friendly web applications. Understanding web technologies enables them to create interactive and responsive interfaces, integrate with back-end systems, and solve real-world problems. It also opens up diverse career opportunities in a rapidly growing industry, fostering innovation and adaptability in the ever-evolving digital landscape.

Laboratory Experiment - 8b

b. Develop a PHP program (with HTML/CSS) to sort the student records which are stored in the database using selection sort.

Key Components of the Program

1. Database Setup

  • A MySQL database named school is used, containing a table students.
  • The students table has fields such as id, name, and marks.
  • Sample SQL to create and populate the table:
CREATE DATABASE school;
USE school; CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), marks INT ); INSERT INTO students (name, marks) VALUES ('Alice', 85), ('Bob', 75), ('Charlie', 95), ('Diana', 65);

2. Selection Sort Algorithm

  • The Selection Sort algorithm is implemented in PHP to sort the records by marks.
  • The algorithm repeatedly selects the smallest element and places it at the correct position.

3. Program Flow

  1. Connect to the database.
  2. Fetch student records into an array.
  3. Apply the Selection Sort algorithm to sort the array by marks.
  4. Display the sorted records on a styled web page.

Program Implementation

Database Connection and Fetching Records

  • Connect to the MySQL database using mysqli.
  • Retrieve records using a SELECT query and store them in an array.

Sorting Logic

  • Use the Selection Sort algorithm to sort the array by marks.

Display with HTML/CSS

  • Display the sorted records in a styled table.

Program Code

<?php
// Database connection $servername = "localhost"; $username = "root"; $password = ""; $dbname = "school"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Fetch student records $sql = "SELECT * FROM students"; $result = $conn->query($sql); $students = []; if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { $students[] = $row; } } // Selection Sort Algorithm for ($i = 0; $i < count($students) - 1; $i++) { $minIndex = $i; for ($j = $i + 1; $j < count($students); $j++) { if ($students[$j]['marks'] < $students[$minIndex]['marks']) { $minIndex = $j; } } // Swap the records $temp = $students[$i]; $students[$i] = $students[$minIndex]; $students[$minIndex] = $temp; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Sorted Student Records</title> <style> body { font-family: Arial, sans-serif; background-color: #f4f4f9; margin: 0; padding: 0; } .container { width: 80%; margin: 50px auto; background: #fff; padding: 20px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); border-radius: 8px; } h1 { text-align: center; color: #333; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 10px; text-align: center; } th { background-color: #5a5af0; color: white; } tr:nth-child(even) { background-color: #f9f9f9; } tr:hover { background-color: #f1f1f1; } </style> </head> <body> <div class="container"> <h1>Sorted Student Records</h1> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Marks</th> </tr> </thead> <tbody> <?php foreach ($students as $student): ?> <tr> <td><?php echo $student['id']; ?></td> <td><?php echo $student['name']; ?></td> <td><?php echo $student['marks']; ?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </body> </html>

Output



Explanation

  1. Database Connection:

    • Connects to the school database using the mysqli object.
    • Terminates the script if the connection fails.
  2. Fetching Records:

    • Executes a SELECT query to fetch all student records.
    • Stores the records in an associative array for further processing.
  3. Sorting with Selection Sort:

    • Finds the minimum marks in the unsorted part of the array.
    • Swaps it with the first element of the unsorted part.
    • Repeats until the array is fully sorted.
  4. Displaying Records:

    • The sorted records are displayed in an HTML table.
    • CSS is used to style the table with alternating row colors and hover effects for better readability.
Viva-Voce Questions:

1. What is the purpose of this program?

  • Answer:
    The program retrieves student records from a database, sorts them using the selection sort algorithm (based on marks), and displays the sorted records on a web page with HTML and CSS.

2. What is selection sort, and how does it work?

  • Answer:
    Selection sort is a simple sorting algorithm that repeatedly selects the smallest (or largest) element from the unsorted portion of an array and places it in its correct position. It works as follows:
    • Start from the first element and find the smallest element in the array.
    • Swap it with the first element.
    • Repeat for the next position until the entire array is sorted.

3. Why did you choose selection sort for this program?

  • Answer:
    Selection sort was chosen because it is easy to understand and implement. Although not efficient for large datasets, it works well for small arrays like the student records used in this program.

4. How are the student records stored in the database?

  • Answer:
    The student records are stored in a MySQL table with columns for attributes like id, name, and marks. Each row represents a student's information.

5. What SQL query did you use to fetch the student records?

  • Answer:
    The SQL query used is:
    SELECT * FROM students;
    This query retrieves all columns and rows from the students table.

6. Why did you use an array to store the student records?

  • Answer:
    An array is used because it provides a convenient data structure for processing the records in PHP. It allows us to manipulate the data, such as sorting it using algorithms like selection sort.

7. Can this program handle large datasets efficiently? Why or why not?

  • Answer:
    No, this program is not efficient for large datasets because selection sort has a time complexity of O(n2)O(n^2), which makes it slow for large arrays. A more efficient sorting algorithm, such as quicksort or mergesort, could be used for better performance.

8. How did you connect the PHP program to the database?

  • Answer:
    The program connects to the database using the mysqli extension in PHP. A connection object is created with credentials (server name, username, password, database name), and the query() function is used to execute SQL commands.

9. What are the advantages of using a database over a flat file for storing records?

  • Answer:
    • Databases allow efficient querying and sorting of large datasets.
    • They support concurrent access and are more secure.
    • Databases provide data integrity and prevent data corruption.
    • They make it easier to scale the application.

10. How did you display the sorted records?

  • Answer:
    The sorted records are displayed in an HTML table using PHP to dynamically generate rows and columns. CSS is applied to style the table with features like alternating row colors and hover effects.

11. How can users change the sorting criterion, such as sorting by name instead of marks?

  • Answer:
    To allow users to change the sorting criterion, a dropdown or radio buttons can be added to the web page. Based on the user's selection, the PHP program can modify the sorting logic to sort by the chosen attribute (e.g., name or marks).

12. How does CSS enhance the user experience in this program?

  • Answer:
    CSS improves the visual appeal by:
    • Styling the table with borders, padding, and alternating row colors.
    • Adding hover effects to make the interface more interactive.
    • Ensuring the layout is responsive and easy to read on different devices.

13. What happens if the database connection fails?

  • Answer:
    If the database connection fails, the program will terminate, and an error message will be displayed. This is handled by checking the connection using the connect_error property of the mysqli object.

14. What improvements can you suggest for this program?

  • Answer:
    • Use a more efficient sorting algorithm, such as quicksort, for better performance.
    • Add user options to select the sorting attribute (e.g., name or marks).
    • Implement pagination for handling large datasets.
    • Replace the sorting logic in PHP with an SQL query that uses the ORDER BY clause for sorting directly in the database.

15. Why is sorting important in real-world applications?

  • Answer:
    Sorting is crucial because it organizes data for better readability, faster searching, and easier processing. In real-world applications, it is used in tasks like ranking students, arranging files, and optimizing database queries.

16. What are the limitations of this program?

  • Answer:
    • It uses selection sort, which is inefficient for large datasets.
    • Sorting is done in PHP instead of using SQL, which can be slower for large data.
    • There is no user input to customize the sorting order.
    • The program doesn’t handle edge cases like empty records or invalid data gracefully.
-:END of Program 8B:-

Comments

Popular posts from this blog

Why to study Web Technology ?