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 tablestudents
. - The
students
table has fields such asid
,name
, andmarks
. - Sample SQL to create and populate the table:
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
- Connect to the database.
- Fetch student records into an array.
- Apply the Selection Sort algorithm to sort the array by marks.
- 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
Output
Explanation
Database Connection:
- Connects to the
school
database using themysqli
object. - Terminates the script if the connection fails.
- Connects to the
Fetching Records:
- Executes a
SELECT
query to fetch all student records. - Stores the records in an associative array for further processing.
- Executes a
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.
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:
Comments
Post a Comment