Skip to content

Student Listing: Adding Navigation Menu - Updating Pages to use Nav Menu

We need to modify the following pages to use the new navigation menu:

  • index.php
  • studentdetails.php
  • unauthorizedaccess.php
  • signup.php
  • login.php
  • addstudent.php
  • removestudent.php
  • editstudent.php

Let's take them one at a time and I'll point out additional changes we will make as appropriate.

index.php - Student Listing: Modify to use Nav Menu

The first thing we want to do is open the index.php script and include the pagetitle.php script at the top of the script:

1
2
3
4
5
6
<?php
  require_once('pagetitles.php');
  $page_title = SL_HOME_PAGE;
?>
<!DOCTYPE html>
<html>

Next we need to make the following changes for displaying the <title>:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  Unchanged Lines
    <html>
      <head>
        <link ...>
- Removed Line
-       <title>Student Listing</title>
+ Added Line
+       <title><?= $page_title ?></title>
  Unchanged Lines
      </head>
  <body>

Next, right below the <body> element, include the navmenu.php script:

1
2
3
4
5
6
<body>
<?php
  require_once('navmenu.php');
?>
  <div class="card">
    <div class="card-body">

Since We already have the functionality to add a student from the navbar when a user with administrative privileges is logged in, let’s remove the link to add a new student, just below the <div class="card-body"> line and replace our header with $page_title :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  Unchanged Lines
    <div class="card">
      <div class="card-body">
- Removed Lines
-       <h1>Students</h1>
-       <p class='nav-link'>If you have a student you would like to include, 
-         feel free to <a href='addstudent.php'> add one</a></p>
+ Added Line
+       <h1><?= $page_title ?></h1>
  Unchanged Lines
        <?php
            require_once('dbconnection.php');

Currently, we always show the trashcan icon for each student row:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
while($row = mysqli_fetch_assoc($result))
{
    ...

    echo "<tr><td>"
        . "<img src=" . $student_image_file . " class='img-thumbnail'"
        . " style='max-height: 75px;' alt='Student image'></td>"
        . "<td><a class='nav-link' href='studentdetails.php?id=" . $row['id'] . "'>"
        . $row['first_name'] . ' ' . $row['last_name'] . "</a></td>"
        . "<td><a class='nav-link' href='removestudent.php?id_to_delete="
        . $row['id'] ."'><i class='fas fa-trash-alt'></i></a></td></tr>";
}

We need to modify the logic to only show the trashcan link if someone is logged in with administrative privilieges. Therefore, make the following modifications:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  Unchanged Lines
    while($row = mysqli_fetch_assoc($result))
    {
        $student_image_file = $row['image_file'];

        if (empty($student_image_file))
        {
            $student_image_file = SL_UPLOAD_PATH . SL_DEFAULT_STUDENT_FILE_NAME;
        }

- Removed Lines
-       echo "<tr><td>"
-           . "<img src=" . $student_image_file . " class='img-thumbnail'"
-           . " style='max-height: 75px;' alt='Student image'></td>"
-           . "<td><a class='nav-link' href='studentdetails.php?id=" . $row['id'] . "'>"
-           . $row['first_name'] . ' ' . $row['last_name'] . "</a></td>"
-           . "<td><a class='nav-link' href='removestudent.php?id_to_delete="
-           . $row['id'] ."'><i class='fas fa-trash-alt'></i></a></td></tr>";
+ Added Line
+       $student_row = "<tr><td>"
+               . "<img src=" . $student_image_file . " class='img-thumbnail'"
+               . " style='max-height: 75px;' alt='Student image'></td>"
+               . "<td><a class='nav-link' href='studentdetails.php?id=" . $row['id'] . "'>"
+               . $row['first_name'] . ' ' . $row['last_name'] . "</a></td>";
+
+       if (isset($_SESSION['user_access_privileges'])
+               && $_SESSION['user_access_privileges'] == 'admin')
+       {
+           $student_row .=  "<td><a class='nav-link' href='removestudent.php?id_to_delete="
+                   . $row['id'] ."'><i class='fas fa-trash-alt'></i></a></td></tr>";
+       }
+
+       $student_row .= "</tr>";
+
+       echo $student_row;
  Unchanged Line
    }

studentdetails.php - Student Listing: Modify to use Nav Menu

Again, we want to include the pagetitle.php script at the top of the script. Open up studentdetails.php and add the following code to the to top of the script:

