PHP
85 Best Web Development Courses in 2021
Web development is the process of building and maintenance of websites. Web development has a wide...
In this PHP project, we are going to create an opinion poll application.
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;
Assumptions made
The opinion poll will ask the question –
What is your favorite JavaScript Library?
Answers would be
Here are the steps to create the application –
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;
The script below creates our js_libraries table.
<?php CREATE TABLE `js_libraries` ( `id` int(11) NOT NULL AUTO_INCREMENT, `choice` tinyint(4) NOT NULL DEFAULT '0', `ts` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ); ?>
Let’s now create our business logic layer that will handle the database connectivity. 'opinion_poll_model.php'
<?php
class Opinion_poll_model {
private $db_handle; private $host = 'localhost'; private $db = 'opinion_poll';private $uid = 'root'; private $pwd = 'melody';
public function __construct() {
$this->db_handle = mysqli_connect($this->host, $this->uid, $this->pwd); //connect to MySQL server
if (!$this->db_handle) die("Unable to connect to MySQL: " . mysqli_error());
if (!mysqli_select_db($this->db_handle,$this->db)) die("Unable to select database: " . mysqli_error());
}
private function execute_query($sql_stmt) {
$result = mysqli_query($db_handle,$sql_stmt); //execute SQL statement
return !$result ? FALSE : TRUE;
}
public function select($sql_stmt) {
$result = mysqli_query($db_handle,$sql_stmt);
if (!$result) die("Database access failed: " . mysqli_error());
$rows = mysqli_num_rows($result);
$data = array();
if ($rows) {
while ($row = mysqli_fetch_array($result)) {
$data = $row;
}
}
return $data;
}
public function insert($sql_stmt) {
return $this->execute_query($sql_stmt);
}
public function __destruct(){
mysqli_close($this->db_handle);
}
}
?>HERE,
Let’s now create the front controller index.php
<?php
require 'opinion_poll_model.php';
$model = new Opinion_poll_model();
if (count($_POST) == 1) {
echo "<script>alert('You did not vote!');</script>";
}
if (count($_POST) > 1) {
$ts = date("Y-m-d H:i:s");
$option = $_POST['vote'][0];
$sql_stmt = "INSERT INTO js_libraries (`choice`,`ts`) VALUES ($option,'$ts')";
$model->insert($sql_stmt);
$sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries;";
$choices_count = $model->select($sql_stmt);
$libraries = array("", "JQuery", "MooTools", "YUI Library", "Glow");
$table_rows = '';
for ($i = 1; $i < 5; $i++) {
$sql_stmt = "SELECT COUNT(choice) choices_count FROM js_libraries WHERE choice = $i;";
$result = $model->select($sql_stmt);
$table_rows .= "<tr><td>" . $ libraries [$i] . " Got:</td><td><b>" . $result[0] . "</b> votes</td></tr>";
}
require 'results.html.php';
exit;
}
require 'opinion.html.php';
?>HERE,
Let’s now create the views. opinion.html.php
<html> <head> <title>JavaScript Libraries - Opinion Poll</title> </head> <body> <h2>JavaScript Libraries - Opinion Poll</h2> <p><b>What is your favorite JavaScript?</b></p> <form method="POST" action="index.php"> <p> <input type="radio" name="vote" value="1" />JQuery <br /><input type="radio" name="vote" value="2" />MooToolsl <br /><input type="radio" name="vote" value="3" />YUI Library <br /><input type="radio" name="vote" value="4" />Glow </p> <p><input type="submit" name="submitbutton" value="OK" /></p> </form> </body> </html>
results.html.php
<html> <head> <title>JavaScript Libraries Poll Results</title> </head> <body> <h2>Opinion Poll Results</h2> <p><b>What is your favorite JavaScript Library?</b></p> <p><b><?php echo $choices_count[0]; ?></b> people have thus far taken part in this poll:</p> <p> <table> <?php echo($table_rows); ?> </table> </body> </html>
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.
Web development is the process of building and maintenance of websites. Web development has a wide...
What is XAMPP? XAMPP is an open source cross platform web server, MySQL database engine, and PHP...
What is a Function? A function is a reusable piece or block of code that performs a specific...
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
A Loop is an Iterative Control Structure that involves executing the same number of code a number...
What is OOPs? Object Oriented is an approach to software development that models application...