bubble_chart StackLab

CRUD Web Application Using Php Data Objects

5 steps · pending Not started · person_outline Guest
arrow_back Back

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
code php
// FETCH STUDENTS
$students = [];
if ($section === 'students') {

    $stmt = $pdo->query("
        SELECT *
        FROM students
        ORDER BY student_id DESC
    ");

    $students = $stmt->fetchAll();
}
For the HTML section add this within the
code html
<?php if($section === 'books'): ?>
    <h1>Books</h1>
    <!-- Insert Table Here-->
<?php endif;?>
tags
code 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>
The resulting view shall render the queried data set that was stored within the $students variable and iterated on a $student variable.

login Sign in to save your progress permanently. info Progress is saved in your browser session
format_list_numbered Step 5 of 5