PHP User Survey Part IV: Presentation Layer

So far in this series, we have developed the data layer (database tables) and the business layer (PHP methods) for manipulating the data.

In this piece, we will look at the presentation layer that is used to display the poll question and poll results.

The HTML header will check for the presence of a cookie (in case the user has voted previously) and refresh the page if it has timed out.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" xml:lang="en" lang="en">

<? if (!empty($_COOKIE["cACCOUNT"])) { ?>
<meta http-equiv="Refresh" content="<?php print TIMEOUT / 2 ?>;
url=devdrivepoll.php" />
<? } ?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Developer Drive Poll</title>

We will include the file that pulls the poll data from the database tables:

<? require_once("class.polls.php"); // class functions for polls

We can now instantiate a new “polls” class and start pulling the active poll data:

// instantiate polls class
$oPolls = new polls;

// get poll data
$aPoll = $oPolls->getActivePolls($iCursor);
$iCnt = $oPolls->getPollsCount(true);

Since we are using the same page to display results as we are to show the form, we will check to see if the user has already voted and record their results.

 if ($_POST) {

// assign poll member variables and add vote
$iPollId = (int) $_POST["pollid"];
$iVote = (int) $_POST["vote"];
header("Location: ".SELF);

The HTML below will display the title table.

<table border="0" cellpadding="0" cellspacing="0">
<td><div>Developer Drive Survey</div></td>
<td><div>Cast your vote.</div></td>

The form below will display the poll question and the choices for an answer:

<form action="devdrivepoll.php" method="post" name="devdrivepoll">
<input type="hidden" name="pollid" value="<?php print $aPoll["Poll Id"] ?>">
<table width="600" border="0" cellpadding="0" cellspacing="0">
<?php if ($iCnt) { // check poll count value ?>
<td><?php print format($aPoll["Question"]) ?></td>


<table width="600" border="0" cellpadding="0" cellspacing="0">
$i = 0;
$sChecked = " checked";
strcmp($_COOKIE["cPOLL"], $aPoll["Poll Id"]) ? $iVoted = false : $iVoted = true;

while ($i < count($aPoll["Answers"])) { // loop poll answers
<?php if (!$iVoted && $iCursor < 1) { // poll vote check ?>

This section loops through the available answers and displays the voting form:

<td width="20"><input type="radio" name="vote"
value="<?php print $aPoll["Answers"][$i]["Answer Id"] ?>"
<?php print $sChecked ?>></div></td>
<td width="580"><?php print format($aPoll["Answers"][$i]["Answer"]) ?></td>

This section displays the poll results in both numerical and graphical form:


} else { // display results

// assign calculation defaults
$iPerc = 0;
$iWidth = 0;

// if the poll total vote count is greater than 0
if ($aPoll["Vote Count"]) {

// find the percentage
$iPerc = round($aPoll["Answers"][$i]["Answer Count"] / $aPoll["Vote Count"] * 100, 0);

// multiply the percentage by 4.8 to get a scaled image length
$iWidth = round(($iPerc * 4.8) - 1, 0);

<td><div><?php print format($aPoll["Answers"][$i]["Answer"])." ".$iPerc ?>%</div></td>
<td><img src="../../_img/meter.gif" width="<?php print $iWidth ?>" height="10" alt="" border="0"></td>
<td> </td>
<?php } // end answers  display check ?>
// check default state for radio buttons
if (!strcmp(" checked", $sChecked)) {

$sChecked = "";

} // end poll answers loop


If the user has not voted, the page will display the “Submit” button to send the vote. If the user has voted, this section will display the total number of votes in the poll.

<?php if (!$iVoted && $iCursor < 1) { // poll vote check ?>
<td align="right"><input type="submit"></td>

<? } else { // poll vote has been recorded, render totals ?>
<td>Total Votes: <?php print $aPoll["Vote Count"] ?></td>
<?php  } // end poll vote check ?>

If no active polls exist, the page will display a message showing that there are no polls:

<?php } else { // there are no polls ?>
<td>I am sorry, there are no polls available at this time.</td>
<?php } // end poll count value check ?>

From here, we wrap up the page:

<?php } ?>

In the next part, we will examine the administrative side of this application.

Gerald Hanks has been involved in web development applications since 1996. He has designed applications with JavaScript, ASP.NET and PHP, as well as building databases in MS SQL Server and MySQL. He lives in Houston, Texas. More articles by Gerald Hanks
  • Wojciech Soczyński

    This series is quite informative, but unfortunately it teaches new users some bad habits which they should avoid coding real apps. Just to mention:

    1. the code does not use an autoloader
    2. the class naming convention is different from present standards (PSR-0)
    3. you are mixing responsibilities in your code:
    – sending headers in a template
    – directly manipulating business objects (poll) in a template
    – executing business logic in a template (calculating percentages)
    4. in is now a standard to use php alternative control structure syntax in templates ( like if(something): doA(); else: doB(); endif; )
    5. the html uses deprecated attributes (cellpadding, cellspacing, border)

  • Smart Client applications may not be as easy as thin-client applications are defined, and who can speak many different forms and are not limited to one size fits all thin client applications. The main difference between a smart client and thin client is a smart client that does not rely on a browser to have to use the interface design environment, security and user. Thanks for bringing this useful help.

Home CSS Deals HTML HTML5 Java JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress