Creating a PHP CMS – Part 5

This post is part of a series about creating a PHP CMS. Click here to start from the beginning.

In the last few posts, we learned how to display information from the database. Today, we will be creating lists of posts for a navigation list, and for the CMS admin.

Navigation List

In functions.php, create a new function called listPages.

The first thing we will do is make sure that the page set as the home page is listed first.

$result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
$row = mysql_fetch_array($result);
 
$homeID = $row['value'];
 
$result = mysql_query("SELECT title FROM pages WHERE id='$homeID'");
$row = mysql_fetch_array($result);
 
$homeTitle = $row['title'];
 
echo "<li><a href='" . BASE_URL . "/index.php'>$homeTitle</a></li>";

Here we are using our BASE_URL constant in the echo statement. This is to prepend our base url to the filename 'index.php'.

Next we will list the rest of the pages.

$result = mysql_query("SELECT id, title FROM pages");
 
while ($row = mysql_fetch_array($result)) {
    // Do not list the home page twice
    if ($row['id'] != $homeID) {
        $pageID = $row['id'];
        $pageTitle = $row['title'];
 
        echo "<li><a href='" . BASE_URL . "/index.php?id=$pageID'>$pageTitle</a></li>";
    }
}

while is a type of loop. In this case, we are displaying a list item while there is still a row to display. Our if statement in the loop makes sure that the home page will not be listed twice.

Save functions.php, and in index.php, call our listPages function, wrapping it in <ul> (or <ol>) tags. You should see a list of the pages you created, with links.

Administration Table

In the administration panel, we will be listing pages again, but this time in a table with more information. This will include the page title with a link to the page, the page ID, links to delete, edit and set a home page, and a note saying which is the home page.

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
function displayAdmin() {
    // Find the home page ID
    $result = mysql_query("SELECT value FROM settings WHERE name='homePage'");
    $row = mysql_fetch_array($result);
 
    $homeID = $row['value'];
 
    // Display a table
    $result = mysql_query("SELECT id, title, date FROM pages");
 
    echo '<table>';
    echo '<tr>
        <th>ID</th>
        <th>Title</th>
        <th>Date</th>
        <th>Actions</th>
        </tr>';
 
    while ($row = mysql_fetch_array($result)) {
        $id = $row['id'];
        $title = $row['title'];
        $date = date('M d, Y', $row['date']);
 
        echo "<tr>
            <td>$id</td>
            <td><a href='". BASE_URL . "/index.php?id=$id'>$title</a>";
        if ($id == $homeID) {
            echo ' <strong>(Home Page)</strong>';
        }
        echo "</td>
            <td>$date</td>
            <td><a href='edit.php?id=$id'>Edit</a><br />
            <a href='confirm.php?id=$id'>Delete</a><br />
            <a href='sethome.php?id=$id'>Set as Home</a>";
    }
    echo '</table>';
}

This is basically just what we were doing before, except displaying more information.

Now in admin/index.php, include functions.php (don't forget the '../'.), and call the displayAdmin function. Make sure that everything is correct, and take a break for the day.

Click here for Part 6

Stay Updated

Did you enjoy this post? Don't miss a single post by getting free updates!

7 Comments

  1. August 20, 2009

    What to do if I don't want to have a url like xxxx?id=N? But like to be like this: http://example.com/postname?

  2. August 20, 2009

    Hua Chen: You would create a .htaccess file using some mod_rewrite rules to replace the stuff after the question mark with your own stuff.

    There is a guide on how to do this at Nettuts:
    http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

    • kgdd
      October 16, 2011

      Eric, can you please show an example of the .htaccess file to remove the ?id=__. Or is there a way in the getArray and functions.php to instead call the title of the id row instead of the id?

  3. February 9, 2012

    Functions are always great for me as a C++ programming!

Trackbacks/Pingbacks