Professional Documents
Culture Documents
MVC Pajn
MVC Pajn
I'll create a detailed web application using PHP, HTML, and MySQL for
managing student information, following the MVC (Model-View-Controller)
architecture. I'll break down the code into separate files and explain each part.
```
student_management/
├── config/
│ └── database.php
├── controllers/
│ └── StudentController.php
├── models/
│ └── Student.php
├── views/
│ ├── layout.php
│ └── student/
│ ├── index.php
│ ├── create.php
│ ├── edit.php
│ └── delete.php
├── public/
│ ├── index.php
│ └── css/
│ └── style.css
└── .htaccess
```
1. config/database.php
```php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'your_username');
define('DB_PASS', 'your_password');
define('DB_NAME', 'student_management');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
```
2. models/Student.php
```php
<?php
class Student {
private $conn;
3. controllers/StudentController.php
```php
<?php
require_once 'models/Student.php';
class StudentController {
private $student;
4. views/layout.php
```php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Management System</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<h1>Student Management System</h1>
<nav>
<ul>
<li><a href="index.php?action=index">Home</a></li>
<li><a href="index.php?action=create">Add Student</a></li>
</ul>
</nav>
</header>
<main>
<?php include $content; ?>
</main>
<footer>
<p>© 2024 Student Management System</p>
</footer>
</body>
</html>
```
5. views/student/index.php
```php
<?php $content = 'views/student/index.php'; ?>
<h2>Student List</h2>
<table>
<thead>
<tr>
<th>Enrolment No.</th>
<th>Name</th>
<th>Semester</th>
<th>Gender</th>
<th>Qualification</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($students as $student): ?>
<tr>
<td><?php echo $student['enrolment_no']; ?></td>
<td><?php echo $student['name']; ?></td>
<td><?php echo $student['semester']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['qualification']; ?></td>
<td>
<a href="index.php?action=edit&id=<?php echo $student['id']; ?
>">Edit</a>
<a href="index.php?action=delete&id=<?php echo
$student['id']; ?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
```
6. views/student/create.php
```php
<?php $content = 'views/student/create.php'; ?>
<h2>Add Student</h2>
<?php if (isset($error)): ?>
<p class="error"><?php echo $error; ?></p>
<?php endif; ?>
<form action="index.php?action=create" method="post">
<label for="enrolment_no">Enrolment No:</label>
<input type="text" id="enrolment_no" name="enrolment_no" required>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="semester">Semester:</label>
<input type="number" id="semester" name="semester" required>
<label for="gender">Gender:</label>
<select id="gender" name="gender" required>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
<label for="qualification">Qualification:</label>
<input type="text" id="qualification" name="qualification" required>
7. views/student/edit.php
```php
<?php $content = 'views/student/edit.php'; ?>
<h2>Edit Student</h2>
<?php if (isset($error)): ?>
<p class="error"><?php echo $error; ?></p>
<?php endif; ?>
<form action="index.php?action=edit&id=<?php echo $student['id']; ?>"
method="post">
<label for="enrolment_no">Enrolment No:</label>
<input type="text" id="enrolment_no" name="enrolment_no" value="<?php echo
$student['enrolment_no']; ?>" required>
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<?php echo $student['name']; ?
>" required>
<label for="semester">Semester:</label>
<input type="number" id="semester" name="semester" value="<?php echo
$student['semester']; ?>" required>
<label for="gender">Gender:</label>
<select id="gender" name="gender" required>
<option value="Male" <?php echo $student['gender'] === 'Male' ?
'selected' : ''; ?>>Male</option>
<option value="Female" <?php echo $student['gender'] === 'Female' ?
'selected' : ''; ?>>Female</option>
<option value="Other" <?php echo $student['gender'] === 'Other' ?
'selected' : ''; ?>>Other</option>
</select>
<label for="qualification">Qualification:</label>
<input type="text" id="qualification" name="qualification" value="<?php echo
$student['qualification']; ?>" required>
8. views/student/delete.php
```php
<?php $content = 'views/student/delete.php'; ?>
<h2>Delete Student</h2>
<?php if (isset($error)): ?>
<p class="error"><?php echo $error; ?></p>
<?php endif; ?>
<p>Are you sure you want to delete the following student?</p>
<p><strong>Name:</strong> <?php echo $student['name']; ?></p>
<p><strong>Enrolment No:</strong> <?php echo $student['enrolment_no']; ?></p>
<form action="index.php?action=delete&id=<?php echo $student['id']; ?>"
method="post">
<button type="submit">Confirm Delete</button>
</form>
<a href="index.php?action=index">Cancel</a>
```
9. public/index.php
```php
<?php
require_once '../config/database.php';
require_once '../controllers/StudentController.php';
switch ($action) {
case 'index':
$controller->index();
break;
case 'create':
$controller->create();
break;
case 'edit':
$id = isset($_GET['id']) ? $_GET['id'] : null;
if ($id) {
$controller->edit($id);
} else {
header('Location: index.php?action=index');
}
break;
case 'delete':
$id = isset($_GET['id']) ? $_GET['id'] : null;
if ($id) {
$controller->delete($id);
} else {
header('Location: index.php?action=index');
}
break;
default:
header('Location: index.php?action=index');
break;
}
```
10. public/css/style.css
```css
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
}
header {
background-color: #333;
color: #fff;
padding: 1rem;
}
header h1 {
margin: 0;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 10px;
}
nav ul li a {
color: #fff;
text-decoration: none;
}
main {
padding: 2rem;
}
table {
width: 100%;
border-collapse: collapse;
}
table th {
background-color: #f2f2f2;
}
form {
max-width: 500px;
margin: 0 auto;
}
form label {
display: block;
margin-top: 1rem;
}
form button {
display: block;
width: 100%;
padding: 0.5rem;
margin-top: 1rem;
background-color: #333;
color: #fff;
border: none;
cursor: pointer;
}
.error {
color: red;
font-weight: bold;
}
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 1rem;
position: fixed;
bottom: 0;
width: 100%;
}
```
11. .htaccess
```
RewriteEngine On
RewriteBase /student_management/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
```
Now, let's explain how to run this application:
1. Set up a local web server (e.g., Apache) with PHP and MySQL support.
2. Create a new MySQL database named "student_management".
3. Create a "students" table in the database with the following structure:
```sql
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
enrolment_no VARCHAR(20) NOT NULL,
name VARCHAR(100) NOT NULL,
semester INT NOT NULL,
gender ENUM('Male', 'Female', 'Other') NOT NULL,
qualification VARCHAR(100) NOT NULL
);
```
4. Update the database connection details in `config/database.php`.
5. Place the entire `student_management` folder in your web server's document root.
6. Access the application through your web browser, e.g.,
`http://localhost/student_management/public/`.
The `public/index.php` file serves as the entry point for all requests, routing
them to the appropriate controller action based on the URL parameters.
This structure