|
|
|
|
|
|
|
|
Creating a PHP-Based Content Management System, Part 4
Peter Zeidman 10/8/2004 Go to page: 1 2
Getting Organized We've covered how to create forms, manipulate the database, and set up a page for creating articles. But having an intranet with hundreds of articles will quickly become disorganized and unwieldy. The solution is to create a system of categories (sections), into which each article or news item can be stored. You'll notice that when we set up the database in Part 3 we created a "section" field. Well, now we'll use it. We want to be able to dynamically add and remove sections from our system, so we'll need a new table in the database. Here's the schema:
The SQL code to create this table is listed below: CREATE TABLE `database`.`cmssections`
( (If you don't know how to run SQL queries, see a short explanation in the Intranet Journal Discussion Forum, SQLCourse.com, or consult a reference text on SQL.) In the same way that we created an admin page for adding articles, it's simple to create a page to add, edit or delete sections. We want the page to look something like the one below: When this page loads, a list of sections is displayed as well as a link to remove any of them. There's also a form to create a new section. Notice the drop-down list for choosing the "parent" section; a similar menu will be used on the Add Article page to choose which section an article belongs in. How's all this coded? We begin by connecting to the database, and creating a Validator object (using the class we just designed): <?php // Create an object (instance)
of the DbConnector and Validator
Next, we'll add code to deal with the "delete" link. Notice that we use the Validator to make sure the ID number is numeric. To tell the page whether it should be adding, deleting or just listing the sections, we set the 'action' variable in the query string (i.e., http://yourintranet/cmsadmin/sectionEdit.php?action=XXX). This variable is read by the first line below: if ($HTTP_GET_VARS['action'] == 'delete'){
} The code to insert a section is quite simple so I won't detail it here, you'll find it easy to understand by reading the source file at the end. The last part of the PHP code for this page is to list the sections as shown above, with the delete link along side: // Execute the query to retrieve
articles // Get an array containing the
results.
} And now we have a category system set up. You can use it to define the sections that will make up your site, and then assign articles to them. On the front end that your users will see, you could have a page called showarticles.php and use it to show only the articles in a particular category (e.g., showarticles.php?id=1 for news, showarticles.php?id=2 for press releases, etc.). Next month I'll be covering authentication and security, so we can keep unwanted people out of the admin area. I'll be providing you with the rest of the forms required to make up the admin area, and will then move on to tying up the project — leaving you with a fully functional content management system for your intranet. Until next time! Source Files: Windows .zip Linux/Unix .tar Contents (Note: Some of these files require the creation of the cmssections table, described on page 1 of this article) Validator.php:
the Validator class, to be stored in the includes folder (if you're using
my suggested layout) Read Part 5 of this series at: http://www.intranetjournal.com/articles/200411/ij_11_08_04a.html.
Go to page: 1 2
|
Intranet Journal's Tutorials |
|
Managing Editor |