Creating and maintain a web site is not a hard job today especially at a time when the full-proof and automated content management systems are so widely available. However, creating a simple CMS is also not much of a tough task. Here is the simplified and step by step guide for first time PHP users for the creation of their first CMS.
Building the Class
In order to lay a foundation for your first content management system, you simply need to build a class in the file named “simpleCMS.php”. Once the class has been created, add four variable namely host, username, password and table. In other words, try to create one class with five methods and four variables. To bring out a cleaner code especially in a project as large as creating an entire CMS, try opting for the PHP’s object oriented approach.
All the four variables created under the class are actually used in connecting the database. $table, $username, $password and $host provide an access and path to the database on the specified server. The database is further constructed through the method buildDB ().
Building the Database
The function buildDB () is always declared as private in to the parent class. Such a function usually runs a MySQL command which further checks the generated database so as to confirm if ‘testDB’ exists. If the performed check is positive, the function would pass on a notification terming “success”. In case the result is negative, it will create a table and will further assign three columns to it in order to hold the unmatched data.
Connect the Database
We have already created a function which will build our table. It’s now time to create one that will connect it to the database. In order to so, we call two functions. The first one being mysql_connect() that will simply hook the database and the other one being mysql_select_db() that will save all the data in the appropriate place. However, the connect () function is used to connect further to the database and run the buildDB () function. Also, you must make sure that you are in no way overwriting the entire database every time you tend to load the page so as to call the specified functions.
Build the Form
Now the database has been created and you need to put relevant stuff into it. Use the form element in order to declare the function. In order to reference the file which is currently in usage, try adding an extra $_SERVER[‘PHP_SELF’] variable in the action attribute of the current form element. This will help in serving as a shortcut and is highly beneficial if you are planning to reuse your code for each page on a different website as it will save the trouble of rewriting.
Saving the Data in the Database
The write () method is employed to save the data in the existing database. Put a variable $p in to the function call which will hold the entire information that has been earlier sent from the form through the past method. Also, make sure that you check all the variables before saving the entire database so that they are not left blank. Once the variables have been set, you should then try to set the variable $ created with the existing Unix Timestamp. This will aid in the sorting of the entries in a chronological manner whenever you will try to view them in future.
Displaying the Information in the Database
In order to get the information back out from the generated database, we make use of the display_public () method. This is indeed one of the most complex methods employed in the creation of simple content management systems. Therefore, you might try and ask for a little savvy help on the same in case you are unable to understand anything.
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.