Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8

Thread: Tally Up Fields

  1. #1
    Member mikaelsnavy's Avatar
    Join Date
    Jun 2005
    Location
    Oregon
    Posts
    48
    Member #
    10358
    Hello,
    I'm almost done with my voting website! I just need help with one more thing. If I have a database with all the votes in this form:
    Code:
    Choice1          Choice2           Choice3           Choice4
    Person1           Person3           Person4          Person6    
    Person1           Person2           Person5          Person7
    I need to add up all of the people that are alike. Like
    Code:
    Person1: 56 Votes
    Person2: 46 Votes
    Person3: 58 Votes
    I really wouldn't care if it was the tally for each coulomb of choice (Like the total for choice1, choice2, choice3, choice4). I just need it to work asap.
    Thanks,
    Mikael

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,733
    Member #
    5580
    Liked
    718 times
    When you say "database", do you mean MySQL or a text flat-file?

    If "flat-file", provide a portion of that file.


  4. #3
    Member mikaelsnavy's Avatar
    Join Date
    Jun 2005
    Location
    Oregon
    Posts
    48
    Member #
    10358
    Sorry, it's mysql

  5. #4
    Senior Member leprechaun13's Avatar
    Join Date
    May 2005
    Location
    Northampton
    Posts
    487
    Member #
    10058
    dont really understand how your database works can you post the schema, so i can get a better understanding
    Regards Phil,


  6. #5
    Member mikaelsnavy's Avatar
    Join Date
    Jun 2005
    Location
    Oregon
    Posts
    48
    Member #
    10358
    O.K., I think I got it. Check this code out:
    Code:
    <?php
    $run="0";
    $person1="0";
    $person2="0";
    $person3="0";
    $person4="0";
    $person5="0";
    $person6="0";
    $person7="0";
    $person8="0";
    $person9="0";
    $person10="0";
    $person11="0";
    $person12="0";
     do { ?>
      <?php  $choice1=$row_Recordset1['Choice1']; 
      if ($choice1=="person1") { 
       $sum_person1=$person1 + 1;
       }
         if ($choice1=="person2") { 
       $sum_person2=$person2 + 1;
       }
         if ($choice1=="person3") { 
       $sum_person3=$person3 + 1;
       }
         if ($choice1=="person4") { 
       $sum_person4=$person4 + 1;
       }
         if ($choice1=="person5") { 
       $sum_person5=$person5 + 1;
       }
         if ($choice1=="person6") { 
       $sum_person6=$person6 + 1;
       }
         if ($choice1=="person7") { 
       $sum_person7=$person7 + 1;
       }
         if ($choice1=="person8") { 
       $sum_person8=$person8 + 1;
       }
         if ($choice1=="person9") { 
       $sum_person9=$person9 + 1;
       }
         if ($choice1=="person10") { 
       $sum_person10=$person10 + 1;
       }
         if ($choice1=="person11") { 
       $sum_person11=$person11 + 1;
       }
         if ($choice1=="person12") { 
       $sum_person12=$person12 + 1;
       }
      ?> 
      
      <?php  $choice2=$row_Recordset1['Choice2']; 
      $choice2=$row_Recordset1['Choice2']; 
      if ($choice2=="person1") { 
       $sum_person1=$person1 + 1;
       }
         if ($choice2=="person2") { 
       $sum_person2=$person2 + 1;
       }
         if ($choice2=="person3") { 
       $sum_person3=$person3 + 1;
       }
         if ($choice2=="person4") { 
       $sum_person4=$person4 + 1;
       }
         if ($choice2=="person5") { 
       $sum_person5=$person5 + 1;
       }
         if ($choice2=="person6") { 
       $sum_person6=$person6 + 1;
       }
         if ($choice2=="person7") { 
       $sum_person7=$person7 + 1;
       }
         if ($choice2=="person8") { 
       $sum_person8=$person8 + 1;
       }
         if ($choice2=="person9") { 
       $sum_person9=$person9 + 1;
       }
         if ($choice2=="person10") { 
       $sum_person10=$person10 + 1;
       }
         if ($choice2=="person11") { 
       $sum_person11=$person11 + 1;
       }
         if ($choice2=="person12") { 
       $sum_person12=$person12 + 1;
       }
      ?> 
      
      <?php  $choice3=$row_Recordset1['Choice3'];
      $choice3=$row_Recordset1['Choice3']; 
      if ($choice3=="person1") { 
       $sum_person1=$person1 + 1;
       }
         if ($choice3=="person2") { 
       $sum_person2=$person2 + 1;
       }
         if ($choice3=="person3") { 
       $sum_person3=$person3 + 1;
       }
         if ($choice3=="person4") { 
       $sum_person4=$person4 + 1;
       }
         if ($choice3=="person5") { 
       $sum_person5=$person5 + 1;
       }
         if ($choice3=="person6") { 
       $sum_person6=$person6 + 1;
       }
         if ($choice3=="person7") { 
       $sum_person7=$person7 + 1;
       }
         if ($choice3=="person8") { 
       $sum_person8=$person8 + 1;
       }
         if ($choice3=="person9") { 
       $sum_person9=$person9 + 1;
       }
         if ($choice3=="person10") { 
       $sum_person10=$person10 + 1;
       }
         if ($choice3=="person11") { 
       $sum_person11=$person11 + 1;
       }
         if ($choice3=="person12") { 
       $sum_person12=$person12 + 1;
       }
       ?>
      
      <?php  $choice4=$row_Recordset1['Choice4']; 
      $choice4=$row_Recordset1['Choice4']; 
      if ($choice4=="person1") { 
       $sum_person1=$person1 + 1;
       }
         if ($choice4=="person2") { 
       $sum_person2=$person2 + 1;
       }
         if ($choice4=="person3") { 
       $sum_person3=$person3 + 1;
       }
         if ($choice4=="person4") { 
       $sum_person4=$person4 + 1;
       }
         if ($choice4=="person5") { 
       $sum_person5=$person5 + 1;
       }
         if ($choice4=="person6") { 
       $sum_person6=$person6 + 1;
       }
         if ($choice4=="person7") { 
       $sum_person7=$person7 + 1;
       }
         if ($choice4=="person8") { 
       $sum_person8=$person8 + 1;
       }
         if ($choice4=="person9") { 
       $sum_person9=$person9 + 1;
       }
         if ($choice4=="person10") { 
       $sum_person10=$person10 + 1;
       }
         if ($choice4=="person11") { 
       $sum_person11=$person11 + 1;
       }
         if ($choice4=="person12") { 
       $sum_person12=$person12 + 1;
       }
      $main_sum=$run + 1;
      $run=$main_sum;
      $person1=$sum_person1; 
      $person2=$sum_person2;
      $person3=$sum_person3;
      $person4=$sum_person4;
      $person5=$sum_person5;
      $person6=$sum_person6;
      $person7=$sum_person7;
      $person8=$sum_person8;
      $person9=$sum_person9;
      $person10=$sum_person10;
      $person11=$sum_person11;
      $person12=$sum_person12;  
      ?>
      
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
      Person 1:<?php
      echo $sum_person1; 
      ?> <br /> Person 2:<?
      echo $sum_person2;
      ?> <br /> Person 3: <?
      echo $sum_person3;
      ?> <br /> Person 4: <?
      echo $sum_person4;
      ?> <br /> Person 5: <?
      echo $sum_person5;
      ?> <br /> Person 6: <?
      echo $sum_person6;
      ?> <br /> Person 7: <?
      echo $sum_person7;
      ?> <br /> Person 8: <?
      echo $sum_person8;
      ?> <br /> Person 9: <?
      echo $sum_person9;
      ?> <br /> Person 10: <?
      echo $sum_person10;
      ?> <br /> Person 11: <?
      echo $sum_person11;
      ?> <br /> Person 12: <?
      echo $sum_person12;
       ?> <br /> Total People Who Voted: <?
       echo $main_sum;
       ?>
    What do you guys think about that? I seamed to work for me!

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Owie... The violations of DRY hurt my eyes :-P

    Let's try this for tallying up the choices instead of all those if statements:
    PHP Code:
    $choices = Array( $row_Recordset1['Choice1'], $row_Recordset1['Choice2'], $row_Recordset1['Choice3'], $row_Recordset1['Choice4'] );

    for ( 
    $choices as $choice )
        eval( 
    '$' "sum_$choice += 1" ); 
    (And then the echoing.)

  8. #7
    Member mikaelsnavy's Avatar
    Join Date
    Jun 2005
    Location
    Oregon
    Posts
    48
    Member #
    10358
    lol, thanks. PHP noob here.

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    No worries. Turns out, even experienced programmers have trouble understanding the wonderful power that is [minicode]eval[/minicode]. My understanding and appreciation of that function only came from learning Ruby, where programming and metaprogramming aren't really distinguishable from each other :-)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:40 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com