Jump to content

creating a +/- poll (returning multiple values)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
SuchTheFool

SuchTheFool

    Newbie

  • Members
  • Pip
  • 3 posts
fist of all i'm new with webpage programming and english is not my native language so, please, bare with me
also it's more of a html problem, than a php, so feel free to move it if it doesn't belong in this section

the thing is, i'm trying to create a poll with multiple choices that should return a value on each of the choices
a picture to help understand it easier

i have made a script (vote.php) that takes the user chosen value and makes all the calculations etc, but now i can make it so that only one value is returned.
how to return multiple values? so that every choice (opt1, opt2, etc.) returned one of the two (+ or -) values

here's the code i've been messing around:
<table>

    <tr>

  <td>

  <em><?=$header?></em><br />


  <form action="vote.php" method="post">

    <hr />     

    <p><input name="choice0" type="radio" value="0" /> <input name="choice00" type="radio" value="0" /> <?=$choices[0]?> </p>

    <p><input name="choice1" type="radio" value="1" /> <input name="choice01" type="radio" value="1" /> <?=$choices[1]?> </p>

<? if(isset($choices[2]) && trim($choices[2]) != "") { ?>     

    <p><input name="choice2" type="radio" value="2" /> <input name="choice02" type="radio" value="2" /> <?=$choices[2]?> </p>

<? } if(isset($choices[3]) && trim($choices[3]) != "") { ?>

    <p><input name="choice3" type="radio" value="3" /> <input name="choice03" type="radio" value="3" /> <?=$choices[3]?> </p>

<? } if(isset($choices[4]) && trim($choices[4]) != "") { ?>

    <p><input name="choice4" type="radio" value="4" /> <input name="choice04" type="radio" value="4" /> <?=$choices[4]?> </p>

<? } if(isset($choices[5]) && trim($choices[5]) != "") { ?>

    <p><input name="choice5" type="radio" value="5" /> <input name="choice05" type="radio" value="5" /> <?=$choices[5]?> </p>

<? } if(isset($choices[6]) && trim($choices[6]) != "") { ?>

    <p><input name="choice6" type="radio" value="6" /> <input name="choice06" type="radio" value="6" /> <?=$choices[6]?> </p>

<? } if(isset($choices[7]) && trim($choices[7]) != "") { ?>

    <p><input name="choice7" type="radio" value="7" /> <input name="choice07" type="radio" value="7" /> <?=$choices[7]?> </p>

<? } if(isset($choices[8]) && trim($choices[8]) != "") { ?>

    <p><input name="choice8" type="radio" value="8" /> <input name="choice08" type="radio" value="8" /> <?=$choices[8]?> </p>

<? } if(isset($choices[9]) && trim($choices[9]) != "") { ?>

    <p><input name="choice9" type="radio" value="9" /> <input name="choice09" type="radio" value="9" /> <?=$choices[9]?> </p>

<? } ?>


<hr />    

  <p><strong><?=$total_votes?></strong> total <br /><br /></p>

  <? if(!in_array($user,$users)) { ?>

<p><input name="submit" type="submit" value="vote" /></p>

  <? } else { ?>

<p><em>vote casted</em></p>  

</form>  

<form action="delete.php" method="post">

<p><input name="submit" type="submit" value="change vote" /></p  

</form>    

  <? }  ?>


    </td>

    </tr>

   </table> 

where $choices and $user is read form a txt file
if there are any questions feel free to ask.
any help would be appreciated
thanks

#2
Jody LeCompte

Jody LeCompte

    Newbie

  • Members
  • PipPip
  • 18 posts
Could you post a screen shot or url of the actual poll? I have a pretty decent idea of what your trying to accomplish but seeing the poll itself would help.

Am I correct in assuming you will have multiple poll options, with a + or - vote cast(like / dislike system essentially?), but you only want a user to vote on one choice out of the poll?

#3
SuchTheFool

SuchTheFool

    Newbie

  • Members
  • Pip
  • 3 posts
thank you for the reply

yeah, pretty much like like/dislike system
here's the picture
Posted Image
i'd like the user to vote on multiple choices (for example - opt1, opt5, op7, or just all of them), but the problem is right now it is only returning one of the value (i think it was the last option the user checks)
so i'm thinking there should be multiple variables that i have to pass on and all the calculating would take place in the vote.php

#4
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
hmm
why dont you use same input name for radios?
i think your inputs should look like this:

