PHP

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<? 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>
</head>

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"];
$oPolls->setPollId($iPollId);
$iVote = (int) $_POST["vote"];
$oPolls->setAnswerId($iVote);
$oPolls->addVote();
header("Location: ".SELF);
}
?>

The HTML below will display the title table.

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

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 ?>
<tr>
<td><?php print format($aPoll["Question"]) ?></td>
</tr>

<tr>
<td>

<table width="600" border="0" cellpadding="0" cellspacing="0">
<?php
$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:

<tr>
<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>
</tr>

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

<?php

} 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);

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

$sChecked = "";
}

++$i;
} // end poll answers loop
?>
</table>

</td>
</tr>

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 ?>
<tr>
<td align="right"><input type="submit"></td>
</tr>

<? } else { // poll vote has been recorded, render totals ?>
<tr>
<td>Total Votes: <?php print $aPoll["Vote Count"] ?></td>
</tr>
<?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 ?>
<tr>
<td>I am sorry, there are no polls available at this time.</td>
</tr>
<?php } // end poll count value check ?>

From here, we wrap up the page:

</table>
</form>
</body>
</html>
<?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
Home CSS Deals HTML HTML5 Java JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress