The administration menu

See more about:

Where are we?

You know how to add records, and show them. What about maintaining the data? How do we let people edit and delete records?

That’s what we look at next. But we start with the administration menu. It lets users select the records they want to edit or delete.

This lesson’s goals

Learn that:

  • Our applications’ sites have two parts: one for regular users, and one for administration.
  • The admin menu lists items (products or articles), with Edit and Delete links for each.

The administration section

Both DogToys and DogRock have two parts to their sites:

  • The public part, with product and article lists.
  • The adminstration part.

The pages for the administration part of the site are all in the admin directory. Here is the layout of the DogToys site:

DogToys directory layout

Figure 1. DogToys directory layout

The administration menu (admin/index.php) gives users access to the all of the administrative functions. This is what it looks like:

DogToys administration menu

Figure 2. DogToys administration menu

At the top is a link to add a new record. At the bottom is a list of existing records, with Edit and Delete links. This is much like the list of products on the product catalog, but with some extra stuff.

Let’s see how this works.

DogToys administration menu

Here’s the code. I’ve included all the sorting and error checking.

<p>What do you want to do?</p>
<blockquote>
  <p><a href="add-product.php">Add a new product</a></p>
</blockquote>
<h2>Current products</h2>
<?php
//List the current products, along with edit/delete action for each one.
//Connect to DB.
require $path_to_root . '/library/db-connect.php';
$db = new mysqli($host, $user_name, $password, $db);
if ( mysqli_connect_error() ) {
  print '<p>Error! Could not connect to the database. ';
  print 'Error message: '.mysqli_connect_error().'</p>';
  exit();
}
//Fetch product data.
$query = "select product_id, name, description,
      image_file_name, price
    from products
    order by ";
//Get the product list order, if given.
$order = $_GET['order'];
if ( $order == 'price_asc' ) {
  $query .= 'price asc';
}
else if ( $order == 'price_desc' ) {
  $query .= 'price desc';
}
else {
  //Default sort order.
  $query .= 'name';
}
$record_set = $db->query($query);
if ( $db->error != '' ) {
  print '<p>SQL error! Message: ' . $db->error . '</p>';
  print "<p>Query:</p>
    <blockquote>
    $query
  </blockquote>";
  exit();
}
//Start the product table.
?>
<table cellpadding="5" cellspacing="0" border="0">
  <thead>
    <tr>
      <th> </th>
      <th><a href="index.php?order=name">Name</a></th>
      <th>Description</th>
      <th>Price<br>
        <a href="index.php?order=price_asc">
          <img src="<?php print $path_to_root; ?>/library/arrow_down.png"
               alt="Sort from low to high">
        </a>
        <a href="index.php?order=price_desc">
          <img src="<?php print $path_to_root; ?>/library/arrow_up.png"
               alt="Sort from high to low">
        </a>
      </th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
<?php
//Loop across records.
while( $row = $record_set->fetch_assoc() ) {
  //Get fields for an product.
  $product_id = $row['product_id'];
  $name = $row['name'];
  $description = $row['description'];
  $image_file_name = $row['image_file_name'];
  $price = $row['price'];
  //Output
  print "
    <tr>
      <td>
        <img src='$path_to_root/product-images/$image_file_name'>
      </td>
      <td>$name</td>
      <td>$description</td>
      <td>$price</td>
      <td>
        <a href='edit-product.php?id=$product_id'>Edit</a><br>
        <a href='confirm-delete-product.php?id=$product_id'>Delete</a>
      </td>
    </tr>";
} //End while.
?>
  </tbody>
</table>

Figure 3. DogToys administration menu code

Line 3 makes a link to the add-product.php page.

Lines 9 and 10 connect to the database. Lines 11 to 15 check that the connection was successful.

Lines 17 to 32 create the SQL query. The statement has an ORDER BY clause attached.

Line 33 runs the query. Lines 34 to 41 check for error reports from the database.

Lines 66 to 87 output a table row for each product. Here are the lines that make the Edit and Delete links:

<a href='edit-product.php?id=$product_id'>Edit</a><br>
<a href='confirm-delete-product.php?id=$product_id'>Delete</a>

They use the “drill-down” method we used in the previous lesson. The product id is attached to the URL for the edit and delete pages.

We’ll look at how editing and deletion is done in future lessons. For now, just notice how the links are created to edit-product.php and confirm-delete-product.php.

DogRock administration menu

The DogRock administration menu is much the same:

DogRock administration menu

Figure 4. DogRock administration menu

Here are excerpts from the code that makes the menu:

<p>What do you want to do?</p>
<blockquote>
  <p><a href="add-article.php">Add a new article</a></p>
</blockquote>
<h2>Current articles</h2>
<?php
//Connect to DB.
require $path_to_root . '/library/db-connect.php';
$db = new mysqli($host, $user_name, $password, $db);
...
//Fetch article data.
$query = "select article_id, title, author, when_published
    from articles
    order by ";
...
$record_set = $db->query($query);
...
//Loop across records.
while( $row = $record_set->fetch_assoc() ) {
  //Get fields for an article.
  $article_id = $row['article_id'];
  $title = $row['title'];
  $when_published = $row['when_published'];
  $author = $row['author'];
  ...
  //Output
  print "
    <tr>
      <td><a href='../show-article.php?id=$article_id'>$title</a></td>
      <td>$when_published</td>
      <td>$author</td>
      <td>
        <a href='edit-article.php?id=$article_id'>Edit</a><br>
        <a href='confirm-delete-article.php?id=$article_id'>Delete</a>
      </td>
    </tr>";
}
?>

Figure 5. DogRock administration menu code

It’s much the same as the other administration menu. Lines 33 and 34 create the Edit and Delete links.

Exercise: Jokes administration menu

Add an administration menu to your Jokes application. It should have a link to the add form, plus edit and delete links for each joke.

The links should refer to edit-joke.php and confirm-delete-joke.php. They don’t exist yet; you’ll add them later.

You can check my solution. But don’t look at it now! Do it yourself, first.

(Log in to enter your solution to this exercise.)

Summary

  • Our applications’ sites have two parts: one for regular users, and one for administration.
  • The admin menu lists items (products or articles), with Edit and Delete links for each.

What now?

Let’s see how record deletion works.


How to...

Lessons

User login

Log in problems? Try here


Dogs