In the last piece on our PHP online user poll, we look at the administrative service and how the site supervisor enters, deletes and manages the poll data.

The first poll administrative page checks if the administrator is logged in. You can choose from Session variables or Cookies to check the site administrator login status.

Once the application has confirmed the identity of the site administrator, the page lists the available polls.

The first step is to use the class methods and variables in the class.polls.php file:

<?php
require_once("class.polls.php");

The next steps include instantiating the poll object and retrieving the poll data:

// instantiate polls class
$oPolls = new polls;

// get polls and poll count
$aPolls = $oPolls->getPolls("created_dt desc", $iCursor);
$iCnt = $oPolls->getPollsCount();

We create an array to hold and display the data from the polls table in the database:

// check for polls
if (count($aPolls)) {

// build page data array
$i = 0;
while ($i < count($aPolls)) {
$aData[$i]["Id"] = $aPolls[$i]["Poll Id"];
$aData[$i]["Name"] = $aPolls[$i]["Question"];
$aData[$i]["Status"] = $aPolls[$i]["Status"];
$aData[$i]["Created"] =$aPolls[$i]["Created Date"];
++$i;
}
}

For a given poll ID number, we can delete, activate or deactivate a selected poll:

// check for id
if ($id) {

// assign poll id
$oPolls->setPollId($id);

// check operation type
if (!strcmp($op, "del")) {

// try delete poll and redirect
$oPolls->deletePoll();
header("Location: ".SELF);

} elseif (!strcmp($op, "act")) {

// try activate poll and redirect
$oPolls->activatePoll();
header("Location: ".SELF);

} elseif (!strcmp($op, "deact")) {

// try deactivate poll and redirect
$oPolls->deactivatePoll();
header("Location: ".SELF);
}
}

?>

This function displays the list in an HTML table:

<?php
function renderList($iCnt=0, $aData='') {

global $iCursor, $iPerm;
?>
<br />
<?php if (is_array($aData)) { ?>
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><strong>Item Name</strong></td>
<td><strong>Created</strong></td>
<td><?php if ($iPerm > 2) { ?><strong>Actions</strong><?php } ?></td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<?php
// loop through data and conditionally display functionality and content
$i = 0;
?>
<tr>
<td width="16">
<divStatus"] ?>">
<?php if ($iPerm > 2) { ?>
<a href="index.php?op=<?php print $aState[$aData[$i]["Status"]] ?>&id=
<?php print $aData[$i]["Id"] ?>" onclick="return verify();"><?php } ?>
<img src="../../_img/icon_status_<?php print $aData[$i]["Status"] ?>.gif" width="16" height="10" alt="" border="0" />

<?php if ($iPerm > 2) { ?></a><?php } ?></td>
<td><?php print format($aData[$i]["Name"]) ?></td>
<td><?php print date("Y-m-d H:i:s", $aData[$i]["Created"]) ?></td>
<td><?php if ($iPerm > 1) { ?>
<a href="form.php?op=edit&id=<?php print $aData[$i]["Id"] ?>">EDIT</a> | 
<a href="index.php?op=del&id=<?php print $aData[$i]["Id"] ?>" onclick="return verify();">DELETE</a>
<?php } ?>
</td>
</tr>
<tr>
<td colspan="4"> </td>
</tr>
<?php
++$i;
} // end loop
?>
</table>
<?php } else { ?>
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>There is currently no data available for this section.</td>
</tr>
</table>
<?php } // end condition ?>
<?php } // end function ?>

The HTML Table below will display the list of available polls and call the renderList function:

<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><div>Developer Drive Polls Administration</div></td>
</tr>
<tr>
<td colspan="2"><div>To manage polls, select a poll action from the list below.</div></td>
</tr>
<tr>
<td><div><?php writeErrors() ?></div></td>
<td align="right" valign="top">
<?php if ($iPerm > 1) { ?>
<a href="form.php?op=add">Add a Poll</a>
<?php } ?></td>
</tr>
</table>
<?php renderList($iCnt, $aData) ?>

Once you have selected a poll, the input form (form.php) will allow you to add new polls or manage available poll data. Again, we must include the class.polls.php code and instantiate a new polls class:

<?php
require_once("class.polls.php");

// instantiate polls class
$oPolls = new polls;

The form will check for the poll ID query string value set in the link and assign the poll ID for this page to that value:

if ($id) {

// assign poll id
$oPolls->setPollId($id);
}

Since the form posts the information to itself, this section checks if the form has sent the POST values and makes the appropriate responses:

if ($_POST) { // check for http post vars

// assign post vars
$sQuestion = $_POST["question"];
$aAnswer = $_POST["answer"];

// check question value
if (!strcmp("", $sQuestion)) {

catchErr("Enter a poll question");
$FORMOK = false;
}

// check answers
$i = 0;
while (list($key, $val) = each($aAnswer)) {

$aAnswers[$i]["Answer Id"] = $key;
$aAnswers[$i]["Answer"] = $val;

if (!strcmp("", $val) && $i < 2) {
catchErr("Enter a value for answer ".($i + 1));
$FORMOK = false;
}
++$i;
}

// if forms variables validated
if ($FORMOK) {

$aArgs["Question"] = $sQuestion;
$aArgs["Answers"] = $aAnswer;

// check operation type
if (!strcmp("edit", $op)) {

// try edit poll
$FORMOK = $oPolls->editPoll($aArgs);

} elseif (!strcmp("add", $op)) {

// try add poll
$FORMOK = $oPolls->addPoll($aArgs);
}

// redirect if successful
if ($FORMOK) {

// redirect if successful
header("Location: index.php");
}
}

If the form has not yet sent any POST values, the page calls the getPoll method from the polls class and fills in the values:

} else {

// assign page variables
if (!strcmp("edit", $op)) {

$aPoll = $oPolls->getPoll();
$sQuestion = $aPoll["Question"];
$aAnswers = $aPoll["Answers"];

$i = 0;
while ($i < count($aAnswers)) {

$aAnswer[$i] = $aAnswers[$i]["Answer"];
++$i;
}
}
}

This form displays the poll data retrieved from the getPolls method:

} else {

// assign page variables
if (!strcmp("edit", $op)) {

$aPoll = $oPolls->getPoll();
$sQuestion = $aPoll["Question"];
$aAnswers = $aPoll["Answers"];

$i = 0;
while ($i < count($aAnswers)) {

$aAnswer[$i] = $aAnswers[$i]["Answer"];
++$i;
}
}
}

User polls can be fun for both customers and developers. Developers can use this guide as a means for their clients to get immediate feedback from the end user. They can give clients insights into what their customers want and allow users to participate in the process of how business make decisions on their products and services.

By 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.
  • i like you last part most, btw this is more useful. thanks for share 🙂

  • Good to read about administration concept which makes an incredible changes in the environment of reputable firms.

  • I was looking for this script for many days. Thanks for share this great resource on your blog.

  • Rashedur Roni

    Nice tutorial! Thanks.

Home XML WordPress Web Services Web Development Web Design Underscore Uncategorized Tutorial Tools SQL Server Social Apps Snippet SEO Security RoR Responsive Design Resources Python PHP News MySQL Mobile Miscellaneous jQuery JavaScript Java J2EE HTML5 HTML Design Patterns