PHP User Survey Part IV: Presentation Layer

Dec 6, 2011
PHP
By

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.

Author: Gerald Hanks
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.
  • 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)

  • http://www.logocontestreviews.com/99designs-review/ 99designs reviews

    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.