Display Students
On this step we shall now implement the CRUD operation for the students table.
Display students on a table
Add the following script on your Php portion to initialize query for fetching all students
php
// FETCH STUDENTS
$students = [];
if ($section === 'students') {
$stmt = $pdo->query("
SELECT *
FROM students
ORDER BY student_id DESC
");
$students = $stmt->fetchAll();
}
HTML section add this within the
html
<?php if($section === 'books'): ?>
<h1>Books</h1>
<!-- Insert Table Here-->
<?php endif;?>
php
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Course</th>
<th>Created at</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($students as $student): ?>
<tr>
<td>
<?= htmlspecialchars($student['student_id']) ?>
</td>
<td>
<?= htmlspecialchars($student['student_first_name']) ?>
</td>
<td>
<?= htmlspecialchars($student['student_last_name']) ?>
</td>
<td>
<?= htmlspecialchars($student['student_course']) ?>
</td>
<td>
<?= htmlspecialchars($student['student_created_at']) ?>
</td>
<td>
<a>Edit</a>
|
<a>Delete</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
$students variable and iterated on a $student variable.
Sign in to save your progress permanently.
Progress is saved in your browser session
Step 5 of 5