This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 2

Polls... how to show results


  1. ggmaury
    Member

    I just made a form to make a poll. The form works fine but I need to display the results with the total votes per choice and the percentage each one has.
    I know I can export the values to xls or csv but I need to get them automaticly right after each vote.
    Can you suggest any workaround?
    Maybe updating the csv automaticly after casting a vote?

    Thanks

    Posted 16 years ago #
  2. yuniar

    Sorry, I'm afraid I can't help much with this one, since it would require pretty much custom code.


    MachForm Founder

    Posted 16 years ago #
  3. ggmaury
    Member

    Yuniar:
    I achieved that by accessing the database and using form_id, position and option from ap_element_options table to select the form and then count the votes contained in element_1 (table ap_form1, ap_form2 and so on depending on the form number).
    If works for a single multiple choice field and redirects to my results viewer.
    check it out at:
    http://campeche.com/encuestas/formas/view.php?id=1

    There are 4 forms so changong the id to 2, 3 or 4 you can see the others.

    I just need to make a page with a menu of available polls to control the flow.

    Posted 16 years ago #
  4. yuniar

    ooh.. that looks nice!


    MachForm Founder

    Posted 16 years ago #
  5. girljinbink
    Member

    Can you elaborate a bit more on how you coded the results page?

    Posted 15 years ago #
  6. AMurray
    Pro Member

    I don't see anything except a blank screen with green background.....no poll or results there.

    Posted 15 years ago #
  7. ggmaury
    Member

    AMurray:

    Sorry, I disabled that form.
    You can check an example in http://campeche.com/encuestas/index.php?id_1=8&id_2=1&id_3=0&id_4=0

    girljinbink:

    I made a machform directory where I use only 1 field multiple choice forms.

    The machform db structure includes a table called 'ap_element_options' where field 'form_id' is the number of the form you want to show results from. All rows with the same 'form_id' will have different 'position' (or 'option_id' which have the same values). Each 'position' is a choice for the form.

    By querying ($id is the form number passed in the url so the same script takes care of all polls):

    $result = mysql_query("SELECT position, option FROM ap_element_options WHERE form_id = '$id'");

    you will get all rows with the same form_id thus getting all choices (choice number and name) of your poll:

    while ($row = mysql_fetch_array($result))
    {
    $choice[$total]['position'] = $row['position']; //choice number
    $choice[$total]['option'] = $row['option']; //text describing choice
    $choice[$total]['votes'] = 0; //will store total votes per choice

    $total++;
    }

    At this point, you have a multidimentional array holding poll values.

    Then you count the number of times (rows) each choice has been selected (I used a for loop based on $total). Those are saved in table 'ap_form_x' where 'x' is your 'form_id' and field element_1 has the same value than 'option':

    $result = mysql_query("SELECT element_1 , COUNT( * ) as votes FROM ".$apform." WHERE element_1 ='$i' GROUP BY element_1");

    $row = mysql_fetch_array($result);
    $choice[$i]['votos'] = (int)$row['votes'];
    $totalVotes = $totalVotes + $choice[$i]['votes'];

    then I sort the array based on total votes to show them from max to min votes.

    From that point you display the results of your poll.

    I hope that helps.

    Posted 15 years ago #
  8. girljinbink
    Member

    I'm at a beginning level of PHP, so although I understand most of what you're saying, there are some holes...

    would you be willing to send me the code for your results page?
    mjohnson(at)tcgrec.com

    Posted 15 years ago #
  9. garybpenn
    Member

    ggmaury -

    I am attempting to do the same thing that you did here several years back. That is, show instant results after a poll is submitted. Can you possible turn the example links back on or share your code? Thank you!

    Posted 11 years ago #

RSS feed for this topic

Reply