Creating a PHP CMS – Part 5

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 twiceif($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.