11-30-2007, 04:36 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Nov 2007
Posts: 13
Thanks: 0
|
Poll sample
Hi, this is my first post... A way back I did a simple poll for a web site, here is a snippet from the code that displayed everything: (its an excrept from a class, but everything you need should be there)
PHP Code:
$pid = $this->pollID;
$maxWidth = 120;
$sql = "SELECT COUNT(ID) FROM poll_responses WHERE Poll_id = $pid";
$rData = mssql_query($sql);
$rRow = mssql_fetch_array($rData);
$vCount = $rRow[0];
$sql = "SELECT * FROM poll_answers WHERE Poll_id = $pid ORDER BY sOrd ASC";
$rData = mssql_query($sql);
$x = 1;
$t = 0; //total counted
$n = mssql_num_rows($rData); //number of questions
if ($vCount != 0) {
while($rRow = mssql_fetch_assoc($rData)) {
$rid = $rRow["ID"];
$txt = $rRow["Text"];
$sql = "SELECT COUNT(ID) FROM poll_responses WHERE Poll_id = $pid AND Ans_id = $rid";
$dData = mssql_query($sql);
$dRow = mssql_fetch_array($dData);
$newcount = $dRow[0];
$avg = round(($newcount / $vCount),3);
if ($x == $n) {
$avg = 1 - $t;
}
$graphWidth = $maxWidth * $avg;
$txtAvg = $avg * 100 . "%";
print $txt . ": ($txtAvg)<br /><div id=\"graph$x\" style=\"height:7px; width:{$maxWidth}px; align:left; background-color:#" . $this->bbg . "; float: left; \"><img src=\"/includes/graph.php?color=" . $this->bc . "\" height=\"7\" width=\"$graphWidth\" align=\"middle\" alt=\"$txtAvg\" /></div><br />\r\n";
$x++;
$t = $t + $avg;
} //while
} else {
print("There are no votes yet");
}
I've never been big on group by for SQL either, so I took the less efficient route of just doing nested DB calls. Poll can be viewed in action at http://ww2.boatusangler.com/. If you need clarification on something, just ask, I'll do my best.
|
|
|
|