<? /*
Poll Class - Filipe Almeida - 11:05 quarta - 8 novembro
*******************************************************************************
Install procedure
*******************************************************************************
Create tables in your mysql database.
Change function dbconnect() for your database.
Create a new file in the directory where you have the copy of �poll_class.1.0.php�
Paste in the file you created the �execution example� bellow
This is a stable version and it works quite well. It's not finished though. You
can change a few important things, or derive from the class your display options.
Important note: This IS smokeware.
If you liked it and are using it, please send to the address:
Largo do Corpo Santo n�6 1�
1200-129 Lisboa
Portugal
A PACK OF CIGARETTES.
Feel free to email nowan@variograma.com or visit http://mega.ist.utl.pt/~fra
for details
Thank you
*******************************************************************************
execution example
*******************************************************************************
include "poll_class.1.0.php";
if (!$webpollcontrol){
$poll = new POLL($id_poll);
$poll->show_poll();
}
if ($webpollcontrol == 1){
$poll = new POLL($id_poll);
$poll->list_polls();
}
if ($webpollcontrol == 3){
$poll = new POLL($id_poll);
$poll->show_results();
}
if ($webpollcontrol == 5){
$poll = new POLL($id_poll);
$poll->save_data($id_poll,$webpollvote);
$poll->show_results();
}
*******************************************************************************
Table Definitions for Poll Class
*******************************************************************************
table:webpollsQ
id_webpoll int(11)
pollquestion varchar(250)
lang char(3)
comment text
time_stamp timestamp(14)
isvisible int(11)
table: webpollsO
id_webpoll int(11)
id_option int(11)
polloption varchar(250)
comment text
table:webpollsR
id_webpoll int(11)
id_polloption int(11)
id_user int(11)
time_stamp timestamp(14)
*******************************************************************************
CREATE TABLE FOR MYSQL DATABASE
*******************************************************************************
# --------------------------------------------------------
#
# Table structure for table 'webpollsO'
#
CREATE TABLE webpollsO (
id_option int(11) NOT NULL auto_increment,
polloption varchar(250) NOT NULL,
comment text,
id_webpoll int(11) NOT NULL,
PRIMARY KEY (id_option),
KEY id_webpoll (id_webpoll)
);
# --------------------------------------------------------
#
# Table structure for table 'webpollsQ'
#
CREATE TABLE webpollsQ (
id_webpoll int(11) NOT NULL auto_increment,
pollquestion varchar(250) NOT NULL,
lang char(3) DEFAULT 'PO' NOT NULL,
comment text,
time_stamp timestamp(14),
isvisible int(11) DEFAULT '1' NOT NULL,
PRIMARY KEY (id_webpoll),
KEY lang (lang),
KEY time_stamp (time_stamp)
);
# --------------------------------------------------------
#
# Table structure for table 'webpollsR'
#
CREATE TABLE webpollsR (
id_webpoll int(11) NOT NULL,
id_polloption int(11) NOT NULL,
id_user int(11),
time_stamp timestamp(14),
KEY id_webpoll (id_webpoll, id_polloption, time_stamp)
);
*******************************************************************************
important notes:
*******************************************************************************
webpollcontrol == 1 -> see other polls
webpollcontrol == 3 -> see current poll results
webpollcontrol == 5 -> submit voting
*/ class POLL{
var $ID_POLL;//poll ID - integer
var $PollTXT; //poll question - varchar
var $PollTime; //poll time - timestamp
var $PollComment; //poll comment - text
var $PollO; //poll options - array
var $PollR; //poll results - array
function dbconnect(){
//database connection variables
$this->DBHost = "127.0.0.1:3306";//host:port
$this->DBName = "dbase";
$this->DBUser = "user";
$this->DBPass = "password";
$this->DBTableQ = "webpollsQ";
$this->DBTableO = "webpollsO";
$this->DBTableR = "webpollsR";
//connect to database
$this->myconnection = mysql_pconnect($this->DBHost,$this->DBUser,$this->DBPass);
$this->mydatabase = mysql_select_db($this->DBName);
}
function POLL($id_webpoll = 0, $lang = "PO"){//default poll (most recent), language
$this->dbconnect();
$this->lang=$lang;
if ($id_webpoll == 0){
//select most recent poll
//get id
$sqlstring = "select id_webpoll from $this->DBTableQ where isvisible > 0 and lang = '$this->lang' order by time_stamp desc limit 0,1";
$myresult = mysql_query($sqlstring,$this->myconnection);
echo mysql_error();
while ($row = mysql_fetch_array($myresult)) $id_webpoll = $row["id_webpoll"];
}
if ($id_webpoll > 0){
$this->ID_POLL = $id_webpoll;
//prepare question
$sqlstring = "select pollquestion,time_stamp,comment from $this->DBTableQ where id_webpoll = '$id_webpoll'";
$myresult = mysql_query($sqlstring);
while ($row = mysql_fetch_array($myresult)){
$this->PollTXT = $row["pollquestion"];
$this->PollTime = $row["time_stamp"];
$this->PollComment= $row["comment"];
};
//prepare options array
$sqlstring = "select id_option,polloption from $this->DBTableO where id_webpoll = '$id_webpoll'";
$myresult = mysql_query($sqlstring);
$this->PollO = array();
while ($row = mysql_fetch_array($myresult)) $this->PollO[$row["id_option"]] = $row["polloption"];
}
//kill connection
mysql_close($this->myconnection);
}
function save_data($id_webpoll,$id_polloption,$id_user = 0){//saves user data (after form submission)
$this->dbconnect();
mysql_query("insert into $this->DBTableR(id_webpoll,id_polloption,id_user) values('$id_webpoll','$id_polloption','$id_user')");
}
function show_poll(){
GLOBAL $PHP_SELF;
$echo = $echo."n".'<form action="'.$PHP_SELF.'" method="POST" name="webpollform">';
$echo = $echo.$this->PollTXT;
reset ($this->PollO);
while (list ($key, $val) = each ($this->PollO)) {
$echo = $echo."ntt".'<br><INPUT TYPE="radio" NAME="webpollvote" VALUE="'.$key.'">'.$this->PollO[$key];
}
$echo = $echo."ntt".'<center>';
$echo = $echo."ntt".'<br><input type="submit" name="submitbastard" value="Votar">';
$echo = $echo."ntt".'<br>[<a href="'.$PHP_SELF.'?webpollcontrol=3&id_poll='.$this->ID_POLL.'">See Results</a>] [<a href="'.$PHP_SELF.'?webpollcontrol=1&id_poll='.$this->ID_POLL.'">Other Polls</a>]';
$echo = $echo."ntt".'<input type="hidden" name="webpollcontrol" value="5">';
$echo = $echo."ntt".'<input type="hidden" name="id_poll" value="'.$this->ID_POLL.'">';
$echo = $echo."ntt".'</center>';
$echo = $echo."nt".'</form>';
echo $echo;
}
function show_results(){
$this->dbconnect();
GLOBAL $PHP_SELF;
//prepare results
$sqlstring = "
select distinct(webpollsR.id_polloption),webpollsO.polloption,sum(1) as totals
from webpollsR ,webpollsO
where webpollsR.id_webpoll='$this->ID_POLL' and webpollsR.id_polloption=webpollsO.id_option
group by webpollsR.id_polloption;";
$myresult = mysql_query($sqlstring);
$this->PollR = array();
while ($row = mysql_fetch_array($myresult)) {
$this->PollR[$row["polloption"]]=$row["totals"];
$MSUM+=$row["totals"];
};
$echo = $echo.'<b>'.$this->PollTXT.'</b>';
reset ($this->PollR);
while (list ($key, $val) = each ($this->PollR)) {
$graphic = "ntt".'<br>'.'<table cellpadding="0" cellspacing="0" border="0" width="'.($this->PollR[$key]*120/$MSUM).'"><tr><td bgcolor="#FFFF00"><font size="-2"> </font></td></tr></table>';
$this->PollR[$key]=': <b>'.$this->PollR[$key].'</b>'.' <font size="-2">- ('.number_format(($this->PollR[$key]/$MSUM)*100,2).'%)</font>';
$echo = $echo."ntt".'<br>'.$key.$this->PollR[$key].'';
$echo = $echo.$graphic;
}
$echo = $echo."ntt".'<br>[<a href="'.$PHP_SELF.'?webpollcontrol=1&id_poll='.$this->ID_POLL.'">Other Polls</a>]';
echo $echo;
}
function list_polls(){
GLOBAL $PHP_SELF;
$this->dbconnect();
$sqlstring = "select id_webpoll,pollquestion from $this->DBTableQ where isvisible>0 and lang = '$this->lang' order by time_stamp desc";
$myresult = mysql_query($sqlstring);
while ($row = mysql_fetch_array($myresult)) {
$echo=$echo.'<br><a href="'.$PHP_SELF.'?id_poll='.$row["id_webpoll"].'">'.$row["pollquestion"].'</A>';
};
echo $echo;
}
function whoamI(){
//usefull for cookie and session control systems. you'll see :)
return $this->ID_POLL;
}
} ?>
|
|