Now it is time to review the simple application in PHP. The following assumes you are familiar with PHP. How simple is the application? Well I have broken down the workflow to these steps:
This PHP application leaves out some features such as:
In the sample files download at the beginning of this article, you have three PHP files: index.php, getarticle.php, and publish.php. There is also a folder called static. You can drop all this into a site in Dreamweaver.
Now you need to get your DB set up. To do that you can go to the command line and enter the SQL in the downloaded file or you can copy/paste it into phpMyAdmin's SQL tab once you set up a database.
Note: You will need to update the values of the $user, $password, and $dbconnection variables in the three PHP files for the files to successfully connect to your database.
Move the three PHP files and the static directory over to the server, and you should have a crudely functioning version of a PHP check-in/check-out site that will generate static files for you in the static directory.
This script presents a web page, which lets the user fetch articles from the database. This page connects to the database, performa a simple query, and displays the results. Clicking the results will create a static HTML file for the article on the web server (by linking to another script─getArticle.php) and display it (by redirecting to the article's static HTML file). At this point, the user can edit the file in Macromedia Contribute.
The database is simple with one table 'devnet' and three fields in that table:
id – the autogenerated primary id title – the title of the article text – the text for the article The directory structure is simple. It is assumed there is a set folder that's being worked in. At this level, all the scripts are stored. There's one sub-folder called static, which stores the static HTML files that are generated. This is a simple application to show how to handle some basic tasks.
I'll now review some important variables to consider in the index.php file.
Database variables:
$user – the username for the db $password – the password$dbconnection – the connection handle$database – the name of the database$current_table – the name of the table to use at the momentOther variables:
$webPath – the path to the webpage at the level of the scriptDependencies: None
This script connects to the database and accesses one article and constructs a static HTML file. The ID of the file is passed in the $_GET variable id. After connecting to the database and querying for the article's information, a simple HTML file is constructed. It is created with 'chmod'ed to give everybody full permissions. After the file is written, a simple redirect is done to the static HTML file. The file is called ID.html where ID is the auto-generated id field for the record pertaining to the article.
The HTML structure used is simple. There's a line at the top describing the type of document, which is being constructed. Then a basic structure follows:
<html> <head> <title> TITLE </title> </head> <body> TEXT </body> </html>
I'll now review some important variables to consider in the getarticle.php file.
Database variables:
$user – the username for the db$password – the password$dbconnection – the connection handle $database – the name of the database $current_table – the name of the table to use at the moment$row – row that is fetched from the query Other variables:
$webPath – the path to the webpage at the level of the scriptArticle variables:
$id – the id for the article $title – the title for the article $text – the body of the article File-writing variables:
$fhWRITING – the handle used to write the fileDependencies: None
This script will read a directory for all the HTML/HTM files and present them in a table with a checkbox next to each file. Checking the box means the user wants to publish that file (this would parse the file for the title and text and put it into the database; updating a previous article by the same title, or adding a new article if the title is not in the database AND also deleting that file from the web server). Then the user can click the Publish button, which will reload the same page, but things will appear differently. A confirmation of the files that were published as well as a link to the index.php page will be presented.
This is done using forms. A form can have different 'states' by the use of the form variables–such as $_POST is similar to $_GET in that its an array of key/value pairs. The value used in this form is the value of 'submit'. This predefined input type is a button and can be used by implementing the following line of code:
<input type="submit" value="Publish Selected" name="publish_it" size="10">
When it's clicked, 'publish_it' will have the value of 'Publish Selected' and the page will be called again. You can program for the different states of this form by using a simple 'if' conditional statement that checks this key in $_POST. Furthermore, the 'publish' key will hold values for which articles need to be published.
Here are the important variables to consider in the publish.php file.
Database variables:
$user – the username for the db $password – the password $dbconnection – the connection handle$database – the name of the database $current_table – the name of the table to use at the moment$row – row that is fetched from the query Other variables:
$webPath – the path to the webpage at the level of the scriptArticle variables:
$id – the id for the article $title – the title for the article $text – the body of the article File-reading:
$fhREADING – the file handle for reading the filesDependencies: None
In Communications and Public Affairs at the University of Waterloo, we have built a PHP-based application that compliments Contribute and gives us a more functional, cost effective CMS that meets our needs and is flexible. We built it around the simple script covered in this article.
What could you do with it? The first thing that comes to mind is use Contribute to maintain a blog. Recently on Deeje's blog there was a post on a Blog Advisory Group for Contribute. This set of scripts could be used to give you some Blog functionality for Contribute—the way we now publish content with Contribute is very much like a blog.
What about CPS? We do use CPS but it isn't required for the functionality of this specific application.
So grab the code, build on it, and let me know what you come up with.