Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting % 1

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
GB
Hi, i am having problems getting the % of votes cast:

here is my code
Code:
<?php
//get poll q
$qTitle = mysql_query("SELECT pollTitle FROM poll_questions WHERE poll_id = '$poll_id'") or die(mysql_error());
	list($PollTitle) = mysql_fetch_row($qTitle);
?> 
<table width="100%" border="0" cellspacing="0" cellpadding="1"> 
  <tr> 
    <td bgcolor="#000000"> <table width="100%" border="0" cellspacing="0" cellpadding="10"> 
        <tr> 
          <td bgcolor="1e477e"> <?php echo $PollTitle; ?> </td> 
        </tr> 
      </table></td> 
  </tr> 
</table> 
<br> 
<table width="100%" border="0" cellspacing="3" cellpadding="1"> 
<?php
$qAns = mysql_query("SELECT optionText, optionCount, voteID FROM poll_answers WHERE poll_id = '$poll_id'") or die(mysql_error());
	while(list($opT,$opC,$vID) = mysql_fetch_row($qAns)){
		if($opT != ""){
                $w = 100 - $opC;
		$o = 100-$w;
?> 
<tr> 
<td width="100" align="right" nowrap><?php echo $opT; ?></td> 
<td bgcolor="#000000"> 
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
  <tr> 
    <td width="<?php echo $w; ?>%" background="../assets/images/pollBar.gif"><?php echo $opC; ?></td> 
    <td width="<?php echo $o; ?>%" bgcolor="#FF3300"></td> 
  </tr>
  </table></td> 
    <?php
  }//end if
}
?> 
</table>

I want the td width % to be the % of casts voted!

any ideas how to get the %?


Regards,

Martin

Gaming Help And Info:
 
% of votes = (number of votes wanted) / (total number of votes)

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
* 100 (for percent)

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
This is really basic problem. I'll give you the pseudo code,:
1. Calculate the total votes and assign the number to a variable
2. Calculate the individual answer percentage by dividing the number of votes by the total and mulitply the result by 1000

It doesn't sound dumb, no. It sounds lazy.
 
Oh, yes. The typo. I should be kind now that I have shown my own imperfection and give you the code.
Code:
# lets assume the votes are in an associative array array
$total = array_sum($votes);

# iterate the array
foreach ($votes as $key=>$value){
   $votes[$key]['percentage'] = round($value/$total*100);
}

# you can print the calculated values later by iterating the array.
 
yea i got this code:
Code:
<?php
include "../config.php";

$poll_id = $_GET['id'];

$qAns = mysql_query("SELECT optionText, optionCount, voteID FROM poll_answers WHERE poll_id = '$poll_id'") or die(mysql_error());
	while(list($opT,$opC,$vID) = mysql_fetch_row($qAns)){
		if($opT != ""){
		
		$total+=$opC;
		$percent = round(($opC/$total)*100);		
		$less = 100-$percent;
?>
Question: <?php echo $opT; ?> :: Votes: <?php echo $percent; ?>%<br>
<?php
	}
}
?>
this works fine, but the option at the top doesn't get included in the %?? it just stays at 100%

why is this??


Regards,

Martin

Gaming Help And Info:
 
Retrieve the total first with a query:
Code:
SELECT sum(optionCount) FROM poll_answers WHERE poll_id = '$poll_id'

You add up the total when you retrieve the individual rows. That won't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top