I have searched this site and Google and I can't figure it out
<?php
$artistname = "SELECT * from voting";
$artistnameresults = mysql_query( $artistname )
or die( "Could not get video games " .mysql_error() );
for( $i = 0; $i < mysql_numrows( $artistnameresults ); $i++ ) {
$data = mysql_fetch_array( $artistnameresults );
echo "<div>". $data['artist'] ." has " . $data['votes'] ." votes</div>
";
}
?>
How do I echo out percentage of total votes for each record from my table?
So first I would get the total votes in the variable, then in the next for would check if the total votes is more than 0 to avoid the error.
$num_rows = mysql_numrows($artistnameresults);
$total_votes = 0;
$rows = array();
for( $i = 0; $i < $num_rows; $i++ ) {
$data = mysql_fetch_array( $artistnameresults );
$total_votes += $data['votes'];
$row[] = $data;
}
foreach($rows as $data) {
echo "<div>". $data['artist'] ." has " . $data['votes'] ." votes ";
if($total_votes > 0)
$temp_votes = $data['votes']/$total_votes;
else
$temp_votes = 1;
echo '( '.($temp_votes*100).'%)';
echo "</div>
";
}