Jump to content

PHP Poll


robukni

Recommended Posts

Hi everyone i am trying to set up a poll on my website. I have checked through google but i cannot get any script i find to do what i want.

 

Voting System:

 

I want to allow the user to vote for the best image out of 5.

Below each image will be a radio button.

 

There will be a submit button which when clicked will add vote to DB. I also want to have an option to show the current results.

 

Can anyone help?

 

 

Thanks

Robert

Link to comment
https://forums.phpfreaks.com/topic/46707-php-poll/
Share on other sites

ok..

heres something to get you started

 

<?php
session_start();
//get the poll id
$poll=$_GET['poll'];
//check if they have submited anything
$checkip = mysql_num_rows(SELECT ip FROM poll WHERE ip = '$_SERVER[REMOTE_ADDR]' AND poll = '$poll');
if(!$checkip){
//if the forum hasent been submited echo it
if (isset($_POST[submit])) {
echo "
<form acion='$_SERVER[php_SELF]' method=POST>
<label><input name=radio type=radio value=image1 />image1</label><br />
<label><input name=radio type=radio value=image2 />image2</label><br />
<label><input name=radio type=radio value=image3 />image3</label><br />
<label><input name=radio type=radio value=image4 />image4</label><br />
<label><input name=radio type=radio value=image5 />image5</label><br />
<input name=submit type=submit value=submit>
</forum>
";
}else{
//else insert the values into your table.. you can edit this bit to suit you
mysql_query(INSERT INTO poll(`ip`, `radio`, `poll`)VALUES('$_SERVER[REMOTE_ADDR]', '$radio', '$poll')); }
//if the ip has submitted something echo the results
}else{

$checkimage1= mysql_num_rows(SELECT radio FROM poll WHERE radio = 'image1' AND poll = '$poll');
$checkimage2= mysql_num_rows(SELECT radio FROM poll WHERE radio = 'image2' AND poll = '$poll');
$checkimage3= mysql_num_rows(SELECT radio FROM poll WHERE radio = 'image3' AND poll = '$poll');
$checkimage4= mysql_num_rows(SELECT radio FROM poll WHERE radio = 'image4' AND poll = '$poll');
$checkimage5= mysql_num_rows(SELECT radio FROM poll WHERE radio = 'image5' AND poll = '$poll');
$sum1 = $checkimage1 + $checkimage2 + $checkimage3 + $checkimage4 + $checkimage5;
$forimage1 = 100 * $checkimage1 / $sum1;
$forimage2 = 100 * $checkimage2 / $sum1;
$forimage3 = 100 * $checkimage3 / $sum1;
$forimage4 = 100 * $checkimage4 / $sum1;
$forimage5 = 100 * $checkimage5 / $sum1;
echo "
image1: $forimage1 % - $checkimage1 votes<br>
image2: $forimage2 % - $checkimage2 votes<br>
image3: $forimage3 % - $checkimage3 votes<br>
image4: $forimage4 % - $checkimage4 votes<br>
image5: $forimage5 % - $checkimage5 votes<br>
in total there have been $sum1 votes
";
?>

 

$poll is determined by the the page.. page.php?poll=1 etc..

 

i havent tested this code, but im pretty sure it will work

 

you will also need a table in your database called poll with the feilds..

 

poll, radio and ip

Link to comment
https://forums.phpfreaks.com/topic/46707-php-poll/#findComment-227617
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.