【php开源代码栏目提醒】:网学会员鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“class_vote.php”一文,供大家参考学习
<?php
class TSvote extends SupportFunctions {
//投票主页名称
var $pageName = "demo.php";
var $voteID;
// 保存数据的文件
var $question_data = "question.dat";
var $answer_data = "answer.dat";
//用于显示
var $choice_template = "choice_template.php";
var $result_template = "result_template.php";
// 用于显示投票结果的图像,不同的选项使用不同的图片显示
var $bar_images = array ( "images/aqua.gif",
"images/yellow.gif",
"images/red.gif",
"images/green.gif",
"images/pink.gif",
"images/orange.gif",
"images/darkgreen.gif",
"images/gold.gif",
"images/grey.gif",
"images/blue.gif" );
//该函数返回关于投票的所有信息,其返回一个二维数组
//该数组包含投票主题、投票选项投票百分比结果、实际投票数量
//以及总投票数
function _getVoteInfo() {
$question_file = file($this->question_data);
$answer_file = file($this->answer_data);
//找到被选择的投票主题
for ($i = sizeof($question_file) - 1; $i >=0; $i--) {
$question_line = explode("|", $question_file[$i]);
if (!strcmp($question_line[0], $this->voteID)) {
$vote_info["headline"] = $question_line[1];
// The corresponding answers are in the same row ($i)
// of the answer_file.
$answer_line = explode("|", $answer_file[$i]);
$vote_info["totalVotes"] = $answer_line[1];
for ($j = 2; $j < sizeof($question_line) - 1; $j++) {
$vote_info["choice"][$j - 2] = $question_line[$j];
$vote_info["answer_absolute"][$j] = $answer_line[$j];
if ($vote_info["totalVotes"] == 0)
$vote_info["answer_relative"][$j - 2] = 0;
else $vote_info["answer_relative"][$j - 2] = round(($vote_info["answer_absolute"][$j]
/ $vote_info["totalVotes"]) * 100, 2);
}
}
}
return $vote_info;
}
//显示投票选项
function _displayVote() {
$vote_info = $this->_getVoteInfo();
//调用显示选项的模板
include ($this->choice_template);
return $vote_info;
}
//该函数根据用户投票结果,修改保存投票信息的数组
//然后将数据保存到文件中
function _processVote(&$vote_info, $radiobutton, $voteOnce) {
if ($this->_checkPermission($voteOnce) == 1) {
$vote_info["totalVotes"]++;
$vote_info["answer_absolute"][$radiobutton + 2]++;
$vote_info["answer_relative"][$radiobutton]=round(($vote_info["answer_absolute"][$radiobutton + 2] / $vote_info["totalVotes"]) * 100, 2);
$answer_file = file($this->answer_data);
for ($i = 0; $i < sizeof($answer_file); $i++) {
$answer_line = explode("|", $answer_file[$i]);
// Find the selected poll in the DAT file.
if (!strcmp($answer_line[0], $this->voteID)) {
// Register the casted vote.
$answer_line[$radiobutton + 2] = $vote_info["answer_absolute"][$radiobutton + 2];
// Register the new total number of votes.
$answer_line[1] = $vote_info["totalVotes"];
$answer_file[$i] = implode("|", $answer_line);
$logged = $this->_writeArray($this->answer_data, $answer_file, "w");
}
}
} else echo("对不起,你只能投票1次!<br><br>");
return 0;
}
//显示投票结果
function _displayResult($vote_info) {
//计算每个投票选项的显示宽度
for ($i = 0; $i < sizeof($vote_info["answer_relative"]); $i++)
$width[$i] = 2 * round($vote_info["answer_relative"][$i]);
// 调用显示投票结果的模板
include($this->result_template);
}
//检查用户是否有投票权限,如果已经投票过,则取消投票权限
function _checkPermission($voteOnce) {
$votePermission = 1;
// Check if the user has already voted for the actual poll.
for ($i = 0; $i < sizeof($voteOnce); $i++) {
if ((isset($voteOnce)) && ($voteOnce[$i] == $this->voteID)) {
// User has already voted.
$votePermission = 0;
}
}
return $votePermission;
}
//保证每个用户只有一次投票机会,但是客户端必须能够支持cookie
function _setPermission($voteOnce) {
$voteCookieIndex = sizeof($voteOnce);
$setNewCookie = 1;
for ($i = 0; $i < $voteCookieIndex; $i++) {
if ($voteOnce[$i] == $this->voteID)
$setNewCookie = 0;
}
if ($setNewCookie == 1)
header ("Set-Cookie: voteOnce[$voteCookieIndex]=$this->voteID;
expires=Friday, 16-Jan-2037 00:00:00 GMT; path=/");
return 0;
}
//该函数添加一个新的投票主题,依次按照文件格式写入对应的编号
//主题和选项
function _setNewVote($vote_info) {
$question_file = file($this->question_data);
$index = sizeof($question_file);
$newQuestion[0] = $index;
$newQuestion[1] = $vote_info["headline"];
$newAnswer[0] = $index;
$newAnswer[1] = 0;
// 将选项写入文件
//并且初始化每个选项的投票结果为0
for ($i = 1; $i <= sizeof($vote_info["choice"]); $i++) {
if ($vote_info["choice"][$i] != "") {
$newQuestion[$i + 1] = $vote_info["choice"][$i];
$newAnswer[$i + 1] = 0;
}
}
$newQuestion = implode("|", $newQuestion)."|\r\n";;
$newAnswer = implode("|", $newAnswer)."|\r\n";;
$this->_writeLine($this->question_data, $newQuestion, "a");
$this->_writeLine($this->answer_data, $newAnswer, "a");
return 0;
}
//定义下拉菜单,让用户选择投票主题
function _defineJumpMenu() {
echo("<script language=\"JavaScript\" type=\"text/javascript\" >\n");
echo("<!--\n");
echo("function JumpMenu(targ,selObj,restore){\n");
echo(" eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\")\n");
echo(" if (restore) selObj.selectedIndex=0\n");
echo("}\n");
echo("//-->\n");
echo("</script>\n");
return 0;
}
//显示投票主题 ,让用户选择
function _displayJumpMenu() {
echo("<select class=\"selectVoteMenu\" name=\"VoteMenu\" onChange=\"JumpMenu('parent',this,0)\">");
echo(" <option selected>Select poll...</option>");
$question_file = file($this->question_data);
for ($i = sizeof($question_file) - 1; $i >= 0; $i--) {
$question_line = explode("|", $question_file[$i]);
echo("<option value=\"$this->pageName?ID=$question_line[0]\">");
echo($question_line[1]);
echo("</option>");
}
echo("</select>");
return 0;
}
}
?>
上一篇:
Class_support.php
下一篇:
哈弗F7 VS名爵HS,谁更懂得年轻人的心?