The opinion poll will consist of 3 major components; Front controller – this is the index page that will determine the HTML code to be loaded. This will ensure that our application has a single entry point. This will give us more control over the application. Business Logic – this will contain the PHP code for interacting with the database. This will allow us to separate the business logic from the presentation making our application easy to maintain Views – this will contain the HTML code. We will have two pages namely;
opinion.html.php – this will contain the HTML code with the question and options results.html.php – this will contain the HTML code that displays the opinion poll results
Assumptions made The opinion poll will ask the question – What is your favorite JavaScript Library? Answers would be
JQuery MooTools YUI Library Glow
Here are the steps to create the application–
Step 1) Database Connectivity
This section assumes knowledge of MySQL and how to administer it, if you are not familiar with these MySQL, check our SQL tutorials section. Our application will have one table only with 3 fields namely;
id – auto-generate number as the primary key choice – the number representing a presidential candidate ts – the timestamp for the vote
The script below creates our js_libraries table.
Step 2) Coding our application
Let’s now create our business logic layer that will handle the database connectivity. ‘opinion_poll_model.php’ HERE,
“public function __construct()” is the class constructor method that is used to establish the database connection “public function execute_query(…)” is the method for executing queries such as insert, update and delete “public function select” is the method for retrieving data from the database and returning a numeric array. “public function insert(…)” is the insert method that calls the execute_query method. “public function __destruct()” is the class destructor that closes the database connection.
Let’s now create the front controller index.php HERE,
“require ‘opinion_poll_model.php’;” loads the business logic class “$model = new Opinion_poll_model();” creates an instance of the business logic class “if (count($_POST) == 1)…” performs the data validation and uses JavaScript to display a message box if not candidate has been voted for. “if (count($_POST) > 1)…” checks if a vote has been selected by counting the number of items in the $_POST array. If no item has been select, the $_POST will only contain the submit item. If a candidate has been chosen, the $_POST array will two elements, the submit and vote item. This code is also used to insert a new vote record and then display the results page “exit;” is used to terminate the script execution after the results have been displayed so that the opinion poll form is not displayed. “require ‘opinion.html.php’;” displays the opinion poll form if nothing has been selected.
Let’s now create the views. opinion.html.php results.html.php
Step 3) Testing our application
Assuming you have saved the files in opinionpoll folder, browse to the URL http://localhost/opinionpoll/
If you click on the Ok button without selecting a JS library, you will get the following message box.
Select a JS library then click on OK button. You will get the results page similar to the one shown below.
Summary
Dividing your application into business logic, front controller view layers is a good application design practice JavaScript is useful for performing client-side validation It is a good programming practice to use file.html.php for files that contain both HTML and PHP codes The opinion poll application demonstrates how the knowledge learned in the previous lessons can be put together to developing a working application with a database back end.