0% found this document useful (0 votes)
28 views9 pages

MVC Pajn

Uploaded by

Par Veen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views9 pages

MVC Pajn

Uploaded by

Par Veen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Certainly!

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.

First, let's set up the directory structure:

```
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
```

Now, let's create each file:

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');

$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
```

2. models/Student.php

```php
<?php
class Student {
private $conn;

public function __construct($db) {


$this->conn = $db;
}

public function getAll() {


$query = "SELECT * FROM students";
$result = $this->conn->query($query);
return $result->fetch_all(MYSQLI_ASSOC);
}

public function getById($id) {


$query = "SELECT * FROM students WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}

public function create($data) {


$query = "INSERT INTO students (enrolment_no, name, semester, gender,
qualification) VALUES (?, ?, ?, ?, ?)";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ssiss", $data['enrolment_no'], $data['name'],
$data['semester'], $data['gender'], $data['qualification']);
return $stmt->execute();
}

public function update($id, $data) {


$query = "UPDATE students SET enrolment_no = ?, name = ?, semester = ?,
gender = ?, qualification = ? WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("ssissi", $data['enrolment_no'], $data['name'],
$data['semester'], $data['gender'], $data['qualification'], $id);
return $stmt->execute();
}

public function delete($id) {


$query = "DELETE FROM students WHERE id = ?";
$stmt = $this->conn->prepare($query);
$stmt->bind_param("i", $id);
return $stmt->execute();
}
}
```

3. controllers/StudentController.php

```php
<?php
require_once 'models/Student.php';

class StudentController {
private $student;

public function __construct($db) {


$this->student = new Student($db);
}

public function index() {


$students = $this->student->getAll();
require 'views/student/index.php';
}

public function create() {


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = [
'enrolment_no' => $_POST['enrolment_no'],
'name' => $_POST['name'],
'semester' => $_POST['semester'],
'gender' => $_POST['gender'],
'qualification' => $_POST['qualification']
];
if ($this->student->create($data)) {
header('Location: index.php?action=index');
} else {
$error = "Error creating student";
}
}
require 'views/student/create.php';
}

public function edit($id) {


$student = $this->student->getById($id);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$data = [
'enrolment_no' => $_POST['enrolment_no'],
'name' => $_POST['name'],
'semester' => $_POST['semester'],
'gender' => $_POST['gender'],
'qualification' => $_POST['qualification']
];
if ($this->student->update($id, $data)) {
header('Location: index.php?action=index');
} else {
$error = "Error updating student";
}
}
require 'views/student/edit.php';
}

public function delete($id) {


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($this->student->delete($id)) {
header('Location: index.php?action=index');
} else {
$error = "Error deleting student";
}
}
$student = $this->student->getById($id);
require 'views/student/delete.php';
}
}
```

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>&copy; 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>

<button type="submit">Add Student</button>


</form>
```

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>

<button type="submit">Update Student</button>


</form>
```

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';

$controller = new StudentController($conn);

$action = isset($_GET['action']) ? $_GET['action'] : 'index';

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, table td {


border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

table th {
background-color: #f2f2f2;
}

form {
max-width: 500px;
margin: 0 auto;
}

form label {
display: block;
margin-top: 1rem;
}

form input, form select {


width: 100%;
padding: 0.5rem;
margin-top: 0.25rem;
}

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/`.

This application follows the MVC architecture:

- Model: The `Student` class in `models/Student.php` handles all database


operations.
- View: The files in the `views` folder contain the HTML templates for displaying
data and forms.
- Controller: The `StudentController` class in `controllers/StudentController.php`
manages the flow between the Model and View.

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

You might also like