Log in

View Full Version : working on a poll (NEED HELP)



Demonicman
02-26-2007, 10:13 PM
im VERY new with php and i have been working on a site... i want to add a poll but i am stuck!

Here is topgames.php



<?php
mysql_connect("localhost", "(removed username)", "(pass removed for security reasons)") or die(mysql_error());
mysql_select_db("demonic_vote") or die(mysql_error());

$ip = "$HTTP_SERVER_VARS[REMOTE_ADDR]";
$Awesome=mysql_query("select * from results where Awesome='$Awesome'");
$Ok=mysql_query("select * from results where Ok='$Ok'");
$Terrible=mysql_query("select * from results where Terrible='$Terrible'");
$dupe1 = mysql_query("select * from results where ip='$ip'");

if ($dupe1 > 0) {
?>
<font color="00ff00"><center>Results:<br>
<h4>Awesome Votes: <?php print($Awesome) ?><br>
Ok Votes: <?php print($Ok) ?><br>
Terrible Votes: <?php print($Terrible) ?></h4></center></font>

<?php
}
else
{
?>
<center><form action="vote.php" method="post">
<font color="#00ff00">Vote that this game is</font> <font color="#FFFF00">Awesome:</font> <input type="radio" name="Vote" value="Awesome"><br>
<font color="#00ff00">Vote that this game is</font> <font color="#FFFF00">Ok:</font> <input type="radio" name="Vote" value="Ok"><br>
<font color="#00ff00">Vote that this game is</font> <font color="#FFFF00">Terrible</font>: <input type="radio" name="Vote" value="Terrible"><p>
<input type="submit" value="Vote" name="Vote">
</form></center>
<?php
}
?>

Then this is vote.php



<?php
mysql_connect("localhost", "(removed username)", "(pass removed)") or die(mysql_error());
mysql_select_db("demonic_vote") or die(mysql_error());

$Awesome=mysql_query("select * from results where Awesome='$Awesome'");
$Ok=mysql_query("select * from results where Ok='$Ok'");
$Terrible=mysql_query("select * from results where Terrible='$Terrible'");

if (isset($_POST['submit'])) {
if ($_POST['Awesome'])
{
mysql_query("UPDATE * results SET Awesome=Awesome+1 WHERE Awesome='$Awesome'")
or die(mysql_error());
echo "Voted <a href=\"topgames.php\">Go to topgames</a>";
exit;
}
elseif ($_POST['OK'])
{
mysql_query("UPDATE * results SET Ok=Ok+1 WHERE Ok='$Ok'")
or die(mysql_error());
echo "Voted <a href=\"topgames.php\">Go to topgames</a>";
exit;
}
elseif ($_POST['Terrible'])
{
mysql_query("UPDATE * results SET Terrible=Ok+1 WHERE Terrible='$Terrible'")
or die(mysql_error());
echo "Voted <a href=\"topgames.php\">Go to topgames</a>";
exit;
}
}
?>
Something happened... <a href="topgames.php">Go back</a>

Lastly the MYSQL table