<input name='opt1' type='radio' value='1' />

<input name='opt1' type='radio' value='0' />

and check variable opt1 in the php part if is 0 or 1
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#5
Jody LeCompte

Jody LeCompte

    Newbie

  • Members
  • PipPip
  • 18 posts
You need to change the naming and value of your radio inputs firstly. Each set of + and - should share the same name; so that only one may be selected. Rather than having the same value in each, however, you need to have a single specific value for all + inputs, and a different specific value for all - inputs. Then on the PHP side, you'd have say, $_POST['optionOne'] with a value of either + or - which you would use to alter the total votes.

#6
SuchTheFool

SuchTheFool

    Newbie

  • Members
  • Pip
  • 3 posts
thank you so much, it worked

i already had same names for the options, but copy pasted the wrong code (the one i was just fooling around, trying to get something working) :blushing:

here's the modified code
<?
<p><input name="choice0" type="radio" value="0" /> <input name="choice00" type="radio" value="0" /> <?=$choices[0]?> </p>
    <p><input name="choice1" type="radio" value="1" /> <input name="choice01" type="radio" value="1" /> <?=$choices[1]?> </p>
<? if(isset($choices[2]) && trim($choices[2]) != "") { ?>     
    <p><input name="choice2" type="radio" value="2" /> <input name="choice02" type="radio" value="2" /> <?=$choices[2]?> </p>
<? } if(isset($choices[3]) && trim($choices[3]) != "") { ?>
    <p><input name="choice3" type="radio" value="3" /> <input name="choice03" type="radio" value="3" /> <?=$choices[3]?> </p>
<? } if(isset($choices[4]) && trim($choices[4]) != "") { ?>
    <p><input name="choice4" type="radio" value="4" /> <input name="choice04" type="radio" value="4" /> <?=$choices[4]?> </p> 
    //etc

and the vote.php that does all the calculating

<?
 
    include("poll_script.php");
         
	$choice0 = $_POST["choice0"];   
    $choice1 = $_POST["choice1"];
    //etc.
    
	if(!in_array($user,$users)) {
	    
    if($choice0 == 0){
	$choices[10] += 1;}
    elseif($choice0 == 1) {
    $choices[10] -= 1;}
    
    if($choice1 == 0){
    $choices[11] += 1;}
    elseif($choice1 == 1) {
    $choices[11] -= 1;}
    
    //etc.

	$file_write = $choices[0]."|".$choices[1]."|".$choices[2]."|".$choices[3]."|".$choices[4]."|".$choices[5]."|".$choices[6]."|".$choices[7]."|".$choices[8]."|".$choices[9]."|".$choices[10]."|".$choices[11]."|".$choices[12]."|".$choices[13]."|".$choices[14]."|".$choices[15]."|".$choices[16]."|".$choices[17]."|".$choices[18]."|".$choices[19];
    //and the rest of the code that writes the users and choices in a txt file    
    

but there's one question i'd like to ask:
is there any better way to do it, and not write the $choice0 = $_POST["choice0"]; and if($choice0 == 0){ $choices[10] += 1;} elseif($choice0 == 1) { $choices[10] -= 1;} for every of the options?
as i said, it works great, but it would take a lot of code (especially if there would be more than 20-30 options to vote for)

again, thank you very much



EDIT:

if anyone's interested, i solved the problem
might not be perfect, but gets the job done

here's the code
if(!in_array($user,$users)) {
	
    for ( $chnum = 10 and $chn = 0; $chnum <= 19 and $chn <=9; $chnum += 1 and $chn += 1) {
        
    $choice = $_POST["choice"."$chn"];        
       
    if($choice == 0){
	$choices[$chnum] += 1;}
    elseif($choice == 1) {
    $choices[$chnum] -= 1;}
       
    }
     
	$file_write = $choices[0]."|".$choices[1]."|".$choices[2]."|".$choices[3]."|".$choices[4]."|".$choices[5]."|".$choices[6]."|".$choices[7]."|".$choices[8]."|".$choices[9]."|".$choices[10]."|".$choices[11]."|".$choices[12]."|".$choices[13]."|".$choices[14]."|".$choices[15]."|".$choices[16]."|".$choices[17]."|".$choices[18]."|".$choices[19];
      

Edited by SuchTheFool, 09 June 2010 - 06:59 AM.
soulution