jc_ply Posted March 24, 2008 Share Posted March 24, 2008 Hey guys, you were so helpful with my last issue (which I'm very greatful for) I'm back to share another growing pain, and the last for now at least. my row is looking like this: network | user_amt | war_one | war_two | war_three | war_four | war_five sample 50 70 5 8 3 9 the war_one - five fields are just counters for the users selections but I need to calculate the highest value from the last five war fields (e.g 70) and divide it by the total value of the war fields (e.g 95) then times by 100 to return a percentage along with the highest fields name. I have similar set ups in the database where the same returns are needed but their universal and can be column calculated where as this table is network specific so proving to be a bit difficult (for my learner level php) Any help would be as always greatly appreciated , Jason Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/ Share on other sites More sharing options...
ethought Posted March 24, 2008 Share Posted March 24, 2008 Maybe try making an array, finding the max value, then finding the sum value and dividing and multiplying by 100: $query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db); while($rows = mysql_fetch_array){ $score_array = array($rows[war-one], $rows[war-two], $rows[war-three], $rows[war-four], $rows[war-five]); $max_score = max($score_array); $sum_scores = sum($score_array); $percentage = ($max_score / $sum_score) * 100; echo "Average Score is $percentage <br>"; } Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/#findComment-499238 Share on other sites More sharing options...
ryeman98 Posted March 24, 2008 Share Posted March 24, 2008 Maybe try making an array, finding the max value, then finding the sum value and dividing and multiplying by 100: $query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db); while($rows = mysql_fetch_array){ $score_array = array($rows[war-one], $rows[war-two], $rows[war-three], $rows[war-four], $rows[war-five]); $max_score = max($score_array); $sum_scores = sum($score_array); $percentage = ($max_score / $sum_score) * 100; echo "Average Score is $percentage <br>"; } $rows is already an array, why would you put each individual result into another array? Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/#findComment-499252 Share on other sites More sharing options...
ethought Posted March 24, 2008 Share Posted March 24, 2008 sorry the code should have been $query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db); while($rows = mysql_fetch_array($query)){ $score_array = array($rows[war-one], $rows[war-two], $rows[war-three], $rows[war-four], $rows[war-five]); $max_score = max($score_array); $sum_scores = sum($score_array); $percentage = ($max_score / $sum_score) * 100; echo "Average Score is $percentage <br>"; } re arraying because original question only wanted top value for 'war' values not the first user_amt value... I am not an expert so would be interested to see another way this could be done.. Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/#findComment-499260 Share on other sites More sharing options...
ryeman98 Posted March 24, 2008 Share Posted March 24, 2008 sorry the code should have been $query = mysql_query("SELECT * FROM table_name WHERE id='$id'", $db); while($rows = mysql_fetch_array($query)){ $score_array = array($rows[war-one], $rows[war-two], $rows[war-three], $rows[war-four], $rows[war-five]); $max_score = max($score_array); $sum_scores = sum($score_array); $percentage = ($max_score / $sum_score) * 100; echo "Average Score is $percentage <br>"; } re arraying because original question only wanted top value for 'war' values not the first user_amt value... I am not an expert so would be interested to see another way this could be done.. Ah. You could just use the mysql MAX and SUM by just choosing the 'war' fields instead of everything. (*) Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/#findComment-499261 Share on other sites More sharing options...
jc_ply Posted April 11, 2008 Author Share Posted April 11, 2008 Hey guys thanks for the help but still having some problems with it. Been away for a while trying different methods but kinda getting the feeling it may jus be a bit of bad syntax... Any further clarification would be great. Link to comment https://forums.phpfreaks.com/topic/97573-row-calculation-problem/#findComment-514431 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.