CREATE TABLE `results` (
`id` int(11) NOT NULL auto_increment,
`ip` int(20) NOT NULL default '1.1.1.1',
`Awesome` int(30) NOT NULL default '0',
`Ok` int(30) NOT NULL default '0',
`Terrible` int(30) NOT NULL default '0',
UNIQUE KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

if any of you smart coders out there could help it would be great... you see i tried writing this code with logic, not syntax, and i completely messed up

again PLEASE help... this is one of the first things i coded without a wizard and its difficult it is

Demonicman
02-26-2007, 11:52 PM
wow no suggestions? im extremely dissapointed with all you smart people out there XD

i also forgot to mention that you can see this code in use at www.kalrith.com

Blake
02-27-2007, 12:00 AM
Here's a poll script that I made awhile back:




#updated 7/14/06

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>Render</title></head>

<body style="background-color: rgb(0, 0, 0); background-image: url(bg.gif); color: rgb(255, 255, 255);" alink="#ffffff" link="#ffcc66" vlink="#cc6600">

<?php


function percent($value, $total)
{
return round($value * 100.0 / $total, 2);
}

$vote = $_REQUEST['vote'];

include('private/db_connect.inc');

if (strlen($vote) != 0)
{
$query1 = "UPDATE votes SET votecount = votecount + 1 WHERE songname = '$vote'";
$result1 = @mysql_query($query1);
if (!$result1)
{
echo 'MySql error.';
}
}

$query2 = "SELECT * FROM votes ORDER BY 'songname' ASC";
$result2 = @mysql_query($query2);

if ($query2)
{
$fudge_factor = 0;
$votes = mysql_fetch_array($result2, MYSQL_NUM);
$broken = $votes[1] + $fudge_factor;
$votes = mysql_fetch_array($result2, MYSQL_NUM);
$someday = $votes[1] + $fudge_factor;
$votes = mysql_fetch_array($result2, MYSQL_NUM);
$stranded = $votes[1] + $fudge_factor;
$votes = mysql_fetch_array($result2, MYSQL_NUM);
$wasted = $votes[1] + $fudge_factor;
$votes = mysql_fetch_array($result2, MYSQL_NUM);
$youare = $votes[1] + $fudge_factor;
$totalvotes = $broken + $someday + $stranded + $wasted + $youare;
$brokenPercent = percent($broken, $totalvotes);
$somedayPercent = percent($someday, $totalvotes);
$strandedPercent = percent($stranded, $totalvotes);
$wastedPercent = percent($wasted, $totalvotes);
$youarePercent = percent($youare, $totalvotes);
echo "Broken Hearted Man: $brokenPercent&#37;<br>";
echo "Someday Go Away: $somedayPercent%<br>";
echo "Stranded: $strandedPercent%<br>";
echo "Wasted Life: $wastedPercent%<br>";
echo "You Are: $youarePercent%";
}
else
{
echo 'MySql error.';
}

?><br>
<script type="text/javascript"><!--
google_ad_client = "pub-3724700937860885";
google_ad_width = 234;
google_ad_height = 60;
google_ad_format = "234x60_as";
google_ad_type = "text_image";
google_ad_channel ="";
google_color_border = "666666";
google_color_bg = "000000";
google_color_link = "000000";
google_color_text = "FFFFFF";
google_color_url = "FFCC66";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</body></html>

Demonicman
02-27-2007, 12:09 AM
thanks but a sql would be nice if you please

boxxertrumps
02-27-2007, 01:42 AM
SQL gives me headaches. so heres a flat file DB poll code. works perfect, i made it specifically for you.

<?php
$save = "save.txt";
$file = file_get_contents($save); if(!$file) {die("File Not Found");};
$array = explode("|",$file);

if(isset($_POST['choice'])) {

$cho = $_POST['choice'];
$array[$cho]++;
$data = implode("|",$array);
$handle = fopen($save,"w+");
fwrite($handle,$data);
fclose($handle);

echo "You picked choice number". $cho ."\r\n<br>";
echo "Click <a href=\"?look\">Here</a> to View The Results.";

} elseif (isset($_GET['look'])) { ?>

Choice One: <?php echo $array['0']; ?><br>
Choice Two: <?php echo $array['1']; ?><br>
Choice Three: <?php echo $array['2']; ?><br>

<?php /* Repeat above and below For Each Choice you want... */ } else { ?>
<form action="poll.php" method="post">

Choice One: <input type="radio" name="choice" value="0"><br>
Choice Two: <input type="radio" name="choice" value="1"><br>
Choice Three: <input type="radio" name="choice" value="2"><br>

<input type="submit" value="Vote" name="Vote"></form>
<?php };
/* Contents of "save.txt"

0|0|0

have a zero for each choice you want, and a | in between each zero.
in the "look" section, repeat

*/
?>

Rohan72
02-27-2007, 01:59 AM
I'm pretty new to PHP too. I needed a poll on my site and i ran into this script:

http://www.cj-design.com/products/free_downloads/php_scripts/cjdynamicpoll

It's free and pretty easy to set up and maintain.

thetestingsite
02-27-2007, 02:05 AM
Another one that I can think of (which uses AJAX, PHP, and MySQL) is on DHTMLgoodies.com. Here's the demo:
http://dhtmlgoodies.com/scripts/ajax-poller/ajax-poller.html

And info about how to install it is here:
http://dhtmlgoodies.com/index.html?whichScript=ajax-poller

Hope this helps.

Demonicman
02-27-2007, 03:48 AM
wow thanks for all the help guys! i should be able to create a really decent poll now! thanks all around