1
2
3
4
5
6
<?php
  require_once('pagetitles.php');
  $page_title = SL_DETAILS_PAGE;
?>
<!DOCTYPE html>
<html>

Next we need to make the following changes for displaying the <title>:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  Unchanged Lines
    <html>
      <head>
        <link ...>
- Removed Line
-       <title>Student Details</title>
+ Added Line
+       <title><?= $page_title ?></title>
  Unchanged Lines
      </head>
  <body>

Since We already have the functionality to link back to the home page from the navbar, let’s remove the link to index.php, just below the <div class="card-body"> line and replace it with a header with the title set in $page_title, and a horizontal line:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  Unchanged Lines
    <div class="card">
      <div class="card-body">
- Removed Lines
-       <nav class="nav">
-           <a class="nav-link" href="index.php">Students</a>
-       </nav>
+ Added Lines
+       <h1><?= $page_title ?></h1>
+       <hr/>
   Unchanged Lines
        <?php
            require_once('dbconnection.php');

Now let's only show the link to edit a student if the user has admin privileges or if the user is this student. Add the following code starting right after the second closing </div> tag:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    </table>
  </div>
</div>
    <?php
            if (isset($_SESSION['user_access_privileges'], $_SESSION['user_student_listing_id'])
                    && ($_SESSION['user_access_privileges'] == 'admin'
                            || $_SESSION['user_student_listing_id'] == $row['id'])):
    ?>
<hr/>
<p>If you would like to change any of the details of this student, feel free to <a href='editstudent.php?id_to_edit=<?=$row['id']?>'> edit it</a></p>
    <?php
            endif;
        else:
    ?>

unauthorizedaccess.php - Student Listing: Modify to use Nav Menu

All we need to do for the unauthorizedaccess.php script is to included the navmenu.php script right after the opening <body> tag:

1
2
3
4
5
6
7
<body>
<?php
  require_once('navmenu.php');
?>
  <div class="card">
    <div class="card-body">
      <h3>You do not have access to this page.</h3>

signup.php - Student Listing: Modify to use Nav Menu

Again, all we need to do for the signup.php script is to include the navmenu.php script right after the opening <body> tag:

1
2
3
4
5
6
7
<body>
<?php
  require_once('navmenu.php');
?>
  <div class="card">
    <div class="card-body">
      <h1><?= $page_title ?></h1>

login.php - Student Listing: Modify to use Nav Menu

Again, all we need to do for the login.php script is to include the navmenu.php script right after the opening <body> tag:

1
2
3
4
5
6
7
<body>
<?php
  require_once('navmenu.php');
?>
  <div class="card">
    <div class="card-body">
      <h1><?= $page_title ?></h1>

addstudent.php - Student Listing: Modify to use Nav Menu

Open up addstudent.php. First, let's include the navmenu.php script right below the opening <body> element:

1
2
3
4
  <body>
  <?php
    require_once('navmenu.php');
  ?>

Since We already have the functionality to link back to the home page from the navbar, let’s remove the link to index.php, just below the <div class="card-body"> line:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  Unchanged Lines
    <div class="card">
      <div class="card-body">
- Removed Lines
-       <nav class="nav">
-           <a class="nav-link" href="index.php">Students</a>
-       </nav>
   Unchanged Lines
        <h1><?= $page_title ?></h1>
        <hr/>
        <?php
            $display_add_student_form = true;

removestudent.php - Student Listing: Modify to use Nav Menu

Open up removestudent.php. All we need to do is include the navmenu.php script right below the opening <body> element:

1
2
3
4
  <body>
  <?php
    require_once('navmenu.php');
  ?>

editstudent.php - Student Listing: Modify to use Nav Menu

Open up editstudent.php. First, let's include the navmenu.php script right below the opening <body> element:

1
2
3
4
  <body>
  <?php
    require_once('navmenu.php');
  ?>

Since We already have the functionality to link back to the home page from the navbar, let’s remove the link to index.php, just below the <div class="card-body"> line:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  Unchanged Lines
    <div class="card">
      <div class="card-body">
- Removed Lines
-       <nav class="nav">
-           <a class="nav-link" href="index.php">Students</a>
-       </nav>
   Unchanged Lines
        <h1><?= $page_title ?></h1>
        <hr/>
        <?php
            require_once('dbconnection.php');