php - Calculate percentage of up votes for each record. -
i have searched site , google , can't figure out
<?php $artistname = "select * voting"; $artistnameresults = mysql_query( $artistname ) or die( "could not 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>\n"; } ?>
how echo out percentage of total votes each record table?
so first total votes in variable, in next check if total votes more 0 avoid 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 $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>\n"; }
Comments
Post a Comment