Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


Creating a PHP-Based Content Management System, Part 4


Peter Zeidman
10/8/2004

Go to page: 1 2 

Printer Friendly Version

Have a question about this article, object-oriented programming, or PHP? Visit Intranet Journal's Discussion Forum

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:

 Field  Purpose Type
     
ID A unique ID for each section Integer
name The name of the section Varchar(20)
parentid If this is a sub-section, the id of the parent Integer

The SQL code to create this table is listed below:

CREATE TABLE `database`.`cmssections` (
`ID` int(4) unsigned NOT NULL auto_increment COMMENT 'The unique ID of the section',
`name` varchar(20) NULL COMMENT 'The section name',
`parentid` int(4) NULL DEFAULT 0 COMMENT 'The ID of the parent section',
PRIMARY KEY (`ID`)
);

(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:

 

SECTION 1  - Delete
SECTION 2  - Delete

Create a Section:

 Name:

 Parent:

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
// Require the classes
require_once('../includes/DbConnector.php');
require_once('../includes/Validator.php');

// Create an object (instance) of the DbConnector and Validator
$connector = new DbConnector();
$validator = new 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'){

// Store the ID of the section to be deleted in a variable
$sectionID = $HTTP_GET_VARS['id'];

// Validate the section ID, and if it's ok then delete the section
if ( $validator->validateNumber($sectionID,'Section ID') ){

// The validator returned true, so go ahead and delete the section
$connector->query('DELETE FROM cmssections WHERE ID = '.$sectionID);
echo 'Section Deleted <br><br>';

}else{

// The validator returned false, meaning there was a problem
echo "Couldn't delete. There was a problem with: ".$validator->listErrors();

}

}

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
$result = $connector->query('SELECT ID,name,parentid FROM cmssections');

// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){

echo $row['name'].' - &nbsp;&nbsp; '; // Show the name of section
echo '<a href="editSections?action=delete&id='.$row['ID'].'"> Delete </a>'; // Show the delete link
echo '<br>'; // Show a carriage return

}
?>

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)
newArticle.php: the New Article admin page, revised to use the Validator class. Store in cmsadmin
editSections.php: the admin page to add or delete sections, store in cmsadmin.

Read Part 5 of this series at: http://www.intranetjournal.com/articles/200411/ij_11_08_04a.html.

Go to page: 1 2

Printer Friendly Version

Of Interest
Intranet Discussion Forum
Creating a PHP-Based Content Management System, Part 1
Creating a PHP-Based Content Management System, Part 2
Creating a PHP-Based Content Management System, Part 3
PHPBuilder

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit




The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers