|
|
|
|
|
|
|
|
Creating a PHP-Based Content Management System, Part 2
Peter Zeidman 8/5/2004 Go to page: 1 2
All of the information to be displayed in our Content Management System will be stored in a database. It is sensible, therefore, to create a reusable PHP class that we can call upon whenever we need to access our data. The code listed here is for connecting to a MySQL database. If you'll be using a different system, such as PostgreSQL, MS SQL or SQLite, then change the code appropriately. It's obviously quite a bit longer than our previous class, but it performs a number of very important tasks. The code follows: <?php class DbConnector extends SystemComponent {
Some explanation is required. After we've named the class 'DbConnector', we state 'extends SystemComponent'. This tells PHP to grab all of the data and functions from SystemComponent, and provide us with access to them (we'll need this in order to get the $settings variable we created earlier). The first function, 'DbConnector', has the same name as the class that contains it, meaning it's run automatically when DbConnector loads. It firstly calls the 'getSettings' function we wrote earlier, and extracts from it the various database settings. It then uses these settings to connect to the database. (Note that we have no code to deal with errors, this will be covered in detail next time.) The other functions
are explained below:
Save the above code (also attached at the bottom of this article) to the 'includes' folder, with the name DbConnector.php. This class will be widely used in the Intranet system, so let me give you an example of how we'd create an instance of DbConnector, extract some data, and display it to the user. Let's imagine that our database stores the details of one customer, and we want to get hold of his / her name and display it. Here's the code: <?php // Get the PHP file containing the DbConnector class // Create an instance of DbConnector // Use the query function of DbConnector to run a database
query // Get the result // Show it to the user ?> If you'd like to try out the DbConnector class now, you'll need to save the above code in the includes folder in a php file, and set up a 'customers' table in your database. I'll be covering the set up of our Intranet's database next time. The importance and power of using a database is clear — we can store information in a formal way, and rapidly access, manipulate and change it. The information we extract or store is specified using the 'query' function of the DbConnector class, and we create instances of DbConnector using the 'new' command, as shown above. This also demonstrates the usefulness of classes — if the settings are changed in SystemComponent, then all of the classes that extend it will automatically be changed. Next month we'll be adding code to deal with errors, and creating the first part of the administration system that will allow you to add or remove information on the intranet. Until then! Files (in compressed format): .zip files (for Windows) .tar files (for Linux) Read Part 3 of this series at: http://www.intranetjournal.com/articles/200409/ij_09_07_04a.html.
Go to page: 1 2
|
Intranet Journal's Tutorials |
|
Managing Editor |